You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/09/15 07:54:45 UTC

svn commit: r815037 - /commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/PredicatedCollection.java

Author: bayard
Date: Tue Sep 15 05:54:45 2009
New Revision: 815037

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

    ------------------------------------------------------------------------
    r471575 | scolebourne | 2006-11-05 15:58:08 -0800 (Sun, 05 Nov 2006) | 1 line
    
    Generify and remove AbstractSerializableCollectionDecorator
    ------------------------------------------------------------------------
    r471202 | scolebourne | 2006-11-04 06:21:44 -0800 (Sat, 04 Nov 2006) | 1 line
    
    Remove getCollection() - use covariant decorated()
    ------------------------------------------------------------------------

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

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/PredicatedCollection.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/PredicatedCollection.java?rev=815037&r1=815036&r2=815037&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/PredicatedCollection.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/collection/PredicatedCollection.java Tue Sep 15 05:54:45 2009
@@ -17,7 +17,6 @@
 package org.apache.commons.collections.collection;
 
 import java.util.Collection;
-import java.util.Iterator;
 
 import org.apache.commons.collections.Predicate;
 
@@ -34,19 +33,20 @@
  * <p>
  * This class is Serializable from Commons Collections 3.1.
  *
+ * @param <E> the type of the elements in the collection
  * @since Commons Collections 3.0
  * @version $Revision$ $Date$
  *
  * @author Stephen Colebourne
  * @author Paul Jack
  */
-public class PredicatedCollection extends AbstractSerializableCollectionDecorator {
+public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {
 
     /** Serialization version */
     private static final long serialVersionUID = -5259182142076705162L;
 
     /** The predicate to use */
-    protected final Predicate predicate;
+    protected final Predicate<? super E> predicate;
 
     /**
      * Factory method to create a predicated (validating) collection.
@@ -54,16 +54,17 @@
      * If there are any elements already in the collection being decorated, they
      * are validated.
      * 
+     * @param <T> the type of the elements in the collection
      * @param coll  the collection to decorate, must not be null
      * @param predicate  the predicate to use for validation, must not be null
      * @return a new predicated collection
      * @throws IllegalArgumentException if collection or predicate is null
      * @throws IllegalArgumentException if the collection contains invalid elements
      */
-    public static Collection decorate(Collection coll, Predicate predicate) {
-        return new PredicatedCollection(coll, predicate);
+    public static <T> Collection<T> decorate(Collection<T> coll, Predicate<? super T> predicate) {
+        return new PredicatedCollection<T>(coll, predicate);
     }
-    
+
     //-----------------------------------------------------------------------
     /**
      * Constructor that wraps (not copies).
@@ -76,14 +77,14 @@
      * @throws IllegalArgumentException if collection or predicate is null
      * @throws IllegalArgumentException if the collection contains invalid elements
      */
-    protected PredicatedCollection(Collection coll, Predicate predicate) {
+    protected PredicatedCollection(Collection<E> coll, Predicate<? super E> predicate) {
         super(coll);
         if (predicate == null) {
             throw new IllegalArgumentException("Predicate must not be null");
         }
         this.predicate = predicate;
-        for (Iterator it = coll.iterator(); it.hasNext(); ) {
-            validate(it.next());
+        for (E item : coll) {
+            validate(item);
         }
     }
 
@@ -96,7 +97,7 @@
      * @param object  the object being added
      * @throws IllegalArgumentException if the add is invalid
      */
-    protected void validate(Object object) {
+    protected void validate(E object) {
         if (predicate.evaluate(object) == false) {
             throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate '" + predicate + "' rejected it");
         }
@@ -111,9 +112,9 @@
      * @return the result of adding to the underlying collection
      * @throws IllegalArgumentException if the add is invalid
      */
-    public boolean add(Object object) {
+    public boolean add(E object) {
         validate(object);
-        return getCollection().add(object);
+        return decorated().add(object);
     }
 
     /**
@@ -125,11 +126,11 @@
      * @return the result of adding to the underlying collection
      * @throws IllegalArgumentException if the add is invalid
      */
-    public boolean addAll(Collection coll) {
-        for (Iterator it = coll.iterator(); it.hasNext(); ) {
-            validate(it.next());
+    public boolean addAll(Collection<? extends E> coll) {
+        for (E item : coll) {
+            validate(item);
         }
-        return getCollection().addAll(coll);
+        return decorated().addAll(coll);
     }
 
 }