Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6d68614e8e40362e32e3cbc169365707215152d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
 * Copyright (c) 2016 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal.model.value;

import org.eclipse.jpt.common.utility.closure.Closure;
import org.eclipse.jpt.common.utility.internal.ObjectTools;

/**
 * This class adds support for plugging in a closure that can be used to set
 * the model's value.
 * <p>
 * This class is most useful when the adapted model is changed <em>outside</em>
 * the property value model; typically by modifying the original
 * <em>domain</em> model.
 * 
 * @param <V> the type of the model's derived value
 */
public final class PluggableModifiablePropertyValueModelAdapter<V>
	implements PluggableModifiablePropertyValueModel.Adapter<V>
{
	/** Read the adapted model with this. */
	private final BasePluggablePropertyValueModel.Adapter<V> adapter;

	/** Write the adapted model with this. */
	private final Closure<? super V> closure;


	public PluggableModifiablePropertyValueModelAdapter(BasePluggablePropertyValueModel.Adapter<V> adapter, Closure<? super V> closure) {
		super();
		if (adapter == null) {
			throw new NullPointerException();
		}
		this.adapter = adapter;
		if (closure == null) {
			throw new NullPointerException();
		}
		this.closure = closure;
	}

	public V engageModel() {
		return this.adapter.engageModel();
	}

	public V disengageModel() {
		return this.adapter.disengageModel();
	}

	public void setValue(V value) {
		this.closure.execute(value);
	}

	@Override
	public String toString() {
		return ObjectTools.toString(this, this.adapter);
	}


	// ********** Factory **********

	public static final class Factory<V>
		implements PluggableModifiablePropertyValueModel.Adapter.Factory<V>
	{
		private final BasePluggablePropertyValueModel.Adapter.Factory<V, ? extends BasePluggablePropertyValueModel.Adapter<V>> factory;
		private final Closure<? super V> closure;

		public Factory(BasePluggablePropertyValueModel.Adapter.Factory<V, ? extends BasePluggablePropertyValueModel.Adapter<V>> factory, Closure<? super V> closure) {
			super();
			if (factory == null) {
				throw new NullPointerException();
			}
			this.factory = factory;
			if (closure == null) {
				throw new NullPointerException();
			}
			this.closure = closure;
		}

		public PluggableModifiablePropertyValueModel.Adapter<V> buildAdapter(BasePluggablePropertyValueModel.Adapter.Listener<V> listener) {
			return new PluggableModifiablePropertyValueModelAdapter<>(this.factory.buildAdapter(listener), this.closure);
		}

		@Override
		public String toString() {
			return ObjectTools.toString(this);
		}
	}
}

Back to the top