Skip to main content
summaryrefslogtreecommitdiffstats
blob: 07e2fd86615a83c512c7f2670aeab506e4bdaf7f (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * 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.internal.ObjectTools;
import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.transformer.Transformer;

/**
 * Adapt a {@link PropertyValueModel property value model} to
 * another {@link PropertyValueModel property value model}, sorta.
 * <p>
 * This adapter is constructed with a {@link PropertyValueModel
 * property value model} and a {@link Transformer transformer} that can
 * transform the wrapped model's value to this model's derived value.
 * <p>
 * This is an adapter that can be used by a {@link PluggablePropertyValueModel}.
 * 
 * @param <V1> the type of the <em>wrapped</em> model's value
 * @param <V2> the type of the model's <em>derived</em> value
 * 
 * @see PluggablePropertyValueModel
 */
public final class TransformationPluggablePropertyValueModelAdapter<V1, V2>
	implements PluggablePropertyValueModel.Adapter<V2>, PropertyChangeListener
{
	/** The wrapped model */
	private final PropertyValueModel<? extends V1> propertyModel;

	/** Transformer that converts the wrapped model's value to this model's value. */
	private final Transformer<? super V1, ? extends V2> transformer;

	/** The <em>real</em> adapter. */
	private final BasePluggablePropertyValueModel.Adapter.Listener<V2> listener;

	/** Cached copy of model's value. */
	/* package */ volatile V1 propertyModelValue;


	// ********** constructors **********

	public TransformationPluggablePropertyValueModelAdapter(
			PropertyValueModel<? extends V1> propertyModel,
			Transformer<? super V1, ? extends V2> transformer,
			BasePluggablePropertyValueModel.Adapter.Listener<V2> listener
	) {
		super();
		if (propertyModel == null) {
			throw new NullPointerException();
		}
		this.propertyModel = propertyModel;
		if (transformer == null) {
			throw new NullPointerException();
		}
		this.transformer = transformer;
		if (listener == null) {
			throw new NullPointerException();
		}
		this.listener = listener;
	}


	// ********** BasePluggablePropertyValueModel.Adapter **********

	public V2 engageModel() {
		this.propertyModel.addPropertyChangeListener(PropertyValueModel.VALUE, this);
		this.propertyModelValue = this.propertyModel.getValue();
		return this.buildValue();
	}

	public V2 disengageModel() {
		this.propertyModel.removePropertyChangeListener(PropertyValueModel.VALUE, this);
		this.propertyModelValue = null;
		return null;
	}


	// ********** PropertyChangeListener **********

	@SuppressWarnings("unchecked")
	public void propertyChanged(PropertyChangeEvent event) {
		this.propertyModelValue = (V1) event.getNewValue();
		this.update();
	}


	// ********** misc **********

	private void update() {
		this.listener.valueChanged(this.buildValue());
	}

	private V2 buildValue() {
		return this.transformer.transform(this.propertyModelValue);
	}

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


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

	public static final class Factory<V1, V2>
		implements PluggablePropertyValueModel.Adapter.Factory<V2>
	{
		private final PropertyValueModel<? extends V1> propertyModel;
		private final Transformer<? super V1, ? extends V2> transformer;

		public Factory(PropertyValueModel<? extends V1> propertyModel, Transformer<? super V1, ? extends V2> transformer) {
			super();
			if (propertyModel == null) {
				throw new NullPointerException();
			}
			this.propertyModel = propertyModel;
			if (transformer == null) {
				throw new NullPointerException();
			}
			this.transformer = transformer;
		}

		public TransformationPluggablePropertyValueModelAdapter<V1, V2> buildAdapter(BasePluggablePropertyValueModel.Adapter.Listener<V2> listener) {
			return new TransformationPluggablePropertyValueModelAdapter<>(this.propertyModel, this.transformer, listener);
		}

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

Back to the top