Author: scolebourne Date: Sun Nov 5 16:22:53 2006 New Revision: 471580 URL: http://svn.apache.org/viewvc?view=rev&rev=471580 Log: Generify CompositeCollection
Modified: jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/collection/CompositeCollection.java jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/collection/TestCompositeCollection.java jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/set/TestCompositeSet.java Modified: jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/collection/CompositeCollection.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/collection/CompositeCollection.java?view=diff&rev=471580&r1=471579&r2=471580 ============================================================================== --- jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/collection/CompositeCollection.java (original) +++ jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/collection/CompositeCollection.java Sun Nov 5 16:22:53 2006 @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; +import java.util.List; import org.apache.commons.collections.iterators.EmptyIterator; import org.apache.commons.collections.iterators.IteratorChain; @@ -33,6 +34,7 @@ * Add and remove operations require the use of a pluggable strategy. If no * strategy is provided then add and remove are unsupported. * + * @param <E> the type of the elements in the collection * @since Commons Collections 3.0 * @version $Revision$ $Date$ * @@ -40,42 +42,61 @@ * @author Stephen Colebourne * @author Phil Steitz */ -public class CompositeCollection implements Collection { +public class CompositeCollection<E> implements Collection<E> { /** CollectionMutator to handle changes to the collection */ - protected CollectionMutator mutator; + protected CollectionMutator<E> mutator; /** Collections in the composite */ - protected Collection[] all; + protected List<Collection<E>> all = new ArrayList<Collection<E>>(); /** * Create an empty CompositeCollection. */ public CompositeCollection() { super(); - this.all = new Collection[0]; } /** - * Create a Composite Collection with only coll composited. - * - * @param coll a collection to decorate + * Create a Composite Collection with one collection. + * + * @param compositeCollection the Collection to be appended to the composite */ - public CompositeCollection(Collection coll) { - this(); - this.addComposited(coll); + public CompositeCollection(Collection<E> compositeCollection) { + super(); + addComposited(compositeCollection); } /** - * Create a CompositeCollection with colls as the initial list of - * composited collections. + * Create a Composite Collection with two collections. + * + * @param compositeCollection1 the Collection to be appended to the composite + * @param compositeCollection2 the Collection to be appended to the composite + */ + public CompositeCollection(Collection<E> compositeCollection1, Collection<E> compositeCollection2) { + super(); + addComposited(compositeCollection1, compositeCollection2); + } + + /** + * Create a Composite Collection with an array of collections. * - * @param colls an array of collections to decorate + * @param compositeCollections the collections to composite */ - public CompositeCollection(Collection[] colls) { - this(); - this.addComposited(colls); + public CompositeCollection(Collection<E>[] compositeCollections) { + super(); + addComposited(compositeCollections); } +// /** +// * Create a Composite Collection extracting the collections from an iterable. +// * +// * @param compositeCollections the collections to composite +// */ +// public CompositeCollection(Iterable<Collection<E>> compositeCollections) { +// super(); +// addComposited(compositeCollections); +// } + //----------------------------------------------------------------------- /** * Gets the size of this composite collection. @@ -86,8 +107,8 @@ */ public int size() { int size = 0; - for (int i = this.all.length - 1; i >= 0; i--) { - size += this.all[i].size(); + for (Collection<E> item : all) { + size += item.size(); } return size; } @@ -100,8 +121,8 @@ * @return true if all of the contained collections are empty */ public boolean isEmpty() { - for (int i = this.all.length - 1; i >= 0; i--) { - if (this.all[i].isEmpty() == false) { + for (Collection<E> item : all) { + if (item.isEmpty() == false) { return false; } } @@ -117,8 +138,8 @@ * @return true if obj is contained in any of the contained collections */ public boolean contains(Object obj) { - for (int i = this.all.length - 1; i >= 0; i--) { - if (this.all[i].contains(obj)) { + for (Collection<E> item : all) { + if (item.contains(obj)) { return true; } } @@ -135,13 +156,13 @@ * the order they were added, but this behavior should not be relied upon. * @see IteratorChain */ - public Iterator iterator() { - if (this.all.length == 0) { + public Iterator<E> iterator() { + if (all.isEmpty()) { return EmptyIterator.INSTANCE; } IteratorChain chain = new IteratorChain(); - for (int i = 0; i < this.all.length; ++i) { - chain.addIterator(this.all[i].iterator()); + for (Collection<E> item : all) { + chain.addIterator(item.iterator()); } return chain; } @@ -152,9 +173,9 @@ * @return an object array of all the elements in the collection */ public Object[] toArray() { - final Object[] result = new Object[this.size()]; + final Object[] result = new Object[size()]; int i = 0; - for (Iterator it = this.iterator(); it.hasNext(); i++) { + for (Iterator<E> it = iterator(); it.hasNext(); i++) { result[i] = it.next(); } return result; @@ -167,8 +188,9 @@ * @param array the array to use, populating if possible * @return an array of all the elements in the collection */ - public Object[] toArray(Object[] array) { - int size = this.size(); + @SuppressWarnings("unchecked") + public <T> T[] toArray(T[] array) { + int size = size(); Object[] result = null; if (array.length >= size) { result = array; @@ -177,15 +199,15 @@ } int offset = 0; - for (int i = 0; i < this.all.length; ++i) { - for (Iterator it = this.all[i].iterator(); it.hasNext();) { + for (Collection<E> item : all) { + for (Iterator<E> it = item.iterator(); it.hasNext();) { result[offset++] = it.next(); } } if (result.length > size) { result[size] = null; } - return result; + return (T[]) result; } /** @@ -200,12 +222,12 @@ * @throws NullPointerException if the object cannot be added because its null * @throws IllegalArgumentException if the object cannot be added */ - public boolean add(Object obj) { - if (this.mutator == null) { + public boolean add(E obj) { + if (mutator == null) { throw new UnsupportedOperationException( - "add() is not supported on CompositeCollection without a CollectionMutator strategy"); + "add() is not supported on CompositeCollection without a CollectionMutator strategy"); } - return this.mutator.add(this, this.all, obj); + return mutator.add(this, all, obj); } /** @@ -220,11 +242,11 @@ * @throws IllegalArgumentException if the object cannot be removed */ public boolean remove(Object obj) { - if (this.mutator == null) { + if (mutator == null) { throw new UnsupportedOperationException( - "remove() is not supported on CompositeCollection without a CollectionMutator strategy"); + "remove() is not supported on CompositeCollection without a CollectionMutator strategy"); } - return this.mutator.remove(this, this.all, obj); + return mutator.remove(this, all, obj); } /** @@ -236,9 +258,9 @@ * @param coll the collection to check for * @return true if all elements contained */ - public boolean containsAll(Collection coll) { - for (Iterator it = coll.iterator(); it.hasNext();) { - if (this.contains(it.next()) == false) { + public boolean containsAll(Collection<?> coll) { + for (Object item : coll) { + if (contains(item) == false) { return false; } } @@ -257,12 +279,12 @@ * @throws NullPointerException if the object cannot be added because its null * @throws IllegalArgumentException if the object cannot be added */ - public boolean addAll(Collection coll) { - if (this.mutator == null) { + public boolean addAll(Collection<? extends E> coll) { + if (mutator == null) { throw new UnsupportedOperationException( - "addAll() is not supported on CompositeCollection without a CollectionMutator strategy"); + "addAll() is not supported on CompositeCollection without a CollectionMutator strategy"); } - return this.mutator.addAll(this, this.all, coll); + return mutator.addAll(this, all, coll); } /** @@ -274,13 +296,13 @@ * @return true if the collection was modified * @throws UnsupportedOperationException if removeAll is unsupported */ - public boolean removeAll(Collection coll) { + public boolean removeAll(Collection<?> coll) { if (coll.size() == 0) { return false; } boolean changed = false; - for (int i = this.all.length - 1; i >= 0; i--) { - changed = (this.all[i].removeAll(coll) || changed); + for (Collection<E> item : all) { + changed |= item.removeAll(coll); } return changed; } @@ -295,10 +317,10 @@ * @return true if the collection was modified * @throws UnsupportedOperationException if retainAll is unsupported */ - public boolean retainAll(final Collection coll) { + public boolean retainAll(final Collection<?> coll) { boolean changed = false; - for (int i = this.all.length - 1; i >= 0; i--) { - changed = (this.all[i].retainAll(coll) || changed); + for (Collection<E> item : all) { + changed |= item.retainAll(coll); } return changed; } @@ -311,8 +333,8 @@ * @throws UnsupportedOperationException if clear is unsupported */ public void clear() { - for (int i = 0; i < this.all.length; ++i) { - this.all[i].clear(); + for (Collection<E> coll : all) { + coll.clear(); } } @@ -322,76 +344,86 @@ * * @param mutator the mutator to use */ - public void setMutator(CollectionMutator mutator) { + public void setMutator(CollectionMutator<E> mutator) { this.mutator = mutator; } /** * Add these Collections to the list of collections in this composite * - * @param comps Collections to be appended to the composite + * @param compositeCollection the Collection to be appended to the composite */ - public void addComposited(Collection[] comps) { - ArrayList list = new ArrayList(Arrays.asList(this.all)); - list.addAll(Arrays.asList(comps)); - all = (Collection[]) list.toArray(new Collection[list.size()]); + public void addComposited(Collection<E> compositeCollection) { + all.add(compositeCollection); } /** - * Add an additional collection to this composite. - * - * @param c the collection to add + * Add these Collections to the list of collections in this composite + * + * @param compositeCollection1 the Collection to be appended to the composite + * @param compositeCollection2 the Collection to be appended to the composite */ - public void addComposited(Collection c) { - this.addComposited(new Collection[]{c}); + public void addComposited(Collection<E> compositeCollection1, Collection<E> compositeCollection2) { + all.add(compositeCollection1); + all.add(compositeCollection2); } /** - * Add two additional collections to this composite. - * - * @param c the first collection to add - * @param d the second collection to add + * Add these Collections to the list of collections in this composite + * + * @param compositeCollections the Collections to be appended to the composite */ - public void addComposited(Collection c, Collection d) { - this.addComposited(new Collection[]{c, d}); + public void addComposited(Collection<E>[] compositeCollections) { + all.addAll(Arrays.asList(compositeCollections)); } +// /** +// * Add these Collections to the list of collections in this composite +// * +// * @param compositeCollections the Collections to be appended to the composite +// */ +// public void addComposited(Iterable<Collection<E>> compositeCollections) { +// for (Collection<E> item : compositeCollections) { +// all.add(item); +// } +// } + /** * Removes a collection from the those being decorated in this composite. * * @param coll collection to be removed */ - public void removeComposited(Collection coll) { - ArrayList list = new ArrayList(this.all.length); - list.addAll(Arrays.asList(this.all)); - list.remove(coll); - this.all = (Collection[]) list.toArray(new Collection[list.size()]); + public void removeComposited(Collection<E> coll) { + all.remove(coll); } + //----------------------------------------------------------------------- /** * Returns a new collection containing all of the elements * * @return A new ArrayList containing all of the elements in this composite. * The new collection is <i>not</i> backed by this composite. */ - public Collection toCollection() { - return new ArrayList(this); + public Collection<E> toCollection() { + return new ArrayList<E>(this); } /** * Gets the collections being decorated. * - * @return Unmodifiable collection of all collections in this composite. + * @return Unmodifiable list of all collections in this composite. */ - public Collection getCollections() { - return UnmodifiableList.decorate(Arrays.asList(this.all)); + public List<Collection<E>> getCollections() { + return UnmodifiableList.decorate(all); } //----------------------------------------------------------------------- /** * Pluggable strategy to handle changes to the composite. + * + * @param <E> the element being held in the collection */ - public interface CollectionMutator { + public interface CollectionMutator<E> { /** * Called when an object is to be added to the composite. @@ -405,7 +437,7 @@ * @throws NullPointerException if the object cannot be added because its null * @throws IllegalArgumentException if the object cannot be added */ - public boolean add(CompositeCollection composite, Collection[] collections, Object obj); + public boolean add(CompositeCollection<E> composite, List<Collection<E>> collections, E obj); /** * Called when a collection is to be added to the composite. @@ -419,7 +451,7 @@ * @throws NullPointerException if the object cannot be added because its null * @throws IllegalArgumentException if the object cannot be added */ - public boolean addAll(CompositeCollection composite, Collection[] collections, Collection coll); + public boolean addAll(CompositeCollection<E> composite, List<Collection<E>> collections, Collection<? extends E> coll); /** * Called when an object is to be removed to the composite. @@ -433,7 +465,7 @@ * @throws NullPointerException if the object cannot be removed because its null * @throws IllegalArgumentException if the object cannot be removed */ - public boolean remove(CompositeCollection composite, Collection[] collections, Object obj); + public boolean remove(CompositeCollection<E> composite, List<Collection<E>> collections, Object obj); } Modified: jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/collection/TestCompositeCollection.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/collection/TestCompositeCollection.java?view=diff&rev=471580&r1=471579&r2=471580 ============================================================================== --- jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/collection/TestCompositeCollection.java (original) +++ jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/collection/TestCompositeCollection.java Sun Nov 5 16:22:53 2006 @@ -16,10 +16,12 @@ */ package org.apache.commons.collections.collection; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import junit.framework.Test; import junit.framework.TestSuite; @@ -34,7 +36,7 @@ * @author Brian McCallister * @author Phil Steitz */ -public class TestCompositeCollection extends AbstractTestCollection { +public class TestCompositeCollection extends AbstractTestCollection<String> { public TestCompositeCollection(String name) { super(name); @@ -64,26 +66,26 @@ /** * Empty collection is empty composite */ - public Collection makeCollection() { - return new CompositeCollection(); + public Collection<String> makeCollection() { + return new CompositeCollection<String>(); } - public Collection makeConfirmedCollection() { - return new HashSet(); + public Collection<String> makeConfirmedCollection() { + return new HashSet<String>(); } - public Object[] getFullElements() { - return new Object[] {"1", "2", "3", "4"}; + public String[] getFullElements() { + return new String[] {"1", "2", "3", "4"}; } /** * Full collection consists of 4 collections, each with one element */ - public Collection makeFullCollection() { - CompositeCollection compositeCollection = new CompositeCollection(); - Object[] elements = getFullElements(); + public Collection<String> makeFullCollection() { + CompositeCollection<String> compositeCollection = new CompositeCollection<String>(); + String[] elements = getFullElements(); for (int i = 0; i < elements.length; i++) { - Collection summand = new HashSet(); + Collection<String> summand = new HashSet<String>(); summand.add(elements[i]); compositeCollection.addComposited(summand); } @@ -93,8 +95,8 @@ /** * Full collection should look like a collection with 4 elements */ - public Collection makeConfirmedFullCollection() { - Collection collection = new HashSet(); + public Collection<String> makeConfirmedFullCollection() { + Collection<String> collection = new HashSet<String>(); collection.addAll(Arrays.asList(getFullElements())); return collection; } @@ -116,39 +118,39 @@ //-------------------------------------------------------------------------- - protected CompositeCollection c; - protected Collection one; - protected Collection two; + protected CompositeCollection<String> c; + protected Collection<String> one; + protected Collection<String> two; protected void setUpTest() { - c = new CompositeCollection(); - one = new HashSet(); - two = new HashSet(); + c = new CompositeCollection<String>(); + one = new HashSet<String>(); + two = new HashSet<String>(); } protected void setUpMutatorTest() { setUpTest(); - c.setMutator(new CompositeCollection.CollectionMutator() { - public boolean add(CompositeCollection composite, - Collection[] collections, Object obj) { - for (int i = 0; i < collections.length; i++) { - collections[i].add(obj); + c.setMutator(new CompositeCollection.CollectionMutator<String>() { + public boolean add(CompositeCollection<String> composite, + List<Collection<String>> collections, String obj) { + for (Collection<String> collection : collections) { + collection.add(obj); } return true; } - public boolean addAll(CompositeCollection composite, - Collection[] collections, Collection coll) { - for (int i = 0; i < collections.length; i++) { - collections[i].addAll(coll); + public boolean addAll(CompositeCollection<String> composite, + List<Collection<String>> collections, Collection<? extends String> coll) { + for (Collection<String> collection : collections) { + collection.addAll(coll); } return true; } - public boolean remove(CompositeCollection composite, - Collection[] collections, Object obj) { - for (int i = 0; i < collections.length; i++) { - collections[i].remove(obj); + public boolean remove(CompositeCollection<String> composite, + List<Collection<String>> collections, Object obj) { + for (Collection<String> collection : collections) { + collection.remove(obj); } return true; } @@ -157,7 +159,7 @@ public void testSize() { setUpTest(); - HashSet set = new HashSet(); + HashSet<String> set = new HashSet<String>(); set.add("a"); set.add("b"); c.addComposited(set); @@ -166,11 +168,11 @@ public void testMultipleCollectionsSize() { setUpTest(); - HashSet set = new HashSet(); + HashSet<String> set = new HashSet<String>(); set.add("a"); set.add("b"); c.addComposited(set); - HashSet other = new HashSet(); + HashSet<String> other = new HashSet<String>(); other.add("c"); c.addComposited(other); assertEquals(set.size() + other.size(), c.size()); @@ -179,7 +181,7 @@ public void testIsEmpty() { setUpTest(); assertTrue(c.isEmpty()); - HashSet empty = new HashSet(); + HashSet<String> empty = new HashSet<String>(); c.addComposited(empty); assertTrue(c.isEmpty()); empty.add("a"); @@ -193,8 +195,8 @@ two.add("2"); c.addComposited(one); c.addComposited(two); - Iterator i = c.iterator(); - Object next = i.next(); + Iterator<String> i = c.iterator(); + String next = i.next(); assertTrue(c.contains(next)); assertTrue(one.contains(next)); next = i.next(); @@ -237,25 +239,25 @@ public void testAddAllMutator() { setUpTest(); - c.setMutator(new CompositeCollection.CollectionMutator() { - public boolean add(CompositeCollection composite, - Collection[] collections, Object obj) { - for (int i = 0; i < collections.length; i++) { - collections[i].add(obj); + c.setMutator(new CompositeCollection.CollectionMutator<String>() { + public boolean add(CompositeCollection<String> composite, + List<Collection<String>> collections, String obj) { + for (Collection<String> collection : collections) { + collection.add(obj); } return true; } - public boolean addAll(CompositeCollection composite, - Collection[] collections, Collection coll) { - for (int i = 0; i < collections.length; i++) { - collections[i].addAll(coll); + public boolean addAll(CompositeCollection<String> composite, + List<Collection<String>> collections, Collection<? extends String> coll) { + for (Collection<String> collection : collections) { + collection.addAll(coll); } return true; } - public boolean remove(CompositeCollection composite, - Collection[] collections, Object obj) { + public boolean remove(CompositeCollection<String> composite, + List<Collection<String>> collections, Object obj) { return false; } }); @@ -269,25 +271,25 @@ public void testAddMutator() { setUpTest(); - c.setMutator(new CompositeCollection.CollectionMutator() { - public boolean add(CompositeCollection composite, - Collection[] collections, Object obj) { - for (int i = 0; i < collections.length; i++) { - collections[i].add(obj); + c.setMutator(new CompositeCollection.CollectionMutator<String>() { + public boolean add(CompositeCollection<String> composite, + List<Collection<String>> collections, String obj) { + for (Collection<String> collection : collections) { + collection.add(obj); } return true; } - public boolean addAll(CompositeCollection composite, - Collection[] collections, Collection coll) { - for (int i = 0; i < collections.length; i++) { - collections[i].addAll(coll); + public boolean addAll(CompositeCollection<String> composite, + List<Collection<String>> collections, Collection<? extends String> coll) { + for (Collection<String> collection : collections) { + collection.addAll(coll); } return true; } - public boolean remove(CompositeCollection composite, - Collection[] collections, Object obj) { + public boolean remove(CompositeCollection<String> composite, + List<Collection<String>> collections, Object obj) { return false; } }); @@ -303,7 +305,7 @@ one.add("1"); two.add("2"); c.addComposited(one, two); - Collection foo = c.toCollection(); + Collection<String> foo = c.toCollection(); assertTrue(foo.containsAll(c)); assertEquals(c.size(), foo.size()); one.add("3"); @@ -315,7 +317,7 @@ one.add("1"); two.add("2"); c.addComposited(one, two); - Collection toCollection = new HashSet(); + Collection<String> toCollection = new HashSet<String>(); toCollection.addAll(c); assertTrue(toCollection.containsAll(c)); assertEquals(c.size(), toCollection.size()); @@ -338,8 +340,10 @@ one.add("1"); two.add("2"); two.add("1"); + // need separate list to remove, as otherwise one clears itself + Collection<String> removing = new ArrayList<String>(one); c.addComposited(one, two); - c.removeAll(one); + c.removeAll(removing); assertTrue(!c.contains("1")); assertTrue(!one.contains("1")); assertTrue(!two.contains("1")); Modified: jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/set/TestCompositeSet.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/set/TestCompositeSet.java?view=diff&rev=471580&r1=471579&r2=471580 ============================================================================== --- jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/set/TestCompositeSet.java (original) +++ jakarta/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/set/TestCompositeSet.java Sun Nov 5 16:22:53 2006 @@ -16,15 +16,16 @@ */ package org.apache.commons.collections.set; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.collections.collection.CompositeCollection; -import java.util.Set; -import java.util.HashSet; -import java.util.Collection; - /** * Extension of [EMAIL PROTECTED] AbstractTestSet} for exercising the * [EMAIL PROTECTED] CompositeSet} implementation. @@ -55,17 +56,17 @@ } public boolean add(CompositeCollection composite, - Collection[] collections, Object obj) { + List collections, Object obj) { return contained.add(obj); } public boolean addAll(CompositeCollection composite, - Collection[] collections, Collection coll) { + List collections, Collection coll) { return contained.addAll(coll); } public boolean remove(CompositeCollection composite, - Collection[] collections, Object obj) { + List collections, Object obj) { return contained.remove(obj); } }); @@ -123,17 +124,17 @@ } public boolean add(CompositeCollection composite, - Collection[] collections, Object obj) { + List collections, Object obj) { throw new UnsupportedOperationException(); } public boolean addAll(CompositeCollection composite, - Collection[] collections, Collection coll) { + List collections, Collection coll) { throw new UnsupportedOperationException(); } public boolean remove(CompositeCollection composite, - Collection[] collections, Object obj) { + List collections, Object obj) { throw new UnsupportedOperationException(); } }); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]