Skip to main content
summaryrefslogtreecommitdiffstats
blob: aca8325ca83a3769cdb3949d743bc84fa2a2ffb6 (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
/*******************************************************************************
 * 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.Collections;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.collection.CollectionTools;
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.ListAddEvent;
import org.eclipse.jpt.common.utility.model.event.ListChangeEvent;
import org.eclipse.jpt.common.utility.model.event.ListClearEvent;
import org.eclipse.jpt.common.utility.model.event.ListMoveEvent;
import org.eclipse.jpt.common.utility.model.event.ListRemoveEvent;
import org.eclipse.jpt.common.utility.model.event.ListReplaceEvent;
import org.eclipse.jpt.common.utility.model.listener.ListChangeListener;
import org.eclipse.jpt.common.utility.model.value.ListValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.transformer.Transformer;

/**
 * Adapt a {@link ListValueModel list value model} to
 * a {@link PropertyValueModel property value model}, sorta.
 * <p>
 * This adapter is constructed with a {@link ListValueModel
 * list value model} and a {@link Transformer transformer} that can
 * transform the list to a single value.
 * <p>
 * This is an adapter that can be plugged into a {@link PluggablePropertyValueModel}.
 * 
 * @param <E> the type of the adapted list value model's elements
 * @param <V> the type of the model's derived value
 * 
 * @see PluggablePropertyValueModel
 */
public final class ListTransformationPluggablePropertyValueModelAdapter<E, V>
	implements PluggablePropertyValueModel.Adapter<V>, ListChangeListener
{
	/** The wrapped model */
	private final ListValueModel<? extends E> listModel;

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

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

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

	/** Protects {@link #list} from {@link Factory#transformer}. */
	private final List<E> unmodifiableList;

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


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

	public ListTransformationPluggablePropertyValueModelAdapter(Factory<E, V> factory, BasePluggablePropertyValueModel.Adapter.Listener<V> listener) {
		super();
		if (factory == null) {
			throw new NullPointerException();
		}
		this.listModel = factory.listModel;
		this.transformer = factory.transformer;
		if (listener == null) {
			throw new NullPointerException();
		}
		this.listener = listener;
		this.list = new ArrayList<>();
		this.unmodifiableList = Collections.unmodifiableList(this.list);
	}


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

	public V engageModel() {
		this.listModel.addListChangeListener(ListValueModel.LIST_VALUES, this);
		ListTools.addAll(this.list, 0, this.listModel);
		return this.value = this.buildValue();
	}

	public V disengageModel() {
		this.listModel.removeListChangeListener(ListValueModel.LIST_VALUES, this);
		this.list.clear();
		return this.value = null;
	}


	// ********** ListChangeListener **********

	@SuppressWarnings("unchecked")
	public void itemsAdded(ListAddEvent event) {
		ListTools.addAll(this.list, event.getIndex(), (Iterable<E>) event.getItems(), event.getItemsSize());
		this.update();
	}

	public void itemsRemoved(ListRemoveEvent event) {
		ListTools.removeElementsAtIndex(this.list, event.getIndex(), event.getItemsSize());
		this.update();
	}

	public void itemsReplaced(ListReplaceEvent event) {
		@SuppressWarnings("unchecked")
		Iterable<E> newItems = (Iterable<E>) event.getNewItems();
		Iterator<E> stream = newItems.iterator();
		int last = event.getIndex() + event.getItemsSize();
		for (int i = event.getIndex(); i < last; i++) {
			this.list.set(i, stream.next());
		}
		this.update();
	}

	public void itemsMoved(ListMoveEvent event) {
		ListTools.move(this.list, event.getTargetIndex(), event.getSourceIndex(), event.getLength());
		this.update();
	}

	public void listCleared(ListClearEvent event) {
		this.list.clear();
		this.update();
	}

	@SuppressWarnings("unchecked")
	public void listChanged(ListChangeEvent event) {
		this.list.clear();
		CollectionTools.addAll(this.list, (Iterable<E>) event.getList());
		this.update();
	}


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

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

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

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


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

	public static class Factory<E, V>
		implements PluggablePropertyValueModel.Adapter.Factory<V>
	{
		/* CU private */ final ListValueModel<? extends E> listModel;
		/* CU private */ final Transformer<? super List<E>, V> transformer;

		public Factory(ListValueModel<? extends E> listModel, Transformer<? super List<E>, V> transformer) {
			super();
			if (listModel == null) {
				throw new NullPointerException();
			}
			this.listModel = listModel;
			if (transformer == null) {
				throw new NullPointerException();
			}
			this.transformer = transformer;
		}

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

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

Back to the top