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
This commit is contained in:
Henri Yandell 2009-09-15 05:55:06 +00:00
parent e47b5cf540
commit b9db60ade4
1 changed files with 80 additions and 28 deletions

View File

@ -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;
@ -43,15 +42,35 @@ class FunctorUtils {
/**
* 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;
}
/**
@ -59,7 +78,7 @@ class FunctorUtils {
*
* @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");
}
@ -76,15 +95,16 @@ class FunctorUtils {
* @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");
}
@ -99,11 +119,12 @@ class FunctorUtils {
* @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();
}
/**
@ -111,7 +132,7 @@ class FunctorUtils {
*
* @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,17 +143,33 @@ class FunctorUtils {
}
}
/**
* 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();
}
/**
@ -140,7 +177,7 @@ class FunctorUtils {
*
* @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<? 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;
}
}