All/Any/One/None Predicate - allow construction with zero or one predicates
rfe 37979, implemented by Matt Benson git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@377508 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c56039ade9
commit
7440726c44
|
@ -80,6 +80,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
|
||||||
<li>ListOrderedMap - additional method, put(int,Object,Object)</li>
|
<li>ListOrderedMap - additional method, put(int,Object,Object)</li>
|
||||||
<li>PriorityBuffer - now Serializable [36163]</li>
|
<li>PriorityBuffer - now Serializable [36163]</li>
|
||||||
<li>IfClosure - add single argument constructor [38495]</li>
|
<li>IfClosure - add single argument constructor [38495]</li>
|
||||||
|
<li>All/Any/One/None Predicate - allow construction with zero or one predicates [37979]</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<center><h3>BUG FIXES</h3></center>
|
<center><h3>BUG FIXES</h3></center>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2004 The Apache Software Foundation
|
* Copyright 2002-2004,2006 The Apache Software Foundation
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -264,13 +264,13 @@ public class PredicateUtils {
|
||||||
/**
|
/**
|
||||||
* Create a new Predicate that returns true only if all of the specified
|
* Create a new Predicate that returns true only if all of the specified
|
||||||
* predicates are true.
|
* predicates are true.
|
||||||
|
* If the array of predicates is empty, then this predicate returns true.
|
||||||
*
|
*
|
||||||
* @see org.apache.commons.collections.functors.AllPredicate
|
* @see org.apache.commons.collections.functors.AllPredicate
|
||||||
*
|
*
|
||||||
* @param predicates an array of predicates to check, may not be null
|
* @param predicates an array of predicates to check, may not be null
|
||||||
* @return the <code>all</code> predicate
|
* @return the <code>all</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
*/
|
*/
|
||||||
public static Predicate allPredicate(Predicate[] predicates) {
|
public static Predicate allPredicate(Predicate[] predicates) {
|
||||||
|
@ -280,13 +280,13 @@ public class PredicateUtils {
|
||||||
/**
|
/**
|
||||||
* Create a new Predicate that returns true only if all of the specified
|
* Create a new Predicate that returns true only if all of the specified
|
||||||
* predicates are true. The predicates are checked in iterator order.
|
* predicates are true. The predicates are checked in iterator order.
|
||||||
|
* If the collection of predicates is empty, then this predicate returns true.
|
||||||
*
|
*
|
||||||
* @see org.apache.commons.collections.functors.AllPredicate
|
* @see org.apache.commons.collections.functors.AllPredicate
|
||||||
*
|
*
|
||||||
* @param predicates a collection of predicates to check, may not be null
|
* @param predicates a collection of predicates to check, may not be null
|
||||||
* @return the <code>all</code> predicate
|
* @return the <code>all</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates collection is null
|
* @throws IllegalArgumentException if the predicates collection is null
|
||||||
* @throws IllegalArgumentException if the predicates collection has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the collection is null
|
* @throws IllegalArgumentException if any predicate in the collection is null
|
||||||
*/
|
*/
|
||||||
public static Predicate allPredicate(Collection predicates) {
|
public static Predicate allPredicate(Collection predicates) {
|
||||||
|
@ -311,13 +311,13 @@ public class PredicateUtils {
|
||||||
/**
|
/**
|
||||||
* Create a new Predicate that returns true if any of the specified
|
* Create a new Predicate that returns true if any of the specified
|
||||||
* predicates are true.
|
* predicates are true.
|
||||||
|
* If the array of predicates is empty, then this predicate returns false.
|
||||||
*
|
*
|
||||||
* @see org.apache.commons.collections.functors.AnyPredicate
|
* @see org.apache.commons.collections.functors.AnyPredicate
|
||||||
*
|
*
|
||||||
* @param predicates an array of predicates to check, may not be null
|
* @param predicates an array of predicates to check, may not be null
|
||||||
* @return the <code>any</code> predicate
|
* @return the <code>any</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
*/
|
*/
|
||||||
public static Predicate anyPredicate(Predicate[] predicates) {
|
public static Predicate anyPredicate(Predicate[] predicates) {
|
||||||
|
@ -327,13 +327,13 @@ public class PredicateUtils {
|
||||||
/**
|
/**
|
||||||
* Create a new Predicate that returns true if any of the specified
|
* Create a new Predicate that returns true if any of the specified
|
||||||
* predicates are true. The predicates are checked in iterator order.
|
* predicates are true. The predicates are checked in iterator order.
|
||||||
|
* If the collection of predicates is empty, then this predicate returns false.
|
||||||
*
|
*
|
||||||
* @see org.apache.commons.collections.functors.AnyPredicate
|
* @see org.apache.commons.collections.functors.AnyPredicate
|
||||||
*
|
*
|
||||||
* @param predicates a collection of predicates to check, may not be null
|
* @param predicates a collection of predicates to check, may not be null
|
||||||
* @return the <code>any</code> predicate
|
* @return the <code>any</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates collection is null
|
* @throws IllegalArgumentException if the predicates collection is null
|
||||||
* @throws IllegalArgumentException if the predicates collection has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the collection is null
|
* @throws IllegalArgumentException if any predicate in the collection is null
|
||||||
*/
|
*/
|
||||||
public static Predicate anyPredicate(Collection predicates) {
|
public static Predicate anyPredicate(Collection predicates) {
|
||||||
|
@ -358,13 +358,13 @@ public class PredicateUtils {
|
||||||
/**
|
/**
|
||||||
* Create a new Predicate that returns true if only one of the specified
|
* Create a new Predicate that returns true if only one of the specified
|
||||||
* predicates are true.
|
* predicates are true.
|
||||||
|
* If the array of predicates is empty, then this predicate returns false.
|
||||||
*
|
*
|
||||||
* @see org.apache.commons.collections.functors.OnePredicate
|
* @see org.apache.commons.collections.functors.OnePredicate
|
||||||
*
|
*
|
||||||
* @param predicates an array of predicates to check, may not be null
|
* @param predicates an array of predicates to check, may not be null
|
||||||
* @return the <code>one</code> predicate
|
* @return the <code>one</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
*/
|
*/
|
||||||
public static Predicate onePredicate(Predicate[] predicates) {
|
public static Predicate onePredicate(Predicate[] predicates) {
|
||||||
|
@ -374,13 +374,13 @@ public class PredicateUtils {
|
||||||
/**
|
/**
|
||||||
* Create a new Predicate that returns true if only one of the specified
|
* Create a new Predicate that returns true if only one of the specified
|
||||||
* predicates are true. The predicates are checked in iterator order.
|
* predicates are true. The predicates are checked in iterator order.
|
||||||
|
* If the collection of predicates is empty, then this predicate returns false.
|
||||||
*
|
*
|
||||||
* @see org.apache.commons.collections.functors.OnePredicate
|
* @see org.apache.commons.collections.functors.OnePredicate
|
||||||
*
|
*
|
||||||
* @param predicates a collection of predicates to check, may not be null
|
* @param predicates a collection of predicates to check, may not be null
|
||||||
* @return the <code>one</code> predicate
|
* @return the <code>one</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates collection is null
|
* @throws IllegalArgumentException if the predicates collection is null
|
||||||
* @throws IllegalArgumentException if the predicates collection has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the collection is null
|
* @throws IllegalArgumentException if any predicate in the collection is null
|
||||||
*/
|
*/
|
||||||
public static Predicate onePredicate(Collection predicates) {
|
public static Predicate onePredicate(Collection predicates) {
|
||||||
|
@ -405,13 +405,13 @@ public class PredicateUtils {
|
||||||
/**
|
/**
|
||||||
* Create a new Predicate that returns true if none of the specified
|
* Create a new Predicate that returns true if none of the specified
|
||||||
* predicates are true.
|
* predicates are true.
|
||||||
|
* If the array of predicates is empty, then this predicate returns true.
|
||||||
*
|
*
|
||||||
* @see org.apache.commons.collections.functors.NonePredicate
|
* @see org.apache.commons.collections.functors.NonePredicate
|
||||||
*
|
*
|
||||||
* @param predicates an array of predicates to check, may not be null
|
* @param predicates an array of predicates to check, may not be null
|
||||||
* @return the <code>none</code> predicate
|
* @return the <code>none</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
*/
|
*/
|
||||||
public static Predicate nonePredicate(Predicate[] predicates) {
|
public static Predicate nonePredicate(Predicate[] predicates) {
|
||||||
|
@ -421,13 +421,13 @@ public class PredicateUtils {
|
||||||
/**
|
/**
|
||||||
* Create a new Predicate that returns true if none of the specified
|
* Create a new Predicate that returns true if none of the specified
|
||||||
* predicates are true. The predicates are checked in iterator order.
|
* predicates are true. The predicates are checked in iterator order.
|
||||||
|
* If the collection of predicates is empty, then this predicate returns true.
|
||||||
*
|
*
|
||||||
* @see org.apache.commons.collections.functors.NonePredicate
|
* @see org.apache.commons.collections.functors.NonePredicate
|
||||||
*
|
*
|
||||||
* @param predicates a collection of predicates to check, may not be null
|
* @param predicates a collection of predicates to check, may not be null
|
||||||
* @return the <code>none</code> predicate
|
* @return the <code>none</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates collection is null
|
* @throws IllegalArgumentException if the predicates collection is null
|
||||||
* @throws IllegalArgumentException if the predicates collection has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the collection is null
|
* @throws IllegalArgumentException if any predicate in the collection is null
|
||||||
*/
|
*/
|
||||||
public static Predicate nonePredicate(Collection predicates) {
|
public static Predicate nonePredicate(Collection predicates) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2001-2004 The Apache Software Foundation
|
* Copyright 2001-2004,2006 The Apache Software Foundation
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,12 +21,15 @@ import java.util.Collection;
|
||||||
import org.apache.commons.collections.Predicate;
|
import org.apache.commons.collections.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Predicate implementation that returns true if all the predicates return true.
|
* Predicate implementation that returns true if all the
|
||||||
|
* predicates return true.
|
||||||
|
* If the array of predicates is empty, then this predicate returns true.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
|
* @author Matt Benson
|
||||||
*/
|
*/
|
||||||
public final class AllPredicate implements Predicate, PredicateDecorator, Serializable {
|
public final class AllPredicate implements Predicate, PredicateDecorator, Serializable {
|
||||||
|
|
||||||
|
@ -42,11 +45,10 @@ public final class AllPredicate implements Predicate, PredicateDecorator, Serial
|
||||||
* @param predicates the predicates to check, cloned, not null
|
* @param predicates the predicates to check, cloned, not null
|
||||||
* @return the <code>all</code> predicate
|
* @return the <code>all</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
*/
|
*/
|
||||||
public static Predicate getInstance(Predicate[] predicates) {
|
public static Predicate getInstance(Predicate[] predicates) {
|
||||||
FunctorUtils.validateMin2(predicates);
|
FunctorUtils.validate(predicates);
|
||||||
predicates = FunctorUtils.copy(predicates);
|
predicates = FunctorUtils.copy(predicates);
|
||||||
return new AllPredicate(predicates);
|
return new AllPredicate(predicates);
|
||||||
}
|
}
|
||||||
|
@ -58,7 +60,6 @@ public final class AllPredicate implements Predicate, PredicateDecorator, Serial
|
||||||
* @return the <code>all</code> predicate
|
* @return the <code>all</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
*/
|
*/
|
||||||
public static Predicate getInstance(Collection predicates) {
|
public static Predicate getInstance(Collection predicates) {
|
||||||
Predicate[] preds = FunctorUtils.validate(predicates);
|
Predicate[] preds = FunctorUtils.validate(predicates);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2001-2004 The Apache Software Foundation
|
* Copyright 2001-2004,2006 The Apache Software Foundation
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,12 +21,15 @@ import java.util.Collection;
|
||||||
import org.apache.commons.collections.Predicate;
|
import org.apache.commons.collections.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Predicate implementation that returns true if any of the predicates return true.
|
* Predicate implementation that returns true if any of the
|
||||||
|
* predicates return true.
|
||||||
|
* If the array of predicates is empty, then this predicate returns false.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
|
* @author Matt Benson
|
||||||
*/
|
*/
|
||||||
public final class AnyPredicate implements Predicate, PredicateDecorator, Serializable {
|
public final class AnyPredicate implements Predicate, PredicateDecorator, Serializable {
|
||||||
|
|
||||||
|
@ -42,11 +45,10 @@ public final class AnyPredicate implements Predicate, PredicateDecorator, Serial
|
||||||
* @param predicates the predicates to check, cloned, not null
|
* @param predicates the predicates to check, cloned, not null
|
||||||
* @return the <code>any</code> predicate
|
* @return the <code>any</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
*/
|
*/
|
||||||
public static Predicate getInstance(Predicate[] predicates) {
|
public static Predicate getInstance(Predicate[] predicates) {
|
||||||
FunctorUtils.validateMin2(predicates);
|
FunctorUtils.validate(predicates);
|
||||||
predicates = FunctorUtils.copy(predicates);
|
predicates = FunctorUtils.copy(predicates);
|
||||||
return new AnyPredicate(predicates);
|
return new AnyPredicate(predicates);
|
||||||
}
|
}
|
||||||
|
@ -58,7 +60,6 @@ public final class AnyPredicate implements Predicate, PredicateDecorator, Serial
|
||||||
* @return the <code>all</code> predicate
|
* @return the <code>all</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
*/
|
*/
|
||||||
public static Predicate getInstance(Collection predicates) {
|
public static Predicate getInstance(Collection predicates) {
|
||||||
Predicate[] preds = FunctorUtils.validate(predicates);
|
Predicate[] preds = FunctorUtils.validate(predicates);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2001-2004 The Apache Software Foundation
|
* Copyright 2001-2004,2006 The Apache Software Foundation
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -29,6 +29,7 @@ import org.apache.commons.collections.Transformer;
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
|
* @author Matt Benson
|
||||||
*/
|
*/
|
||||||
class FunctorUtils {
|
class FunctorUtils {
|
||||||
|
|
||||||
|
@ -68,26 +69,6 @@ class FunctorUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate the predicates to ensure that all is well.
|
|
||||||
*
|
|
||||||
* @param predicates the predicates to validate
|
|
||||||
*/
|
|
||||||
static void validateMin2(Predicate[] predicates) {
|
|
||||||
if (predicates == null) {
|
|
||||||
throw new IllegalArgumentException("The predicate array must not be null");
|
|
||||||
}
|
|
||||||
if (predicates.length < 2) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"At least 2 predicates must be specified in the predicate array, size was " + predicates.length);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < predicates.length; i++) {
|
|
||||||
if (predicates[i] == null) {
|
|
||||||
throw new IllegalArgumentException("The predicate array must not contain a null predicate, index " + i + " was null");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate the predicates to ensure that all is well.
|
* Validate the predicates to ensure that all is well.
|
||||||
*
|
*
|
||||||
|
@ -98,10 +79,6 @@ class FunctorUtils {
|
||||||
if (predicates == null) {
|
if (predicates == null) {
|
||||||
throw new IllegalArgumentException("The predicate collection must not be null");
|
throw new IllegalArgumentException("The predicate collection must not be null");
|
||||||
}
|
}
|
||||||
if (predicates.size() < 2) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"At least 2 predicates must be specified in the predicate collection, size was " + predicates.size());
|
|
||||||
}
|
|
||||||
// convert to array like this to guarantee iterator() ordering
|
// convert to array like this to guarantee iterator() ordering
|
||||||
Predicate[] preds = new Predicate[predicates.size()];
|
Predicate[] preds = new Predicate[predicates.size()];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2001-2004 The Apache Software Foundation
|
* Copyright 2001-2004,2006 The Apache Software Foundation
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,12 +21,15 @@ import java.util.Collection;
|
||||||
import org.apache.commons.collections.Predicate;
|
import org.apache.commons.collections.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Predicate implementation that returns true if none of the predicates return true.
|
* Predicate implementation that returns true if none of the
|
||||||
|
* predicates return true.
|
||||||
|
* If the array of predicates is empty, then this predicate returns true.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
|
* @author Matt Benson
|
||||||
*/
|
*/
|
||||||
public final class NonePredicate implements Predicate, PredicateDecorator, Serializable {
|
public final class NonePredicate implements Predicate, PredicateDecorator, Serializable {
|
||||||
|
|
||||||
|
@ -42,11 +45,10 @@ public final class NonePredicate implements Predicate, PredicateDecorator, Seria
|
||||||
* @param predicates the predicates to check, cloned, not null
|
* @param predicates the predicates to check, cloned, not null
|
||||||
* @return the <code>any</code> predicate
|
* @return the <code>any</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
*/
|
*/
|
||||||
public static Predicate getInstance(Predicate[] predicates) {
|
public static Predicate getInstance(Predicate[] predicates) {
|
||||||
FunctorUtils.validateMin2(predicates);
|
FunctorUtils.validate(predicates);
|
||||||
predicates = FunctorUtils.copy(predicates);
|
predicates = FunctorUtils.copy(predicates);
|
||||||
return new NonePredicate(predicates);
|
return new NonePredicate(predicates);
|
||||||
}
|
}
|
||||||
|
@ -58,7 +60,6 @@ public final class NonePredicate implements Predicate, PredicateDecorator, Seria
|
||||||
* @return the <code>one</code> predicate
|
* @return the <code>one</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
*/
|
*/
|
||||||
public static Predicate getInstance(Collection predicates) {
|
public static Predicate getInstance(Collection predicates) {
|
||||||
Predicate[] preds = FunctorUtils.validate(predicates);
|
Predicate[] preds = FunctorUtils.validate(predicates);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2001-2004 The Apache Software Foundation
|
* Copyright 2001-2004,2006 The Apache Software Foundation
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,12 +21,15 @@ import java.util.Collection;
|
||||||
import org.apache.commons.collections.Predicate;
|
import org.apache.commons.collections.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Predicate implementation that returns true if only one of the predicates return true.
|
* Predicate implementation that returns true if only one of the
|
||||||
|
* predicates return true.
|
||||||
|
* If the array of predicates is empty, then this predicate returns false.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
|
* @author Matt Benson
|
||||||
*/
|
*/
|
||||||
public final class OnePredicate implements Predicate, PredicateDecorator, Serializable {
|
public final class OnePredicate implements Predicate, PredicateDecorator, Serializable {
|
||||||
|
|
||||||
|
@ -42,11 +45,10 @@ public final class OnePredicate implements Predicate, PredicateDecorator, Serial
|
||||||
* @param predicates the predicates to check, cloned, not null
|
* @param predicates the predicates to check, cloned, not null
|
||||||
* @return the <code>any</code> predicate
|
* @return the <code>any</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
*/
|
*/
|
||||||
public static Predicate getInstance(Predicate[] predicates) {
|
public static Predicate getInstance(Predicate[] predicates) {
|
||||||
FunctorUtils.validateMin2(predicates);
|
FunctorUtils.validate(predicates);
|
||||||
predicates = FunctorUtils.copy(predicates);
|
predicates = FunctorUtils.copy(predicates);
|
||||||
return new OnePredicate(predicates);
|
return new OnePredicate(predicates);
|
||||||
}
|
}
|
||||||
|
@ -58,7 +60,6 @@ public final class OnePredicate implements Predicate, PredicateDecorator, Serial
|
||||||
* @return the <code>one</code> predicate
|
* @return the <code>one</code> predicate
|
||||||
* @throws IllegalArgumentException if the predicates array is null
|
* @throws IllegalArgumentException if the predicates array is null
|
||||||
* @throws IllegalArgumentException if any predicate in the array is null
|
* @throws IllegalArgumentException if any predicate in the array is null
|
||||||
* @throws IllegalArgumentException if the predicates array has less than 2 elements
|
|
||||||
*/
|
*/
|
||||||
public static Predicate getInstance(Collection predicates) {
|
public static Predicate getInstance(Collection predicates) {
|
||||||
Predicate[] preds = FunctorUtils.validate(predicates);
|
Predicate[] preds = FunctorUtils.validate(predicates);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2001-2004 The Apache Software Foundation
|
* Copyright 2001-2004,2006 The Apache Software Foundation
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -34,6 +34,7 @@ import junit.textui.TestRunner;
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
|
* @author Matt Benson
|
||||||
*/
|
*/
|
||||||
public class TestPredicateUtils extends junit.framework.TestCase {
|
public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
|
|
||||||
|
@ -237,6 +238,14 @@ public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
coll.add(PredicateUtils.falsePredicate());
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
coll.add(PredicateUtils.falsePredicate());
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null));
|
assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
|
assertFalse(PredicateUtils.allPredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
coll.add(PredicateUtils.truePredicate());
|
||||||
|
assertTrue(PredicateUtils.allPredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
assertTrue(PredicateUtils.allPredicate(coll).evaluate(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAllPredicateEx1() {
|
public void testAllPredicateEx1() {
|
||||||
|
@ -276,12 +285,7 @@ public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAllPredicateEx5() {
|
public void testAllPredicateEx5() {
|
||||||
try {
|
PredicateUtils.allPredicate(Collections.EMPTY_LIST);
|
||||||
PredicateUtils.allPredicate(Collections.EMPTY_LIST);
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAllPredicateEx6() {
|
public void testAllPredicateEx6() {
|
||||||
|
@ -347,6 +351,14 @@ public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
coll.add(PredicateUtils.falsePredicate());
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
coll.add(PredicateUtils.falsePredicate());
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
assertEquals(false, PredicateUtils.anyPredicate(coll).evaluate(null));
|
assertEquals(false, PredicateUtils.anyPredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
|
assertFalse(PredicateUtils.anyPredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
coll.add(PredicateUtils.truePredicate());
|
||||||
|
assertTrue(PredicateUtils.anyPredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
assertFalse(PredicateUtils.anyPredicate(coll).evaluate(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAnyPredicateEx1() {
|
public void testAnyPredicateEx1() {
|
||||||
|
@ -386,12 +398,7 @@ public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAnyPredicateEx5() {
|
public void testAnyPredicateEx5() {
|
||||||
try {
|
PredicateUtils.anyPredicate(Collections.EMPTY_LIST);
|
||||||
PredicateUtils.anyPredicate(Collections.EMPTY_LIST);
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAnyPredicateEx6() {
|
public void testAnyPredicateEx6() {
|
||||||
|
@ -461,6 +468,14 @@ public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
coll.add(PredicateUtils.falsePredicate());
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
coll.add(PredicateUtils.falsePredicate());
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null));
|
assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
|
assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
coll.add(PredicateUtils.truePredicate());
|
||||||
|
assertTrue(PredicateUtils.onePredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testOnePredicateEx1() {
|
public void testOnePredicateEx1() {
|
||||||
|
@ -500,12 +515,7 @@ public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testOnePredicateEx5() {
|
public void testOnePredicateEx5() {
|
||||||
try {
|
PredicateUtils.onePredicate(Collections.EMPTY_LIST);
|
||||||
PredicateUtils.onePredicate(Collections.EMPTY_LIST);
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testOnePredicateEx6() {
|
public void testOnePredicateEx6() {
|
||||||
|
@ -571,6 +581,14 @@ public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
coll.add(PredicateUtils.falsePredicate());
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
coll.add(PredicateUtils.falsePredicate());
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
assertEquals(true, PredicateUtils.nonePredicate(coll).evaluate(null));
|
assertEquals(true, PredicateUtils.nonePredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
coll.add(PredicateUtils.falsePredicate());
|
||||||
|
assertTrue(PredicateUtils.nonePredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
coll.add(PredicateUtils.truePredicate());
|
||||||
|
assertFalse(PredicateUtils.nonePredicate(coll).evaluate(null));
|
||||||
|
coll.clear();
|
||||||
|
assertTrue(PredicateUtils.nonePredicate(coll).evaluate(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNonePredicateEx1() {
|
public void testNonePredicateEx1() {
|
||||||
|
@ -610,12 +628,7 @@ public class TestPredicateUtils extends junit.framework.TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNonePredicateEx5() {
|
public void testNonePredicateEx5() {
|
||||||
try {
|
PredicateUtils.nonePredicate(Collections.EMPTY_LIST);
|
||||||
PredicateUtils.nonePredicate(Collections.EMPTY_LIST);
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fail();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNonePredicateEx6() {
|
public void testNonePredicateEx6() {
|
||||||
|
|
Loading…
Reference in New Issue