diff --git a/src/java/org/apache/commons/collections/functors/FunctorUtils.java b/src/java/org/apache/commons/collections/functors/FunctorUtils.java index 4267cbc7d..35855587f 100644 --- a/src/java/org/apache/commons/collections/functors/FunctorUtils.java +++ b/src/java/org/apache/commons/collections/functors/FunctorUtils.java @@ -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 @@ import org.apache.commons.collections.Transformer; * @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 is + * able to be coerced to Predicate without casting issues. + * * @param predicates the predicates to copy * @return the cloned predicates */ - static Predicate[] copy(Predicate[] predicates) { + @SuppressWarnings("unchecked") + static Predicate[] copy(Predicate[] predicates) { if (predicates == null) { return null; } - return (Predicate[]) predicates.clone(); + return (Predicate[]) predicates.clone(); } - + + /** + * A very simple method that coerces Predicate to Predicate. + * Due to the {@link Predicate#evaluate(T)} method, Predicate is + * able to be coerced to Predicate without casting issues. + *

This method exists + * simply as centralised documentation and atomic unchecked warning + * suppression. + * + * @param the type of object the returned predicate should "accept" + * @param predicate the predicate to coerce. + * @return the coerced predicate. + */ + @SuppressWarnings("unchecked") + static Predicate coerce(Predicate predicate){ + return (Predicate) 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 @@ class FunctorUtils { } } } - + /** * 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 Predicate[] validate(Collection> 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[] preds = new Predicate[predicates.size()]; int i = 0; - for (Iterator it = predicates.iterator(); it.hasNext();) { - preds[i] = (Predicate) it.next(); + for (Predicate 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 @@ class FunctorUtils { } 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 Closure[] copy(Closure[] closures) { if (closures == null) { return null; } - return (Closure[]) closures.clone(); + return (Closure[]) 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"); } @@ -122,25 +143,41 @@ class FunctorUtils { } } + /** + * A very simple method that coerces Closure to Closure. + *

This method exists + * simply as centralised documentation and atomic unchecked warning + * suppression. + * + * @param the type of object the returned closure should "accept" + * @param closure the closure to coerce. + * @return the coerced closure. + */ + @SuppressWarnings("unchecked") + static Closure coerce(Closure closure){ + return (Closure) closure; + } + /** * Copy method - * + * * @param transformers the transformers to copy * @return a clone of the transformers */ - static Transformer[] copy(Transformer[] transformers) { + @SuppressWarnings("unchecked") + static Transformer[] copy(Transformer[] transformers) { if (transformers == null) { return null; } - return (Transformer[]) transformers.clone(); + return (Transformer[]) 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 @@ class FunctorUtils { } } + /** + * A very simple method that coerces Transformer to Transformer. + *

This method exists + * simply as centralised documentation and atomic unchecked warning + * suppression. + * + * @param the type of object the returned transformer should "accept" + * @param transformer the transformer to coerce. + * @return the coerced transformer. + */ + @SuppressWarnings("unchecked") + static Transformer coerce(Transformer transformer) { + return (Transformer) transformer; + } + }