Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.

Also see the following revisions:

    ------------------------------------------------------------------------
    r471580 | scolebourne | 2006-11-05 16:22:53 -0800 (Sun, 05 Nov 2006) | 1 line
    
    Generify CompositeCollection
    ------------------------------------------------------------------------
    r471203 | scolebourne | 2006-11-04 06:28:02 -0800 (Sat, 04 Nov 2006) | 1 line
    
    Whitespace change
    ------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815036 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:54:43 +00:00
parent 10af29350d
commit 4e30197607
1 changed files with 164 additions and 125 deletions

View File

@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.iterators.EmptyIterator; import org.apache.commons.collections.iterators.EmptyIterator;
import org.apache.commons.collections.iterators.IteratorChain; 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. * Decorates a collection of other collections to provide a single unified view.
* <p> * <p>
* Changes made to this collection will actually be made on the decorated collection. * 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. * strategy is provided then add and remove are unsupported.
* *
* @param <E> the type of the elements in the collection
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* *
@ -41,43 +43,62 @@ import org.apache.commons.collections.list.UnmodifiableList;
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Phil Steitz * @author Phil Steitz
*/ */
public class CompositeCollection implements Collection, Serializable { public class CompositeCollection<E> implements Collection<E>, Serializable {
/** CollectionMutator to handle changes to the collection */ /** CollectionMutator to handle changes to the collection */
protected CollectionMutator mutator; protected CollectionMutator<E> mutator;
/** Collections in the composite */ /** Collections in the composite */
protected Collection[] all; protected List<Collection<E>> all = new ArrayList<Collection<E>>();
/** /**
* Create an empty CompositeCollection. * Create an empty CompositeCollection.
*/ */
public CompositeCollection() { public CompositeCollection() {
super(); super();
this.all = new Collection[0];
} }
/** /**
* Create a Composite Collection with only coll composited. * Create a Composite Collection with one collection.
* *
* @param coll a collection to decorate * @param compositeCollection the Collection to be appended to the composite
*/ */
public CompositeCollection(Collection coll) { public CompositeCollection(Collection<E> compositeCollection) {
this(); super();
this.addComposited(coll); addComposited(compositeCollection);
} }
/** /**
* Create a CompositeCollection with colls as the initial list of * Create a Composite Collection with two collections.
* composited collections. *
* * @param compositeCollection1 the Collection to be appended to the composite
* @param colls an array of collections to decorate * @param compositeCollection2 the Collection to be appended to the composite
*/ */
public CompositeCollection(Collection[] colls) { public CompositeCollection(Collection<E> compositeCollection1, Collection<E> compositeCollection2) {
this(); super();
this.addComposited(colls); addComposited(compositeCollection1, compositeCollection2);
} }
/**
* Create a Composite Collection with an array of collections.
*
* @param compositeCollections the collections to composite
*/
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. * Gets the size of this composite collection.
@ -88,12 +109,12 @@ public class CompositeCollection implements Collection, Serializable {
*/ */
public int size() { public int size() {
int size = 0; int size = 0;
for (int i = this.all.length - 1; i >= 0; i--) { for (Collection<E> item : all) {
size += this.all[i].size(); size += item.size();
} }
return size; return size;
} }
/** /**
* Checks whether this composite collection is empty. * Checks whether this composite collection is empty.
* <p> * <p>
@ -102,14 +123,14 @@ public class CompositeCollection implements Collection, Serializable {
* @return true if all of the contained collections are empty * @return true if all of the contained collections are empty
*/ */
public boolean isEmpty() { public boolean isEmpty() {
for (int i = this.all.length - 1; i >= 0; i--) { for (Collection<? extends E> item : all) {
if (this.all[i].isEmpty() == false) { if (item.isEmpty() == false) {
return false; return false;
} }
} }
return true; return true;
} }
/** /**
* Checks whether this composite collection contains the object. * Checks whether this composite collection contains the object.
* <p> * <p>
@ -119,14 +140,14 @@ public class CompositeCollection implements Collection, Serializable {
* @return true if obj is contained in any of the contained collections * @return true if obj is contained in any of the contained collections
*/ */
public boolean contains(Object obj) { public boolean contains(Object obj) {
for (int i = this.all.length - 1; i >= 0; i--) { for (Collection<? extends E> item : all) {
if (this.all[i].contains(obj)) { if (item.contains(obj)) {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* Gets an iterator over all the collections in this composite. * Gets an iterator over all the collections in this composite.
* <p> * <p>
@ -137,31 +158,31 @@ public class CompositeCollection implements Collection, Serializable {
* the order they were added, but this behavior should not be relied upon. * the order they were added, but this behavior should not be relied upon.
* @see IteratorChain * @see IteratorChain
*/ */
public Iterator iterator() { public Iterator<E> iterator() {
if (this.all.length == 0) { if (all.isEmpty()) {
return EmptyIterator.INSTANCE; return EmptyIterator.<E>getInstance();
} }
IteratorChain chain = new IteratorChain(); IteratorChain<E> chain = new IteratorChain<E>();
for (int i = 0; i < this.all.length; ++i) { for (Collection<? extends E> item : all) {
chain.addIterator(this.all[i].iterator()); chain.addIterator(item.iterator());
} }
return chain; return chain;
} }
/** /**
* Returns an array containing all of the elements in this composite. * Returns an array containing all of the elements in this composite.
* *
* @return an object array of all the elements in the collection * @return an object array of all the elements in the collection
*/ */
public Object[] toArray() { public Object[] toArray() {
final Object[] result = new Object[this.size()]; final Object[] result = new Object[size()];
int i = 0; int i = 0;
for (Iterator it = this.iterator(); it.hasNext(); i++) { for (Iterator<E> it = iterator(); it.hasNext(); i++) {
result[i] = it.next(); result[i] = it.next();
} }
return result; return result;
} }
/** /**
* Returns an object array, populating the supplied array if possible. * Returns an object array, populating the supplied array if possible.
* See <code>Collection</code> interface for full details. * See <code>Collection</code> interface for full details.
@ -169,28 +190,28 @@ public class CompositeCollection implements Collection, Serializable {
* @param array the array to use, populating if possible * @param array the array to use, populating if possible
* @return an array of all the elements in the collection * @return an array of all the elements in the collection
*/ */
public Object[] toArray(Object[] array) { @SuppressWarnings("unchecked")
int size = this.size(); public <T> T[] toArray(T[] array) {
int size = size();
Object[] result = null; Object[] result = null;
if (array.length >= size) { if (array.length >= size) {
result = array; result = array;
} } else {
else {
result = (Object[]) Array.newInstance(array.getClass().getComponentType(), size); result = (Object[]) Array.newInstance(array.getClass().getComponentType(), size);
} }
int offset = 0; int offset = 0;
for (int i = 0; i < this.all.length; ++i) { for (Collection<? extends E> item : all) {
for (Iterator it = this.all[i].iterator(); it.hasNext();) { for (E e : item) {
result[offset++] = it.next(); result[offset++] = e;
} }
} }
if (result.length > size) { if (result.length > size) {
result[size] = null; result[size] = null;
} }
return result; return (T[]) result;
} }
/** /**
* Adds an object to the collection, throwing UnsupportedOperationException * Adds an object to the collection, throwing UnsupportedOperationException
* unless a CollectionMutator strategy is specified. * 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 NullPointerException if the object cannot be added because its null
* @throws IllegalArgumentException if the object cannot be added * @throws IllegalArgumentException if the object cannot be added
*/ */
public boolean add(Object obj) { public boolean add(E obj) {
if (this.mutator == null) { if (mutator == null) {
throw new UnsupportedOperationException( 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 * Removes an object from the collection, throwing UnsupportedOperationException
* unless a CollectionMutator strategy is specified. * unless a CollectionMutator strategy is specified.
@ -223,13 +244,13 @@ public class CompositeCollection implements Collection, Serializable {
* @throws IllegalArgumentException if the object cannot be removed * @throws IllegalArgumentException if the object cannot be removed
*/ */
public boolean remove(Object obj) { public boolean remove(Object obj) {
if (this.mutator == null) { if (mutator == null) {
throw new UnsupportedOperationException( 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. * Checks whether this composite contains all the elements in the specified collection.
* <p> * <p>
@ -239,15 +260,15 @@ public class CompositeCollection implements Collection, Serializable {
* @param coll the collection to check for * @param coll the collection to check for
* @return true if all elements contained * @return true if all elements contained
*/ */
public boolean containsAll(Collection coll) { public boolean containsAll(Collection<?> coll) {
for (Iterator it = coll.iterator(); it.hasNext();) { for (Object item : coll) {
if (this.contains(it.next()) == false) { if (contains(item) == false) {
return false; return false;
} }
} }
return true; return true;
} }
/** /**
* Adds a collection of elements to this collection, throwing * Adds a collection of elements to this collection, throwing
* UnsupportedOperationException unless a CollectionMutator strategy is specified. * 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 NullPointerException if the object cannot be added because its null
* @throws IllegalArgumentException if the object cannot be added * @throws IllegalArgumentException if the object cannot be added
*/ */
public boolean addAll(Collection coll) { public boolean addAll(Collection<? extends E> coll) {
if (this.mutator == null) { if (mutator == null) {
throw new UnsupportedOperationException( 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. * Removes the elements in the specified collection from this composite collection.
* <p> * <p>
@ -277,17 +298,17 @@ public class CompositeCollection implements Collection, Serializable {
* @return true if the collection was modified * @return true if the collection was modified
* @throws UnsupportedOperationException if removeAll is unsupported * @throws UnsupportedOperationException if removeAll is unsupported
*/ */
public boolean removeAll(Collection coll) { public boolean removeAll(Collection<?> coll) {
if (coll.size() == 0) { if (coll.size() == 0) {
return false; return false;
} }
boolean changed = false; boolean changed = false;
for (int i = this.all.length - 1; i >= 0; i--) { for (Collection<? extends E> item : all) {
changed = (this.all[i].removeAll(coll) || changed); changed |= item.removeAll(coll);
} }
return changed; return changed;
} }
/** /**
* Retains all the elements in the specified collection in this composite collection, * Retains all the elements in the specified collection in this composite collection,
* removing all others. * removing all others.
@ -298,14 +319,14 @@ public class CompositeCollection implements Collection, Serializable {
* @return true if the collection was modified * @return true if the collection was modified
* @throws UnsupportedOperationException if retainAll is unsupported * @throws UnsupportedOperationException if retainAll is unsupported
*/ */
public boolean retainAll(final Collection coll) { public boolean retainAll(final Collection<?> coll) {
boolean changed = false; boolean changed = false;
for (int i = this.all.length - 1; i >= 0; i--) { for (Collection<? extends E> item : all) {
changed = (this.all[i].retainAll(coll) || changed); changed |= item.retainAll(coll);
} }
return changed; return changed;
} }
/** /**
* Removes all of the elements from this collection . * Removes all of the elements from this collection .
* <p> * <p>
@ -314,88 +335,106 @@ public class CompositeCollection implements Collection, Serializable {
* @throws UnsupportedOperationException if clear is unsupported * @throws UnsupportedOperationException if clear is unsupported
*/ */
public void clear() { public void clear() {
for (int i = 0; i < this.all.length; ++i) { for (Collection<? extends E> coll : all) {
this.all[i].clear(); coll.clear();
} }
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Specify a CollectionMutator strategy instance to handle changes. * Specify a CollectionMutator strategy instance to handle changes.
* *
* @param mutator the mutator to use * @param mutator the mutator to use
*/ */
public void setMutator(CollectionMutator mutator) { public void setMutator(CollectionMutator<E> mutator) {
this.mutator = mutator; this.mutator = mutator;
} }
/** /**
* Add these Collections to the list of collections in this composite * 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) { public void addComposited(Collection<E> compositeCollection) {
ArrayList list = new ArrayList(Arrays.asList(this.all)); all.add(compositeCollection);
list.addAll(Arrays.asList(comps));
all = (Collection[]) list.toArray(new Collection[list.size()]);
} }
/** /**
* Add an additional collection to this composite. * Add these Collections to the list of collections in this composite
* *
* @param c the collection to add * @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) { public void addComposited(Collection<E> compositeCollection1, Collection<E> compositeCollection2) {
this.addComposited(new Collection[]{c}); all.add(compositeCollection1);
all.add(compositeCollection2);
} }
/** /**
* Add two additional collections to this composite. * Add these Collections to the list of collections in this composite
* *
* @param c the first collection to add * @param compositeCollections the Collections to be appended to the composite
* @param d the second collection to add
*/ */
public void addComposited(Collection c, Collection d) { public void addComposited(Collection<E>[] compositeCollections) {
this.addComposited(new Collection[]{c, d}); 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. * Removes a collection from the those being decorated in this composite.
* *
* @param coll collection to be removed * @param coll collection to be removed
*/ */
public void removeComposited(Collection coll) { public void removeComposited(Collection<E> coll) {
ArrayList list = new ArrayList(this.all.length); all.remove(coll);
list.addAll(Arrays.asList(this.all));
list.remove(coll);
this.all = (Collection[]) list.toArray(new Collection[list.size()]);
} }
//-----------------------------------------------------------------------
/** /**
* Returns a new collection containing all of the elements * Returns a new collection containing all of the elements
* *
* @return A new ArrayList containing all of the elements in this composite. * @return A new ArrayList containing all of the elements in this composite.
* The new collection is <i>not</i> backed by this composite. * The new collection is <i>not</i> backed by this composite.
*/ */
public Collection toCollection() { public Collection<E> toCollection() {
return new ArrayList(this); return new ArrayList<E>(this);
} }
/** /**
* Gets the collections being decorated. * 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() { public List<? extends Collection<E>> getCollections() {
return UnmodifiableList.decorate(Arrays.asList(this.all)); return UnmodifiableList.decorate(all);
} }
/**
* Get the collection mutator to be used for this CompositeCollection.
* @return CollectionMutator<E>
*/
protected CollectionMutator<E> getMutator() {
return mutator;
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Pluggable strategy to handle changes to the composite. * Pluggable strategy to handle changes to the composite.
*
* @param <E> the element being held in the collection
*/ */
public interface CollectionMutator extends Serializable { public interface CollectionMutator<E> extends Serializable {
/** /**
* Called when an object is to be added to the composite. * 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 NullPointerException if the object cannot be added because its null
* @throws IllegalArgumentException if the object cannot be added * @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. * 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 NullPointerException if the object cannot be added because its null
* @throws IllegalArgumentException if the object cannot be added * @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. * 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 NullPointerException if the object cannot be removed because its null
* @throws IllegalArgumentException if the object cannot be removed * @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);
} }
} }