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:
Stephen Colebourne 2006-02-13 22:12:33 +00:00
parent c56039ade9
commit 7440726c44
8 changed files with 74 additions and 79 deletions

View File

@ -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>PriorityBuffer - now Serializable [36163]</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>
<center><h3>BUG FIXES</h3></center>

View File

@ -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");
* 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
* predicates are true.
* If the array of predicates is empty, then this predicate returns true.
*
* @see org.apache.commons.collections.functors.AllPredicate
*
* @param predicates an array of predicates to check, may not be null
* @return the <code>all</code> predicate
* @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
*/
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
* 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
*
* @param predicates a collection of predicates to check, may not be null
* @return the <code>all</code> predicate
* @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
*/
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
* predicates are true.
* If the array of predicates is empty, then this predicate returns false.
*
* @see org.apache.commons.collections.functors.AnyPredicate
*
* @param predicates an array of predicates to check, may not be null
* @return the <code>any</code> predicate
* @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
*/
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
* 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
*
* @param predicates a collection of predicates to check, may not be null
* @return the <code>any</code> predicate
* @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
*/
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
* predicates are true.
* If the array of predicates is empty, then this predicate returns false.
*
* @see org.apache.commons.collections.functors.OnePredicate
*
* @param predicates an array of predicates to check, may not be null
* @return the <code>one</code> predicate
* @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
*/
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
* 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
*
* @param predicates a collection of predicates to check, may not be null
* @return the <code>one</code> predicate
* @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
*/
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
* predicates are true.
* If the array of predicates is empty, then this predicate returns true.
*
* @see org.apache.commons.collections.functors.NonePredicate
*
* @param predicates an array of predicates to check, may not be null
* @return the <code>none</code> predicate
* @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
*/
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
* 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
*
* @param predicates a collection of predicates to check, may not be null
* @return the <code>none</code> predicate
* @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
*/
public static Predicate nonePredicate(Collection predicates) {

View File

@ -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");
* 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;
/**
* 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
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
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
* @return the <code>all</code> predicate
* @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
*/
public static Predicate getInstance(Predicate[] predicates) {
FunctorUtils.validateMin2(predicates);
FunctorUtils.validate(predicates);
predicates = FunctorUtils.copy(predicates);
return new AllPredicate(predicates);
}
@ -58,7 +60,6 @@ public final class AllPredicate implements Predicate, PredicateDecorator, Serial
* @return the <code>all</code> predicate
* @throws IllegalArgumentException if the predicates 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) {
Predicate[] preds = FunctorUtils.validate(predicates);

View File

@ -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");
* 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;
/**
* 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
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
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
* @return the <code>any</code> predicate
* @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
*/
public static Predicate getInstance(Predicate[] predicates) {
FunctorUtils.validateMin2(predicates);
FunctorUtils.validate(predicates);
predicates = FunctorUtils.copy(predicates);
return new AnyPredicate(predicates);
}
@ -58,7 +60,6 @@ public final class AnyPredicate implements Predicate, PredicateDecorator, Serial
* @return the <code>all</code> predicate
* @throws IllegalArgumentException if the predicates 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) {
Predicate[] preds = FunctorUtils.validate(predicates);

View File

@ -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");
* 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$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
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.
*
@ -98,10 +79,6 @@ class FunctorUtils {
if (predicates == 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
Predicate[] preds = new Predicate[predicates.size()];
int i = 0;

View File

@ -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");
* 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;
/**
* 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
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
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
* @return the <code>any</code> predicate
* @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
*/
public static Predicate getInstance(Predicate[] predicates) {
FunctorUtils.validateMin2(predicates);
FunctorUtils.validate(predicates);
predicates = FunctorUtils.copy(predicates);
return new NonePredicate(predicates);
}
@ -58,7 +60,6 @@ public final class NonePredicate implements Predicate, PredicateDecorator, Seria
* @return the <code>one</code> predicate
* @throws IllegalArgumentException if the predicates 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) {
Predicate[] preds = FunctorUtils.validate(predicates);

View File

@ -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");
* 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;
/**
* 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
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
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
* @return the <code>any</code> predicate
* @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
*/
public static Predicate getInstance(Predicate[] predicates) {
FunctorUtils.validateMin2(predicates);
FunctorUtils.validate(predicates);
predicates = FunctorUtils.copy(predicates);
return new OnePredicate(predicates);
}
@ -58,7 +60,6 @@ public final class OnePredicate implements Predicate, PredicateDecorator, Serial
* @return the <code>one</code> predicate
* @throws IllegalArgumentException if the predicates 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) {
Predicate[] preds = FunctorUtils.validate(predicates);

View File

@ -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");
* you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ import junit.textui.TestRunner;
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
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());
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() {
@ -276,12 +285,7 @@ public class TestPredicateUtils extends junit.framework.TestCase {
}
public void testAllPredicateEx5() {
try {
PredicateUtils.allPredicate(Collections.EMPTY_LIST);
} catch (IllegalArgumentException ex) {
return;
}
fail();
PredicateUtils.allPredicate(Collections.EMPTY_LIST);
}
public void testAllPredicateEx6() {
@ -347,6 +351,14 @@ public class TestPredicateUtils extends junit.framework.TestCase {
coll.add(PredicateUtils.falsePredicate());
coll.add(PredicateUtils.falsePredicate());
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() {
@ -386,12 +398,7 @@ public class TestPredicateUtils extends junit.framework.TestCase {
}
public void testAnyPredicateEx5() {
try {
PredicateUtils.anyPredicate(Collections.EMPTY_LIST);
} catch (IllegalArgumentException ex) {
return;
}
fail();
PredicateUtils.anyPredicate(Collections.EMPTY_LIST);
}
public void testAnyPredicateEx6() {
@ -461,6 +468,14 @@ public class TestPredicateUtils extends junit.framework.TestCase {
coll.add(PredicateUtils.falsePredicate());
coll.add(PredicateUtils.falsePredicate());
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() {
@ -500,12 +515,7 @@ public class TestPredicateUtils extends junit.framework.TestCase {
}
public void testOnePredicateEx5() {
try {
PredicateUtils.onePredicate(Collections.EMPTY_LIST);
} catch (IllegalArgumentException ex) {
return;
}
fail();
PredicateUtils.onePredicate(Collections.EMPTY_LIST);
}
public void testOnePredicateEx6() {
@ -571,6 +581,14 @@ public class TestPredicateUtils extends junit.framework.TestCase {
coll.add(PredicateUtils.falsePredicate());
coll.add(PredicateUtils.falsePredicate());
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() {
@ -610,12 +628,7 @@ public class TestPredicateUtils extends junit.framework.TestCase {
}
public void testNonePredicateEx5() {
try {
PredicateUtils.nonePredicate(Collections.EMPTY_LIST);
} catch (IllegalArgumentException ex) {
return;
}
fail();
PredicateUtils.nonePredicate(Collections.EMPTY_LIST);
}
public void testNonePredicateEx6() {