diff --git a/src/java/org/apache/commons/collections/collection/CompositeCollection.java b/src/java/org/apache/commons/collections/collection/CompositeCollection.java index 260ffb864..3add3edd2 100644 --- a/src/java/org/apache/commons/collections/collection/CompositeCollection.java +++ b/src/java/org/apache/commons/collections/collection/CompositeCollection.java @@ -22,6 +22,7 @@ import java.util.ArrayList; 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; @@ -31,9 +32,10 @@ import org.apache.commons.collections.list.UnmodifiableList; * Decorates a collection of other collections to provide a single unified view. *

* Changes made to this collection will actually be made on the decorated collection. - * Add and remove operations require the use of a pluggable strategy. If no + * Add and remove operations require the use of a pluggable strategy. If no * strategy is provided then add and remove are unsupported. * + * @param the type of the elements in the collection * @since Commons Collections 3.0 * @version $Revision$ $Date$ * @@ -41,43 +43,62 @@ import org.apache.commons.collections.list.UnmodifiableList; * @author Stephen Colebourne * @author Phil Steitz */ -public class CompositeCollection implements Collection, Serializable { - +public class CompositeCollection implements Collection, Serializable { + /** CollectionMutator to handle changes to the collection */ - protected CollectionMutator mutator; - + protected CollectionMutator mutator; + /** Collections in the composite */ - protected Collection[] all; - + protected List> all = new ArrayList>(); + /** * 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 compositeCollection) { + super(); + addComposited(compositeCollection); } - + /** - * Create a CompositeCollection with colls as the initial list of - * composited collections. - * - * @param colls an array of collections to decorate + * 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[] colls) { - this(); - this.addComposited(colls); + public CompositeCollection(Collection compositeCollection1, Collection compositeCollection2) { + super(); + addComposited(compositeCollection1, compositeCollection2); } - + + /** + * Create a Composite Collection with an array of collections. + * + * @param compositeCollections the collections to composite + */ + public CompositeCollection(Collection[] compositeCollections) { + super(); + addComposited(compositeCollections); + } + +// /** +// * Create a Composite Collection extracting the collections from an iterable. +// * +// * @param compositeCollections the collections to composite +// */ +// public CompositeCollection(Iterable> compositeCollections) { +// super(); +// addComposited(compositeCollections); +// } + //----------------------------------------------------------------------- /** * Gets the size of this composite collection. @@ -88,12 +109,12 @@ public class CompositeCollection implements Collection, Serializable { */ public int size() { int size = 0; - for (int i = this.all.length - 1; i >= 0; i--) { - size += this.all[i].size(); + for (Collection item : all) { + size += item.size(); } return size; } - + /** * Checks whether this composite collection is empty. *

@@ -102,14 +123,14 @@ public class CompositeCollection implements Collection, Serializable { * @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 item : all) { + if (item.isEmpty() == false) { return false; } } return true; } - + /** * Checks whether this composite collection contains the object. *

@@ -119,14 +140,14 @@ public class CompositeCollection implements Collection, Serializable { * @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 item : all) { + if (item.contains(obj)) { return true; } } return false; } - + /** * Gets an iterator over all the collections in this composite. *

@@ -137,31 +158,31 @@ public class CompositeCollection implements Collection, Serializable { * the order they were added, but this behavior should not be relied upon. * @see IteratorChain */ - public Iterator iterator() { - if (this.all.length == 0) { - return EmptyIterator.INSTANCE; + public Iterator iterator() { + if (all.isEmpty()) { + return EmptyIterator.getInstance(); } - IteratorChain chain = new IteratorChain(); - for (int i = 0; i < this.all.length; ++i) { - chain.addIterator(this.all[i].iterator()); + IteratorChain chain = new IteratorChain(); + for (Collection item : all) { + chain.addIterator(item.iterator()); } return chain; } - + /** * Returns an array containing all of the elements in this composite. * * @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 it = iterator(); it.hasNext(); i++) { result[i] = it.next(); } return result; } - + /** * Returns an object array, populating the supplied array if possible. * See Collection interface for full details. @@ -169,28 +190,28 @@ public class CompositeCollection implements Collection, Serializable { * @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[] toArray(T[] array) { + int size = size(); Object[] result = null; if (array.length >= size) { result = array; - } - else { + } else { result = (Object[]) Array.newInstance(array.getClass().getComponentType(), size); } - + int offset = 0; - for (int i = 0; i < this.all.length; ++i) { - for (Iterator it = this.all[i].iterator(); it.hasNext();) { - result[offset++] = it.next(); + for (Collection item : all) { + for (E e : item) { + result[offset++] = e; } } if (result.length > size) { result[size] = null; } - return result; + return (T[]) result; } - + /** * Adds an object to the collection, throwing UnsupportedOperationException * unless a CollectionMutator strategy is specified. @@ -203,14 +224,14 @@ public class CompositeCollection implements Collection, Serializable { * @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); } - + /** * Removes an object from the collection, throwing UnsupportedOperationException * unless a CollectionMutator strategy is specified. @@ -223,13 +244,13 @@ public class CompositeCollection implements Collection, Serializable { * @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); } - + /** * Checks whether this composite contains all the elements in the specified collection. *

@@ -239,15 +260,15 @@ public class CompositeCollection implements Collection, Serializable { * @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; } } return true; } - + /** * Adds a collection of elements to this collection, throwing * UnsupportedOperationException unless a CollectionMutator strategy is specified. @@ -260,14 +281,14 @@ public class CompositeCollection implements Collection, Serializable { * @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 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); } - + /** * Removes the elements in the specified collection from this composite collection. *

@@ -277,17 +298,17 @@ public class CompositeCollection implements Collection, Serializable { * @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 item : all) { + changed |= item.removeAll(coll); } return changed; } - + /** * Retains all the elements in the specified collection in this composite collection, * removing all others. @@ -298,14 +319,14 @@ public class CompositeCollection implements Collection, Serializable { * @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 item : all) { + changed |= item.retainAll(coll); } return changed; } - + /** * Removes all of the elements from this collection . *

@@ -314,88 +335,106 @@ public class CompositeCollection implements Collection, Serializable { * @throws UnsupportedOperationException if clear is unsupported */ public void clear() { - for (int i = 0; i < this.all.length; ++i) { - this.all[i].clear(); + for (Collection coll : all) { + coll.clear(); } } - + //----------------------------------------------------------------------- /** * Specify a CollectionMutator strategy instance to handle changes. * * @param mutator the mutator to use */ - public void setMutator(CollectionMutator mutator) { + public void setMutator(CollectionMutator 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 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 compositeCollection1, Collection 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[] 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> compositeCollections) { +// for (Collection 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 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 not backed by this composite. */ - public Collection toCollection() { - return new ArrayList(this); + public Collection toCollection() { + return new ArrayList(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> getCollections() { + return UnmodifiableList.decorate(all); } - + + /** + * Get the collection mutator to be used for this CompositeCollection. + * @return CollectionMutator + */ + protected CollectionMutator getMutator() { + return mutator; + } + //----------------------------------------------------------------------- /** * Pluggable strategy to handle changes to the composite. + * + * @param the element being held in the collection */ - public interface CollectionMutator extends Serializable { - + public interface CollectionMutator extends Serializable { + /** * Called when an object is to be added to the composite. * @@ -408,8 +447,8 @@ public class CompositeCollection implements Collection, Serializable { * @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 composite, List> collections, E obj); + /** * Called when a collection is to be added to the composite. * @@ -422,8 +461,8 @@ public class CompositeCollection implements Collection, Serializable { * @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 composite, List> collections, Collection coll); + /** * Called when an object is to be removed to the composite. * @@ -436,9 +475,9 @@ public class CompositeCollection implements Collection, Serializable { * @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 composite, List> collections, Object obj); + } - + }