diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index 2cbdf78ff..becc15553 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.27 2003/01/25 11:29:37 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.28 2003/01/25 11:40:26 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -77,7 +77,7 @@ import org.apache.commons.collections.iterators.EnumerationIterator; * A set of {@link Collection} related utility methods. * * @since Commons Collections 1.0 - * @version $Revision: 1.27 $ $Date: 2003/01/25 11:29:37 $ + * @version $Revision: 1.28 $ $Date: 2003/01/25 11:40:26 $ * * @author Rodney Waldhoff * @author Paul Jack @@ -449,6 +449,26 @@ public class CollectionUtils { return count; } + /** + * Answers true if a predicate is true for at least one element of a collection. + *

+ * A null collection or predicate returns false. + * + * @param collection the collection to get the input from, may be null + * @param predicate the predicate to use, may be null + * @return true if at least one element of the collection matches the predicate + */ + public static boolean exists(Collection collection, Predicate predicate) { + if (collection != null && predicate != null) { + for (Iterator it = collection.iterator(); it.hasNext();) { + if (predicate.evaluate(it.next())) { + return true; + } + } + } + return false; + } + /** * Selects all elements from input collection which match the given predicate * into an output collection. diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index 017560db9..8ce1ca36f 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -1,7 +1,7 @@ /* - * $Id: TestCollectionUtils.java,v 1.11 2003/01/25 11:31:12 scolebourne Exp $ - * $Revision: 1.11 $ - * $Date: 2003/01/25 11:31:12 $ + * $Id: TestCollectionUtils.java,v 1.12 2003/01/25 11:40:26 scolebourne Exp $ + * $Revision: 1.12 $ + * $Date: 2003/01/25 11:40:26 $ * * ==================================================================== * @@ -66,7 +66,7 @@ import java.util.*; /** * @author Rodney Waldhoff - * @version $Revision: 1.11 $ $Date: 2003/01/25 11:31:12 $ + * @version $Revision: 1.12 $ $Date: 2003/01/25 11:40:26 $ */ public class TestCollectionUtils extends TestCase { public TestCollectionUtils(String testName) { @@ -459,6 +459,21 @@ public class TestCollectionUtils extends TestCase { assertEquals(0, CollectionUtils.countMatches(null, null)); } + public void testExists() { + List list = new ArrayList(); + assertEquals(false, CollectionUtils.exists(null, null)); + assertEquals(false, CollectionUtils.exists(list, null)); + assertEquals(false, CollectionUtils.exists(null, EQUALS_TWO)); + assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO)); + list.add("One"); + list.add("Three"); + list.add("Four"); + assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO)); + + list.add("Two"); + assertEquals(true, CollectionUtils.exists(list, EQUALS_TWO)); + } + public void testSelect() { List list = new ArrayList(); list.add("One");