commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r815048 - /commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FunctorUtils.java
Date Tue, 15 Sep 2009 05:55:06 GMT
Author: bayard
Date: Tue Sep 15 05:55:06 2009
New Revision: 815048

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

    ------------------------------------------------------------------------
    r641231 | skestle | 2008-03-26 02:58:51 -0700 (Wed, 26 Mar 2008) | 1 line
    
    Started incorporating Edwin's patch for COLLECTIONS-253, in preparation for COLLECTIONS-290.
    ------------------------------------------------------------------------

Modified:
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FunctorUtils.java

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FunctorUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FunctorUtils.java?rev=815048&r1=815047&r2=815048&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FunctorUtils.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/FunctorUtils.java
Tue Sep 15 05:55:06 2009
@@ -17,7 +17,6 @@
 package org.apache.commons.collections.functors;
 
 import java.util.Collection;
-import java.util.Iterator;
 
 import org.apache.commons.collections.Closure;
 import org.apache.commons.collections.Predicate;
@@ -33,33 +32,53 @@
  * @author Matt Benson
  */
 class FunctorUtils {
-    
+
     /**
      * Restricted constructor.
      */
     private FunctorUtils() {
         super();
     }
-    
+
     /**
      * Clone the predicates to ensure that the internal reference can't be messed with.
-     * 
+     * Due to the {@link Predicate#evaluate(T)} method, Predicate<? super T> is
+     * able to be coerced to Predicate<T> without casting issues.
+     *
      * @param predicates  the predicates to copy
      * @return the cloned predicates
      */
-    static Predicate[] copy(Predicate[] predicates) {
+    @SuppressWarnings("unchecked")
+    static <T> Predicate<T>[] copy(Predicate<? super T>[] predicates) {
         if (predicates == null) {
             return null;
         }
-        return (Predicate[]) predicates.clone();
+        return (Predicate<T>[]) predicates.clone();
+    }
+
+    /**
+     * A very simple method that coerces Predicate<? super T> to Predicate<T>.
+     * Due to the {@link Predicate#evaluate(T)} method, Predicate<? super T> is
+     * able to be coerced to Predicate<T> without casting issues.
+     * <p>This method exists
+     * simply as centralised documentation and atomic unchecked warning
+     * suppression.
+     *
+     * @param <T> the type of object the returned predicate should "accept"
+     * @param predicate the predicate to coerce.
+     * @return the coerced predicate.
+     */
+    @SuppressWarnings("unchecked")
+    static <T> Predicate<T> coerce(Predicate<? super T> predicate){
+        return (Predicate<T>) predicate;
     }
-    
+
     /**
      * Validate the predicates to ensure that all is well.
-     * 
+     *
      * @param predicates  the predicates to validate
      */
-    static void validate(Predicate[] predicates) {
+    static void validate(Predicate<?>[] predicates) {
         if (predicates == null) {
             throw new IllegalArgumentException("The predicate array must not be null");
         }
@@ -69,22 +88,23 @@
             }
         }
     }
-    
+
     /**
      * Validate the predicates to ensure that all is well.
-     * 
+     *
      * @param predicates  the predicates to validate
      * @return predicate array
      */
-    static Predicate[] validate(Collection predicates) {
+    @SuppressWarnings("unchecked")
+    static <T> Predicate<T>[] validate(Collection<? extends Predicate<T>>
predicates) {
         if (predicates == null) {
             throw new IllegalArgumentException("The predicate collection must not be null");
         }
         // convert to array like this to guarantee iterator() ordering
-        Predicate[] preds = new Predicate[predicates.size()];
+        Predicate<T>[] preds = new Predicate[predicates.size()];
         int i = 0;
-        for (Iterator it = predicates.iterator(); it.hasNext();) {
-            preds[i] = (Predicate) it.next();
+        for (Predicate<T> predicate : predicates) {
+            preds[i] = predicate;
             if (preds[i] == null) {
                 throw new IllegalArgumentException("The predicate collection must not contain
a null predicate, index " + i + " was null");
             }
@@ -92,26 +112,27 @@
         }
         return preds;
     }
-    
+
     /**
      * Clone the closures to ensure that the internal reference can't be messed with.
-     * 
+     *
      * @param closures  the closures to copy
      * @return the cloned closures
      */
-    static Closure[] copy(Closure[] closures) {
+    @SuppressWarnings("unchecked")
+    static <E> Closure<E>[] copy(Closure<? super E>[] closures) {
         if (closures == null) {
             return null;
         }
-        return (Closure[]) closures.clone();
+        return (Closure<E>[]) closures.clone();
     }
-    
+
     /**
      * Validate the closures to ensure that all is well.
-     * 
+     *
      * @param closures  the closures to validate
      */
-    static void validate(Closure[] closures) {
+    static void validate(Closure<?>[] closures) {
         if (closures == null) {
             throw new IllegalArgumentException("The closure array must not be null");
         }
@@ -123,24 +144,40 @@
     }
 
     /**
+     * A very simple method that coerces Closure<? super T> to Closure<T>.
+     * <p>This method exists
+     * simply as centralised documentation and atomic unchecked warning
+     * suppression.
+     *
+     * @param <T> the type of object the returned closure should "accept"
+     * @param closure the closure to coerce.
+     * @return the coerced closure.
+     */
+    @SuppressWarnings("unchecked")
+    static <T> Closure<T> coerce(Closure<? super T> closure){
+        return (Closure<T>) closure;
+    }
+
+    /**
      * Copy method
-     * 
+     *
      * @param transformers  the transformers to copy
      * @return a clone of the transformers
      */
-    static Transformer[] copy(Transformer[] transformers) {
+    @SuppressWarnings("unchecked")
+    static <I, O> Transformer<I, O>[] copy(Transformer<? super I, ? extends
O>[] transformers) {
         if (transformers == null) {
             return null;
         }
-        return (Transformer[]) transformers.clone();
+        return (Transformer<I, O>[]) transformers.clone();
     }
-    
+
     /**
      * Validate method
-     * 
+     *
      * @param transformers  the transformers to validate
      */
-    static void validate(Transformer[] transformers) {
+    static void validate(Transformer<?, ?>[] transformers) {
         if (transformers == null) {
             throw new IllegalArgumentException("The transformer array must not be null");
         }
@@ -152,4 +189,19 @@
         }
     }
 
+    /**
+     * A very simple method that coerces Transformer<? super I, ? extends O> to Transformer<I,
O>.
+     * <p>This method exists
+     * simply as centralised documentation and atomic unchecked warning
+     * suppression.
+     *
+     * @param <T> the type of object the returned transformer should "accept"
+     * @param transformer the transformer to coerce.
+     * @return the coerced transformer.
+     */
+    @SuppressWarnings("unchecked")
+    static <I, O> Transformer<I, O> coerce(Transformer<? super I, ? extends
O> transformer) {
+        return (Transformer<I, O>) transformer;
+    }
+
 }



Mime
View raw message