Skip to main content
summaryrefslogtreecommitdiffstats
blob: 80851905f6387cd598a716efe9f501ce04052c60 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.collection.ListTools;
import org.eclipse.jpt.common.utility.internal.model.value.PluggablePropertyValueModel.Adapter;
import org.eclipse.jpt.common.utility.model.event.CollectionAddEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionChangeEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionClearEvent;
import org.eclipse.jpt.common.utility.model.event.CollectionRemoveEvent;
import org.eclipse.jpt.common.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.common.utility.model.listener.CollectionChangeListener;
import org.eclipse.jpt.common.utility.model.listener.PropertyChangeAdapter;
import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.common.utility.model.value.CollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.transformer.Transformer;

/**
 * Adapt a {@link CollectionValueModel collection value model} holding
 * {@link PropertyValueModel property value models}
 * to a single {@link PropertyValueModel property value model}, sorta.
 * <p>
 * This adapter is constructed with a {@link CollectionValueModel
 * collection value model} and a {@link Transformer transformer} that can
 * transform the collection's property value models' values to a single value.
 * <p>
 * This is an adapter that can be plugged into a {@link PluggablePropertyValueModel}.
 * <p>
 * <strong>NB:</strong> The wrapped collection value model must not contain any
 * <code>null</code>s or duplicate property value models.
 * 
 * @param <E> the type of the adapted collection value model's
 *     property value models' values
 * @param <V> the type of the model's derived value
 * 
 * @see PluggablePropertyValueModel
 * @see CollectionTransformationPluggablePropertyValueModelAdapter
 */
