diff options
Diffstat (limited to 'jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeCollectionValueModel.java')
-rw-r--r-- | jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeCollectionValueModel.java | 130 |
1 files changed, 72 insertions, 58 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeCollectionValueModel.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeCollectionValueModel.java index 2fd599019e..29a0f0feff 100644 --- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeCollectionValueModel.java +++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CompositeCollectionValueModel.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Oracle. All rights reserved. + * Copyright (c) 2007, 2008 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. @@ -35,28 +35,29 @@ import org.eclipse.jpt.utility.internal.model.listener.CollectionChangeListener; * - components - the component collection value models that are combined * by this composite collection value model */ -public class CompositeCollectionValueModel - extends CollectionValueModelWrapper +public class CompositeCollectionValueModel<T, E> + extends CollectionValueModelWrapper<T> + implements CollectionValueModel<E> { /** * This is the (optional) user-supplied object that transforms * the items in the wrapped collection to collection value models. */ - private final Transformer transformer; + private final Transformer<T, CollectionValueModel<E>> 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 final IdentityHashMap components; + private final IdentityHashMap<T, CollectionValueModel<E>> components; /** * Cache of the collections corresponding to the component * collection value models above; keyed by the component * collection value models. */ - private final IdentityHashMap collections; + private final IdentityHashMap<CollectionValueModel<E>, ArrayList<E>> collections; /** Listener that listens to all the component collection value models. */ private final CollectionChangeListener componentListener; @@ -73,19 +74,19 @@ public class CompositeCollectionValueModel * <code>transform(Object)</code> method instead of building a * <code>Transformer</code>. */ - public CompositeCollectionValueModel(CollectionValueModel collectionHolder) { - this(collectionHolder, Transformer.Disabled.instance()); + public CompositeCollectionValueModel(CollectionValueModel<? extends T> collectionHolder) { + this(collectionHolder, Transformer.Disabled.<T, CollectionValueModel<E>>instance()); } /** * Construct a collection value model with the specified wrapped * collection value model and transformer. */ - public CompositeCollectionValueModel(CollectionValueModel collectionHolder, Transformer transformer) { + public CompositeCollectionValueModel(CollectionValueModel<? extends T> collectionHolder, Transformer<T, CollectionValueModel<E>> transformer) { super(collectionHolder); this.transformer = transformer; - this.components = new IdentityHashMap(); - this.collections = new IdentityHashMap(); + this.components = new IdentityHashMap<T, CollectionValueModel<E>>(); + this.collections = new IdentityHashMap<CollectionValueModel<E>, ArrayList<E>>(); this.componentListener = this.buildComponentListener(); this.size = 0; } @@ -96,16 +97,16 @@ public class CompositeCollectionValueModel * <code>transform(Object)</code> method instead of building a * <code>Transformer</code>. */ - public CompositeCollectionValueModel(ListValueModel listHolder) { - this(new ListCollectionValueModelAdapter(listHolder)); + public CompositeCollectionValueModel(ListValueModel<? extends T> listHolder) { + this(new ListCollectionValueModelAdapter<T>(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); + public CompositeCollectionValueModel(ListValueModel<? extends T> listHolder, Transformer<T, CollectionValueModel<E>> transformer) { + this(new ListCollectionValueModelAdapter<T>(listHolder), transformer); } @@ -135,14 +136,15 @@ public class CompositeCollectionValueModel // ********** CollectionValueModel implementation ********** - public Iterator iterator() { - return new CompositeIterator(this.buildCollectionsIterators()); + public Iterator<E> iterator() { + return new CompositeIterator<E>(this.buildCollectionsIterators()); } - protected Iterator buildCollectionsIterators() { - return new TransformationIterator(this.collections.values().iterator()) { - protected Object transform(Object next) { - return ((ArrayList) next).iterator(); + protected Iterator<Iterator<E>> buildCollectionsIterators() { + return new TransformationIterator<ArrayList<E>, Iterator<E>>(this.collections.values().iterator()) { + @Override + protected Iterator<E> transform(ArrayList<E> next) { + return next.iterator(); } }; } @@ -162,15 +164,15 @@ public class CompositeCollectionValueModel // 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.iterator()); + this.addComponentSources(this.collectionHolder.iterator()); } @Override protected void disengageModel() { super.disengageModel(); // stop listening to the components... - for (Iterator stream = this.components.values().iterator(); stream.hasNext(); ) { - ((CollectionValueModel) stream.next()).removeCollectionChangeListener(CollectionValueModel.VALUES, this.componentListener); + for (CollectionValueModel<E> cvm : this.components.values()) { + cvm.removeCollectionChangeListener(CollectionValueModel.VALUES, this.componentListener); } // ...and clear the cache this.components.clear(); @@ -184,14 +186,14 @@ public class CompositeCollectionValueModel */ @Override protected void itemsAdded(CollectionChangeEvent e) { - this.addComponentSources(e.items()); + this.addComponentSources(this.items(e)); } /** * Transform the specified sources to collection value models * and add their items to our cache. */ - protected void addComponentSources(Iterator sources) { + protected void addComponentSources(Iterator<? extends T> sources) { while (sources.hasNext()) { this.addComponentSource(sources.next()); } @@ -201,13 +203,13 @@ public class CompositeCollectionValueModel * 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); + protected void addComponentSource(T source) { + CollectionValueModel<E> component = this.transform(source); if (this.components.put(source, component) != null) { throw new IllegalStateException("duplicate component: " + source); } component.addCollectionChangeListener(CollectionValueModel.VALUES, this.componentListener); - ArrayList componentCollection = new ArrayList(component.size()); + ArrayList<E> componentCollection = new ArrayList<E>(component.size()); if (this.collections.put(component, componentCollection) != null) { throw new IllegalStateException("duplicate collection: " + source); } @@ -220,14 +222,14 @@ public class CompositeCollectionValueModel */ @Override protected void itemsRemoved(CollectionChangeEvent e) { - this.removeComponentSources(e.items()); + this.removeComponentSources(this.items(e)); } /** * Remove the items corresponding to the specified sources * from our cache. */ - protected void removeComponentSources(Iterator sources) { + protected void removeComponentSources(Iterator<T> sources) { while (sources.hasNext()) { this.removeComponentSource(sources.next()); } @@ -237,13 +239,13 @@ public class CompositeCollectionValueModel * Remove the items corresponding to the specified source * from our cache. */ - protected void removeComponentSource(Object source) { - CollectionValueModel component = (CollectionValueModel) this.components.remove(source); + protected void removeComponentSource(T source) { + CollectionValueModel<E> component = this.components.remove(source); if (component == null) { throw new IllegalStateException("missing component: " + source); } component.removeCollectionChangeListener(CollectionValueModel.VALUES, this.componentListener); - ArrayList componentCollection = (ArrayList) this.collections.remove(component); + ArrayList<E> componentCollection = this.collections.remove(component); if (componentCollection == null) { throw new IllegalStateException("missing collection: " + source); } @@ -257,7 +259,7 @@ public class CompositeCollectionValueModel @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()); + this.removeComponentSources(new ArrayList<T>(this.components.keySet()).iterator()); } /** @@ -267,8 +269,8 @@ public class CompositeCollectionValueModel @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.iterator()); + this.removeComponentSources(new ArrayList<T>(this.components.keySet()).iterator()); + this.addComponentSources(this.collectionHolder.iterator()); } @@ -279,8 +281,8 @@ public class CompositeCollectionValueModel * 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); + protected ArrayList<E> componentCollection(CollectionValueModel<E> collectionValueModel) { + return this.collections.get(collectionValueModel); } @@ -292,8 +294,8 @@ public class CompositeCollectionValueModel * 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); + protected CollectionValueModel<E> transform(T value) { + return this.transformer.transform(value); } /** @@ -301,27 +303,27 @@ public class CompositeCollectionValueModel * synchronize our caches. */ protected void componentItemsAdded(CollectionChangeEvent e) { - this.addComponentItems(e.items(), e.itemsSize(), (CollectionValueModel) e.getSource()); + this.addComponentItems(this.componentItems(e), e.itemsSize(), this.componentCVM(e)); } /** * Update our cache. */ - protected void addComponentItems(Iterator items, int itemsSize, CollectionValueModel cvm) { - this.addComponentItems(items, itemsSize, this.getComponentCollection(cvm)); + protected void addComponentItems(Iterator<E> items, int itemsSize, CollectionValueModel<E> cvm) { + this.addComponentItems(items, itemsSize, this.componentCollection(cvm)); } /** * Update our cache. */ - protected void addComponentItems(CollectionValueModel itemsHolder, ArrayList componentCollection) { - this.addComponentItems((Iterator) itemsHolder.iterator(), itemsHolder.size(), componentCollection); + protected void addComponentItems(CollectionValueModel<E> itemsHolder, ArrayList<E> componentCollection) { + this.addComponentItems(itemsHolder.iterator(), itemsHolder.size(), componentCollection); } /** * Update our size and collection cache. */ - protected void addComponentItems(Iterator items, int itemsSize, ArrayList componentCollection) { + protected void addComponentItems(Iterator<E> items, int itemsSize, ArrayList<E> componentCollection) { this.size += itemsSize; componentCollection.ensureCapacity(componentCollection.size() + itemsSize); this.addItemsToCollection(items, componentCollection, CollectionValueModel.VALUES); @@ -332,28 +334,29 @@ public class CompositeCollectionValueModel * synchronize our caches. */ protected void componentItemsRemoved(CollectionChangeEvent e) { - this.removeComponentItems(e.items(), e.itemsSize(), (CollectionValueModel) e.getSource()); + this.removeComponentItems(this.componentItems(e), e.itemsSize(), this.componentCVM(e)); } /** * Update our size and collection cache. */ - protected void removeComponentItems(Iterator items, int itemsSize, CollectionValueModel cvm) { - this.removeComponentItems(items, itemsSize, this.getComponentCollection(cvm)); + protected void removeComponentItems(Iterator<E> items, int itemsSize, CollectionValueModel<E> cvm) { + this.removeComponentItems(items, itemsSize, this.componentCollection(cvm)); } /** * Update our size and collection cache. */ - protected void clearComponentItems(ArrayList items) { + protected void clearComponentItems(ArrayList<E> items) { // clone the collection so we don't eat our own tail - this.removeComponentItems(((ArrayList) items.clone()).iterator(), items.size(), items); + @SuppressWarnings("unchecked") ArrayList<E> clone = (ArrayList<E>) items.clone(); + this.removeComponentItems(clone.iterator(), items.size(), items); } /** * Update our size and collection cache. */ - protected void removeComponentItems(Iterator items, int itemsSize, ArrayList componentCollection) { + protected void removeComponentItems(Iterator<E> items, int itemsSize, ArrayList<E> componentCollection) { this.size -= itemsSize; this.removeItemsFromCollection(items, componentCollection, CollectionValueModel.VALUES); } @@ -364,8 +367,7 @@ public class CompositeCollectionValueModel * collection. */ protected void componentCollectionCleared(CollectionChangeEvent e) { - CollectionValueModel component = (CollectionValueModel) e.getSource(); - ArrayList items = this.getComponentCollection(component); + ArrayList<E> items = this.componentCollection(this.componentCVM(e)); this.clearComponentItems(items); } @@ -375,10 +377,22 @@ public class CompositeCollectionValueModel * collection and then rebuilding it. */ protected void componentCollectionChanged(CollectionChangeEvent e) { - CollectionValueModel component = (CollectionValueModel) e.getSource(); - ArrayList items = this.getComponentCollection(component); + CollectionValueModel<E> componentCVM = this.componentCVM(e); + ArrayList<E> items = this.componentCollection(componentCVM); this.clearComponentItems(items); - this.addComponentItems(component, items); + this.addComponentItems(componentCVM, items); + } + + // minimize suppressed warnings + @SuppressWarnings("unchecked") + protected Iterator<E> componentItems(CollectionChangeEvent e) { + return (Iterator<E>) e.items(); + } + + // minimize suppressed warnings + @SuppressWarnings("unchecked") + protected CollectionValueModel<E> componentCVM(CollectionChangeEvent e) { + return (CollectionValueModel<E>) e.getSource(); } } |