From 894fd838fb695d794fbd28cc66c82b80a9688c91 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Tue, 5 Nov 2013 10:50:35 +0000 Subject: [PATCH] [COLLECTIONS-488] Added CollectionUtils.matchesAll(Iterable, Predicate), thanks to Josh Cain. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1538935 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE-NOTES.txt | 4 +++ pom.xml | 3 ++ src/changes/changes.xml | 4 +++ .../commons/collections4/CollectionUtils.java | 28 +++++++++++++++++++ src/site/xdoc/release_4_0.xml | 1 + .../collections4/CollectionUtilsTest.java | 23 +++++++++++++++ 6 files changed, 63 insertions(+) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 815035e37..21f9d585f 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -46,6 +46,8 @@ Major changes since 3.2.1 Changes since 4.0-alpha1 ------------------------ + o [COLLECTIONS-488] Added "CollectionsUtils#matchesAll(Iterable, Predicate)" to test if all elements + of a collection match a given predicate. Thanks to Josh Cain. o [COLLECTIONS-485] Accept wildcard input where possible, e.g. in copy-constructors, Unmodifiable* decorators and iterators. Thanks to Hollis Waite. o [COLLECTIONS-481] No collision detection/resolution was performed when calling "CompositeSet#addComposited(...)" @@ -112,6 +114,8 @@ New classes New methods in *Utils --------------------- + o [COLLECTIONS-488] Added "CollectionsUtils#matchesAll(Iterable, Predicate)" to test if all elements + of a collection match a given predicate. Thanks to Josh Cain. o [COLLECTIONS-456] ListUtils#longestCommonSubsequence(...) to get the longest common subsequence of arbitrary lists or CharSequences. o [COLLECTIONS-450] CollectionUtils#forAllButLastDo(Collection, Closure) and forAllButLastDo(Iterator, Closure). Thanks to J. Moldawski. o [COLLECTIONS-446] CollectionUtils#isEqualCollection(Collection, Collection, Equator). Thanks to Matt Lachman. diff --git a/pom.xml b/pom.xml index 1dbe0be04..8abad2b02 100644 --- a/pom.xml +++ b/pom.xml @@ -168,6 +168,9 @@ Julien Buret + + Josh Cain + Jonathan Carlson diff --git a/src/changes/changes.xml b/src/changes/changes.xml index afb57308b..ce80f19b3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,10 @@ + + Added "CollectionsUtils#matchesAll(Iterable, Predicate)" to test if all elements + of a collection match a given predicate. + Accept wildcard input where possible, e.g. in copy-constructors, Unmodifiable* decorators and iterators. diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index 1a9d9b58f..18b60bc04 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -855,6 +855,34 @@ public class CollectionUtils { return false; } + /** + * Answers true if a predicate is true for every element of a + * collection. + *

+ * A null predicate returns false.
+ * A null or empty collection returns true. + * + * @param the type of object the {@link Iterable} contains + * @param input the {@link Iterable} to get the input from, may be null + * @param predicate the predicate to use, may be null + * @return true if every element of the collection matches the predicate or if the + * collection is empty, false otherwise + */ + public static boolean matchesAll(final Iterable input, final Predicate predicate) { + if (predicate == null) { + return false; + } + + if (input != null) { + for (final C o : input) { + if (!predicate.evaluate(o)) { + return false; + } + } + } + return true; + } + /** * Selects all elements from input collection which match the given * predicate into an output collection. diff --git a/src/site/xdoc/release_4_0.xml b/src/site/xdoc/release_4_0.xml index f552a20cf..7ec744714 100644 --- a/src/site/xdoc/release_4_0.xml +++ b/src/site/xdoc/release_4_0.xml @@ -120,6 +120,7 @@ This release is not source or binary compatible with v3.x.

Enhancements

    +
  • Added CollectionsUtils#matchesAll(Iterable, Predicate) to test if all elements of a collection match a given predicate.
  • ListUtils#longestCommonSubsequence(...) to get the longest common subsequence of arbitrary lists or CharSequences.
  • CollectionUtils#forAllButLastDo(Collection, Closure) and forAllButLastDo(Iterator, Closure). Thanks to J. Moldawski.
  • CollectionUtils#isEqualCollection(Collection, Collection, Equator). Thanks to Matt Lachman.
  • diff --git a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java index 2e18ed350..0a6f3e0fe 100644 --- a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java @@ -1718,4 +1718,27 @@ public class CollectionUtilsTest extends MockTestCase { assertEquals(factorial, permutations.size()); } + @Test + public void testMatchesAll() { + assertFalse(CollectionUtils.matchesAll(null, null)); + assertFalse(CollectionUtils.matchesAll(collectionA, null)); + + Predicate lessThanFive = new Predicate() { + public boolean evaluate(Integer object) { + return object < 5; + } + }; + assertTrue(CollectionUtils.matchesAll(collectionA, lessThanFive)); + + Predicate lessThanFour = new Predicate() { + public boolean evaluate(Integer object) { + return object < 4; + } + }; + assertFalse(CollectionUtils.matchesAll(collectionA, lessThanFour)); + + assertTrue(CollectionUtils.matchesAll(null, lessThanFour)); + assertTrue(CollectionUtils.matchesAll(emptyCollection, lessThanFour)); + } + }