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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*******************************************************************************
* Copyright (c) 2007 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.utility.internal.model.value;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import org.eclipse.jpt.utility.internal.Transformer;
import org.eclipse.jpt.utility.internal.iterators.CompositeIterator;
import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;
import org.eclipse.jpt.utility.internal.model.event.CollectionChangeEvent;
import org.eclipse.jpt.utility.internal.model.listener.CollectionChangeListener;
/**
* A <code>CompositeCollectionValueModel</code> wraps another
* <code>CollectionValueModel</code> and uses a <code>Transformer</code>
* to convert each item in the wrapped collection to yet another
* <code>CollectionValueModel</code>. This composite collection contains
* the combined items from all these component collections.
*
* NB: The wrapped collection must be an "identity set" that does not
* contain the same item twice or this class will throw an exception.
*
* Terminology:
* - sources - the items in the wrapped collection value model; these
* are converted into components by the transformer
* - components - the component collection value models that are combined
* by this composite collection value model
*/
public class CompositeCollectionValueModel
extends CollectionValueModelWrapper
{
/**
* This is the (optional) user-supplied object that transforms
* the items in the wrapped collection to collection value models.
*/
private Transformer transformer;
/**
* Cache of the component collection value models that
* were generated by the transformer; keyed by the item
* in the wrapped collection that was passed to the transformer.
*/
private IdentityHashMap components;
/**
* Cache of the collections corresponding to the component
* collection value models above; keyed by the component
* collection value models.
*/
private IdentityHashMap collections;
/** Listener that listens to all the component collection value models. */
private CollectionChangeListener componentListener;
/** Cache the size of the composite collection. */
private int size;
// ********** constructors **********
/**
* Construct a collection value model with the specified wrapped
* collection value model. Use this constructor if you want to override the
* <code>transform(Object)</code> method instead of building a
* <code>Transformer</code>.
*/
public CompositeCollectionValueModel(CollectionValueModel collectionHolder) {
this(collectionHolder, Transformer.Disabled.instance());
}
/**
* Construct a collection value model with the specified wrapped
* collection value model and transformer.
*/
public CompositeCollectionValueModel(CollectionValueModel collectionHolder, Transformer transformer) {
super(collectionHolder);
this.transformer = transformer;
}
/**
* Construct a collection value model with the specified wrapped
* list value model. Use this constructor if you want to override the
* <code>transform(Object)</code> method instead of building a
* <code>Transformer</code>.
*/
public CompositeCollectionValueModel(ListValueModel listHolder) {
this(new ListCollectionValueModelAdapter(listHolder));
}
/**
* Construct a collection value model with the specified wrapped
* list value model and transformer.
*/
public CompositeCollectionValueModel(ListValueModel listHolder, Transformer transformer) {
this(new ListCollectionValueModelAdapter(listHolder), transformer);
}
// ********** initialization **********
@Override
protected void initialize() {
super.initialize();
this.components = new IdentityHashMap();
this.collections = new IdentityHashMap();
this.componentListener = this.buildComponentListener();
this.size = 0;
}
protected CollectionChangeListener buildComponentListener() {
return new CollectionChangeListener() {
public void itemsAdded(CollectionChangeEvent e) {
CompositeCollectionValueModel.this.componentItemsAdded(e);
}
public void itemsRemoved(CollectionChangeEvent e) {
CompositeCollectionValueModel.this.componentItemsRemoved(e);
}
public void collectionCleared(CollectionChangeEvent e) {
CompositeCollectionValueModel.this.componentCollectionCleared(e);
}
public void collectionChanged(CollectionChangeEvent e) {
CompositeCollectionValueModel.this.componentCollectionChanged(e);
}
@Override
public String toString() {
return "component listener";
}
};
}
// ********** ValueModel implementation **********
public Object value() {
return new CompositeIterator(this.buildCollectionsIterators());
}
protected Iterator buildCollectionsIterators() {
return new TransformationIterator(this.collections.values().iterator()) {
protected Object transform(Object next) {
return ((ArrayList) next).iterator();
}
};
}
// ********** CollectionValueModel implementation **********
public int size() {
return this.size;
}
// ********** CollectionValueModelWrapper overrides/implementation **********
@Override
protected void engageModel() {
super.engageModel();
// synch our cache *after* we start listening to the wrapped collection,
// since its value might change when a listener is added;
// the following will trigger the firing of a number of unnecessary events
// (since we don't have any listeners yet),
// but it reduces the amount of duplicate code
this.addComponentSources((Iterator) this.collectionHolder.value());
}
@Override
protected void disengageModel() {
super.disengageModel();
// stop listening to the components...
for (Iterator stream = this.components.values().iterator(); stream.hasNext(); ) {
((CollectionValueModel) stream.next()).removeCollectionChangeListener(ValueModel.VALUE, this.componentListener);
}
// ...and clear the cache
this.components.clear();
this.collections.clear();
this.size = 0;
}
/**
* Some component sources were added;
* add their corresponding items to our cache.
*/
@Override
protected void itemsAdded(CollectionChangeEvent e) {
this.addComponentSources(e.items());
}
/**
* Transform the specified sources to collection value models
* and add their items to our cache.
*/
protected void addComponentSources(Iterator sources) {
while (sources.hasNext()) {
this.addComponentSource(sources.next());
}
}
/**
* Transform the specified source to a collection value model
* and add its items to our cache.
*/
protected void addComponentSource(Object source) {
CollectionValueModel component = this.transform(source);
if (this.components.put(source, component) != null) {
throw new IllegalStateException("duplicate component: " + source);
}
component.addCollectionChangeListener(ValueModel.VALUE, this.componentListener);
ArrayList componentCollection = new ArrayList(component.size());
if (this.collections.put(component, componentCollection) != null) {
throw new IllegalStateException("duplicate collection: " + source);
}
this.addComponentItems(component, componentCollection);
}
/**
* Some component sources were removed;
* remove their corresponding items from our cache.
*/
@Override
protected void itemsRemoved(CollectionChangeEvent e) {
this.removeComponentSources(e.items());
}
/**
* Remove the items corresponding to the specified sources
* from our cache.
*/
protected void removeComponentSources(Iterator sources) {
while (sources.hasNext()) {
this.removeComponentSource(sources.next());
}
}
/**
* Remove the items corresponding to the specified source
* from our cache.
*/
protected void removeComponentSource(Object source) {
CollectionValueModel component = (CollectionValueModel) this.components.remove(source);
if (component == null) {
throw new IllegalStateException("missing component: " + source);
}
component.removeCollectionChangeListener(VALUE, this.componentListener);
ArrayList componentCollection = (ArrayList) this.collections.remove(component);
if (componentCollection == null) {
throw new IllegalStateException("missing collection: " + source);
}
this.clearComponentItems(componentCollection);
}
/**
* The component sources cleared;
* clear our cache.
*/
@Override
protected void collectionCleared(CollectionChangeEvent e) {
// copy the keys so we don't eat our own tail
this.removeComponentSources(new ArrayList(this.components.keySet()).iterator());
}
/**
* The component sources changed;
* rebuild our cache.
*/
@Override
protected void collectionChanged(CollectionChangeEvent e) {
// copy the keys so we don't eat our own tail
this.removeComponentSources(new ArrayList(this.components.keySet()).iterator());
this.addComponentSources((Iterator) this.collectionHolder.value());
}
// ********** queries **********
/**
* Return the cached collection for the specified component model.
* Cast to ArrayList so we can use ArrayList-specific methods
* (e.g. #clone() and #ensureCapacity()).
*/
protected ArrayList getComponentCollection(CollectionValueModel collectionValueModel) {
return (ArrayList) this.collections.get(collectionValueModel);
}
// ********** behavior **********
/**
* Transform the specified object into a collection value model.
* <p>
* This method can be overridden by a subclass as an
* alternative to building a <code>Transformer</code>.
*/
protected CollectionValueModel transform(Object value) {
return (CollectionValueModel) this.transformer.transform(value);
}
/**
* One of the component collections had items added;
* synchronize our caches.
*/
protected void componentItemsAdded(CollectionChangeEvent e) {
this.addComponentItems(e.items(), e.itemsSize(), (CollectionValueModel) e.getSource());
}
/**
* Update our cache.
*/
protected void addComponentItems(Iterator items, int itemsSize, CollectionValueModel cvm) {
this.addComponentItems(items, itemsSize, this.getComponentCollection(cvm));
}
/**
* Update our cache.
*/
protected void addComponentItems(CollectionValueModel itemsHolder, ArrayList componentCollection) {
this.addComponentItems((Iterator) itemsHolder.value(), itemsHolder.size(), componentCollection);
}
/**
* Update our size and collection cache.
*/
protected void addComponentItems(Iterator items, int itemsSize, ArrayList componentCollection) {
this.size += itemsSize;
componentCollection.ensureCapacity(componentCollection.size() + itemsSize);
this.addItemsToCollection(items, componentCollection, VALUE);
}
/**
* One of the component collections had items removed;
* synchronize our caches.
*/
protected void componentItemsRemoved(CollectionChangeEvent e) {
this.removeComponentItems(e.items(), e.itemsSize(), (CollectionValueModel) e.getSource());
}
/**
* Update our size and collection cache.
*/
protected void removeComponentItems(Iterator items, int itemsSize, CollectionValueModel cvm) {
this.removeComponentItems(items, itemsSize, this.getComponentCollection(cvm));
}
/**
* Update our size and collection cache.
*/
protected void clearComponentItems(ArrayList items) {
// clone the collection so we don't eat our own tail
this.removeComponentItems(((ArrayList) items.clone()).iterator(), items.size(), items);
}
/**
* Update our size and collection cache.
*/
protected void removeComponentItems(Iterator items, int itemsSize, ArrayList componentCollection) {
this.size -= itemsSize;
this.removeItemsFromCollection(items, componentCollection, VALUE);
}
/**
* One of the component collections was cleared;
* synchronize our caches by clearing out the appropriate
* collection.
*/
protected void componentCollectionCleared(CollectionChangeEvent e) {
CollectionValueModel component = (CollectionValueModel) e.getSource();
ArrayList items = this.getComponentCollection(component);
this.clearComponentItems(items);
}
/**
* One of the component collections changed;
* synchronize our caches by clearing out the appropriate
* collection and then rebuilding it.
*/
protected void componentCollectionChanged(CollectionChangeEvent e) {
CollectionValueModel component = (CollectionValueModel) e.getSource();
ArrayList items = this.getComponentCollection(component);
this.clearComponentItems(items);
this.addComponentItems(component, items);
}
}
|