Author: bayard
Date: Tue Sep 15 05:54:43 2009
New Revision: 815036

URL: http://svn.apache.org/viewvc?rev=815036&view=rev
Log:
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
    ------------------------------------------------------------------------

Modified:
    
commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/CompositeCollection.java

Modified: 
commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/CompositeCollection.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/CompositeCollection.java?rev=815036&r1=815035&r2=815036&view=diff
==============================================================================
--- 
commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/CompositeCollection.java
 (original)
+++ 
commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/CompositeCollection.java
 Tue Sep 15 05:54:43 2009
@@ -22,6 +22,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;
@@ -31,9 +32,10 @@
  * Decorates a collection of other collections to provide a single unified 
view.
  * <p>
  * 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 <E> the type of the elements in the collection
  * @since Commons Collections 3.0
  * @version $Revision$ $Date$
  *
@@ -41,43 +43,62 @@
  * @author Stephen Colebourne
  * @author Phil Steitz
  */
-public class CompositeCollection implements Collection, Serializable {
-    
+public class CompositeCollection<E> implements Collection<E>, Serializable {
+
     /** 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 one collection.
+     *
+     * @param compositeCollection  the Collection to be appended to the 
composite
+     */
+    public CompositeCollection(Collection<E> compositeCollection) {
+        super();
+        addComposited(compositeCollection);
+    }
+
     /**
-     * Create a Composite Collection with only coll composited.
-     * 
-     * @param coll  a collection 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 coll) {
-        this();
-        this.addComposited(coll);
+    public CompositeCollection(Collection<E> compositeCollection1, 
Collection<E> compositeCollection2) {
+        super();
+        addComposited(compositeCollection1, compositeCollection2);
     }
-    
+
     /**
-     * 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 an array of collections.
+     *
+     * @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.
@@ -88,12 +109,12 @@
      */
     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;
     }
-    
+
     /**
      * Checks whether this composite collection is empty.
      * <p>
@@ -102,14 +123,14 @@
      * @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<? extends E> item : all) {
+            if (item.isEmpty() == false) {
                 return false;
             }
         }
         return true;
     }
-    
+
     /**
      * Checks whether this composite collection contains the object.
      * <p>
@@ -119,14 +140,14 @@
      * @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<? extends E> item : all) {
+            if (item.contains(obj)) {
                 return true;
             }
         }
         return false;
     }
-    
+
     /**
      * Gets an iterator over all the collections in this composite.
      * <p>
@@ -137,31 +158,31 @@
      *  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;
-        }
-        IteratorChain chain = new IteratorChain();
-        for (int i = 0; i < this.all.length; ++i) {
-            chain.addIterator(this.all[i].iterator());
+    public Iterator<E> iterator() {
+        if (all.isEmpty()) {
+            return EmptyIterator.<E>getInstance();
+        }
+        IteratorChain<E> chain = new IteratorChain<E>();
+        for (Collection<? extends E> 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<E> it = iterator(); it.hasNext(); i++) {
             result[i] = it.next();
         }
         return result;
     }
-    
+
     /**
      * Returns an object array, populating the supplied array if possible.
      * See <code>Collection</code> interface for full details.
@@ -169,28 +190,28 @@
      * @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;
-        }
-        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<? extends E> 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 @@
      * @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 @@
      * @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.
      * <p>
@@ -239,15 +260,15 @@
      * @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 @@
      * @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);
     }
-    
+
     /**
      * Removes the elements in the specified collection from this composite 
collection.
      * <p>
@@ -277,17 +298,17 @@
      * @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<? extends E> 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 @@
      * @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<? extends E> item : all) {
+            changed |= item.retainAll(coll);
         }
         return changed;
     }
-    
+
     /**
      * Removes all of the elements from this collection .
      * <p>
@@ -314,88 +335,106 @@
      * @throws UnsupportedOperationException if clear is unsupported
      */
     public void clear() {
-        for (int i = 0; i < this.all.length; ++i) {
-            this.all[i].clear();
+        for (Collection<? extends E> 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<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 List<? extends Collection<E>> getCollections() {
+        return UnmodifiableList.decorate(all);
+    }
+
+    /**
+     * Get the collection mutator to be used for this CompositeCollection.
+     * @return CollectionMutator<E>
      */
-    public Collection getCollections() {
-        return UnmodifiableList.decorate(Arrays.asList(this.all));
+    protected CollectionMutator<E> getMutator() {
+        return mutator;
     }
-    
+
     //-----------------------------------------------------------------------
     /**
      * 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.
          *
@@ -408,8 +447,8 @@
          * @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.
          *
@@ -422,8 +461,8 @@
          * @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.
          *
@@ -436,9 +475,9 @@
          * @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);
+
     }
-    
+
 }
 


Reply via email to