From b9db60ade4ca7d02bbd9b530e12221e5f44e227e Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Tue, 15 Sep 2009 05:55:06 +0000 Subject: [PATCH] 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. ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815048 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections/functors/FunctorUtils.java | 108 +++++++++++++----- 1 file changed, 80 insertions(+), 28 deletions(-) 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; + } + }