public final class CompositePropertyValueModelAdapter<E, V>
	implements PluggablePropertyValueModel.Adapter<V>, CollectionChangeListener
{
	/** The wrapped model */
	private final CollectionValueModel<? extends PropertyValueModel<? extends E>> collectionModel;

	/** Transformer that converts the wrapped model's value to this model's value. */
	private final Transformer<? super Collection<E>, V> transformer;

	/**
	 * The <em>real</em> adapter, passed to us as a listener.
	 */
	private final BasePluggablePropertyValueModel.Adapter.Listener<V> listener;

	/**
	 * Listen to every property value model in the collection value model.
	 * If one changes, we need to re-calculate {@link #value}
	 * and notify @{link #listener}.
	 */
	private final PropertyChangeListener componentListener;

	/**
	 * Cached copy of {@link Factory#collectionModel}'s elements
	 * and their values.
	 */
	private final IdentityHashMap<PropertyValueModel<? extends E>, E> values;

	/**
	 * Protects {@link #values} from {@link Factory#transformer}.
	 */
	private final Collection<E> unmodifiableValues;

	/**
	 * The derived value.
	 */
	private volatile V value;


	// ********** constructor **********

	public CompositePropertyValueModelAdapter(Factory<E, V> factory, BasePluggablePropertyValueModel.Adapter.Listener<V> listener) {
		super();
		if (factory == null) {
			throw new NullPointerException();
		}
		this.collectionModel = factory.collectionModel;
		this.transformer = factory.transformer;
		if (listener == null) {
			throw new NullPointerException();
		}
		this.listener = listener;
		this.componentListener = this.buildComponentListener();
		this.values = new IdentityHashMap<>();
		this.unmodifiableValues = Collections.unmodifiableCollection(this.values.values());
	}


	// ********** PluggablePropertyValueModel.Adapter **********

	public V engageModel() {
		this.collectionModel.addCollectionChangeListener(CollectionValueModel.VALUES, this);
		this.addComponentPVMs(this.collectionModel);
		return this.value = this.buildValue();
	}

	public V disengageModel() {
		this.collectionModel.removeCollectionChangeListener(CollectionValueModel.VALUES, this);
		this.removeCachedPVMs();
		return this.value = null;
	}


	// ********** CollectionChangeListener **********

	@SuppressWarnings("unchecked")
	public void itemsAdded(CollectionAddEvent event) {
		this.addComponentPVMs((Iterable<? extends PropertyValueModel<? extends E>>) event.getItems());
		this.update();
	}

	@SuppressWarnings("unchecked")
	public void itemsRemoved(CollectionRemoveEvent event) {
		this.removeComponentPVMs((Iterable<? extends PropertyValueModel<? extends E>>) event.getItems());
		this.update();
	}

	public void collectionCleared(CollectionClearEvent event) {
		this.removeCachedPVMs();
		this.update();
	}

	@SuppressWarnings("unchecked")
	public void collectionChanged(CollectionChangeEvent event) {
		this.removeCachedPVMs();
		this.addComponentPVMs((Iterable<? extends PropertyValueModel<? extends E>>) event.getCollection());
		this.update();
	}


	// ********** add/remove component PVMs **********

	private void addComponentPVMs(Iterable<? extends PropertyValueModel<? extends E>> pvms) {
		for (PropertyValueModel<? extends E> pvm : pvms) {
			this.addComponentPVM(pvm);
		}
	}

	private void addComponentPVM(PropertyValueModel<? extends E> pvm) {
		if (pvm == null) {
			throw new NullPointerException();
		}
		if (this.values.containsKey(pvm)) {
			throw new IllegalStateException("duplicate component: " + pvm); //$NON-NLS-1$
		}
		pvm.addPropertyChangeListener(PropertyValueModel.VALUE, this.componentListener);
		this.values.put(pvm, pvm.getValue());
	}

	private void removeCachedPVMs() {
		// copy the list so we don't eat our own tail
		ArrayList<PropertyValueModel<? extends E>> copy = ListTools.arrayList(this.values.keySet());
		this.removeComponentPVMs(copy);
	}

	private void removeComponentPVMs(Iterable<? extends PropertyValueModel<? extends E>> pvms) {
		for (PropertyValueModel<? extends E> pvm : pvms) {
			this.removeComponentPVM(pvm);
		}
	}

	private void removeComponentPVM(PropertyValueModel<? extends E> pvm) {
		if ( ! this.values.containsKey(pvm)) {
			throw new IllegalStateException("missing component: " + pvm); //$NON-NLS-1$
		}
		this.values.remove(pvm);
		pvm.removePropertyChangeListener(PropertyValueModel.VALUE, this.componentListener);
	}


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

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

	private V buildValue() {
		return this.transformer.transform(this.unmodifiableValues);
	}

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


	// ********** component listener **********

	private PropertyChangeListener buildComponentListener() {
		return new ComponentListener();
	}

	/* CU private */ class ComponentListener
		extends PropertyChangeAdapter
	{
		@Override
		public void propertyChanged(PropertyChangeEvent event) {
			CompositePropertyValueModelAdapter.this.componentChanged(event);
		}
	}

	/* CU private */ void componentChanged(PropertyChangeEvent event) {
		@SuppressWarnings("unchecked")
		PropertyValueModel<? extends E> source = (PropertyValueModel<? extends E>) event.getSource();
		if ( ! this.values.containsKey(source)) {
			throw new IllegalStateException("invalid component: " + source); //$NON-NLS-1$
		}
		@SuppressWarnings("unchecked")
		E newValue = (E) event.getNewValue();
		this.values.put(source, newValue);
		this.update();
	}


	// ********** PluggablePropertyValueModel.Adapter.Factory **********

	public static class Factory<E, V>
		implements PluggablePropertyValueModel.Adapter.Factory<V>
	{
		/* CU private */ final CollectionValueModel<? extends PropertyValueModel<? extends E>> collectionModel;
		/* CU private */ final Transformer<? super Collection<E>, V> transformer;

		public Factory(CollectionValueModel<? extends PropertyValueModel<? extends E>> collectionModel, Transformer<? super Collection<E>, V> transformer) {
			super();
			if (collectionModel == null) {
				throw new NullPointerException();
			}
			this.collectionModel = collectionModel;
			if (transformer == null) {
				throw new NullPointerException();
			}
			this.transformer = transformer;
		}

		public Adapter<V> buildAdapter(BasePluggablePropertyValueModel.Adapter.Listener<V> listener) {
			return new CompositePropertyValueModelAdapter<>(this, listener);
		}

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

Back to the top