Skip to main content
summaryrefslogtreecommitdiffstats
blob: 65ce3beb17d5a01c7f571f7dcad3dd70c9567b5e (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
/*******************************************************************************
 * 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.Collection;
import java.util.Collections;
import java.util.LinkedList;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.collection.CollectionTools;
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.listener.CollectionChangeListener;
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} to
 * a {@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 to a single value.
 * <p>
 * This is an adapter that can be plugged into a {@link PluggablePropertyValueModel}.
 * 
 * @param <E> the type of the adapted collection value model's elements
 * @param <V> the type of the model's derived value
 * 
 * @see PluggablePropertyValueModel
 */
public final class CollectionTransformationPluggablePropertyValueModelAdapter<E, V>
	implements PluggablePropertyValueModel.Adapter<V>, CollectionChangeListener
{
	/** The wrapped model */
	private final CollectionValueModel<? 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. */
	private final BasePluggablePropertyValueModel.Adapter.Listener<V> listener;

	/** Cached copy of model's elements. */
	private final LinkedList<E> collection;

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

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


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

	public CollectionTransformationPluggablePropertyValueModelAdapter(
			CollectionValueModel<? extends E> collectionModel,
			Transformer<? super Collection<E>, V> transformer,
			BasePluggablePropertyValueModel.Adapter.Listener<V> listener
	) {
		super();
		if (collectionModel == null) {
			throw new NullPointerException();
		}
		this.collectionModel = collectionModel;
		if (transformer == null) {
			throw new NullPointerException();
		}
		this.transformer = transformer;
		if (listener == null) {
			throw new NullPointerException();
		}
		this.listener = listener;
		this.collection = new LinkedList<>();
		this.unmodifiableCollection = Collections.unmodifiableCollection(this.collection);
	}


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

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

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


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

	@SuppressWarnings("unchecked")
	public void itemsAdded(CollectionAddEvent event) {
		CollectionTools.addAll(this.collection, (Iterable<E>) event.getItems());
		this.update();
	}

	public void itemsRemoved(CollectionRemoveEvent event) {
		CollectionTools.removeAll(this.collection, event.getItems());
		this.update();
	}

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

	@SuppressWarnings("unchecked")
	public void collectionChanged(CollectionChangeEvent event) {
		this.collection.clear();
		CollectionTools.addAll(this.collection, (Iterable<E>) event.getCollection());
		this.update();
	}


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

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

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

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


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

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

		public Factory(CollectionValueModel<? 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 CollectionTransformationPluggablePropertyValueModelAdapter<>(this.collectionModel, this.transformer, listener);
		}

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

Back to the top