You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/11/05 11:50:35 UTC
svn commit: r1538935 - in /commons/proper/collections/trunk: ./ src/changes/
src/main/java/org/apache/commons/collections4/ src/site/xdoc/
src/test/java/org/apache/commons/collections4/
Author: tn
Date: Tue Nov 5 10:50:35 2013
New Revision: 1538935
URL: http://svn.apache.org/r1538935
Log:
[COLLECTIONS-488] Added CollectionUtils.matchesAll(Iterable, Predicate), thanks to Josh Cain.
Modified:
commons/proper/collections/trunk/RELEASE-NOTES.txt
commons/proper/collections/trunk/pom.xml
commons/proper/collections/trunk/src/changes/changes.xml
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
commons/proper/collections/trunk/src/site/xdoc/release_4_0.xml
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
Modified: commons/proper/collections/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/RELEASE-NOTES.txt?rev=1538935&r1=1538934&r2=1538935&view=diff
==============================================================================
--- commons/proper/collections/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/collections/trunk/RELEASE-NOTES.txt Tue Nov 5 10:50:35 2013
@@ -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.
Modified: commons/proper/collections/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/pom.xml?rev=1538935&r1=1538934&r2=1538935&view=diff
==============================================================================
--- commons/proper/collections/trunk/pom.xml (original)
+++ commons/proper/collections/trunk/pom.xml Tue Nov 5 10:50:35 2013
@@ -169,6 +169,9 @@
<name>Julien Buret</name>
</contributor>
<contributor>
+ <name>Josh Cain</name>
+ </contributor>
+ <contributor>
<name>Jonathan Carlson</name>
</contributor>
<contributor>
Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1538935&r1=1538934&r2=1538935&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Tue Nov 5 10:50:35 2013
@@ -22,6 +22,10 @@
<body>
<release version="4.0" date="TBA" description="Next release">
+ <action issue="COLLECTIONS-488" dev="tn" type="add" due-to="Josh Cain">
+ Added "CollectionsUtils#matchesAll(Iterable, Predicate)" to test if all elements
+ of a collection match a given predicate.
+ </action>
<action issue="COLLECTIONS-485" dev="tn" type="fix" due-to="Hollis Waite">
Accept wildcard input where possible, e.g. in copy-constructors, Unmodifiable* decorators
and iterators.
Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java?rev=1538935&r1=1538934&r2=1538935&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java Tue Nov 5 10:50:35 2013
@@ -856,6 +856,34 @@ public class CollectionUtils {
}
/**
+ * Answers true if a predicate is true for every element of a
+ * collection.
+ * <p>
+ * A <code>null</code> predicate returns false.<br/>
+ * A <code>null</code> or empty collection returns true.
+ *
+ * @param <C> 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 <C> boolean matchesAll(final Iterable<C> input, final Predicate<? super C> 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.
* <p>
Modified: commons/proper/collections/trunk/src/site/xdoc/release_4_0.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/site/xdoc/release_4_0.xml?rev=1538935&r1=1538934&r2=1538935&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/site/xdoc/release_4_0.xml (original)
+++ commons/proper/collections/trunk/src/site/xdoc/release_4_0.xml Tue Nov 5 10:50:35 2013
@@ -120,6 +120,7 @@ This release is <b>not</b> source or bin
<center><h3>Enhancements</h3></center>
<ul>
+<li>Added CollectionsUtils#matchesAll(Iterable, Predicate) to test if all elements of a collection match a given predicate.</li>
<li>ListUtils#longestCommonSubsequence(...) to get the longest common subsequence of arbitrary lists or CharSequences.</li>
<li>CollectionUtils#forAllButLastDo(Collection, Closure) and forAllButLastDo(Iterator, Closure). Thanks to J. Moldawski.</li>
<li>CollectionUtils#isEqualCollection(Collection, Collection, Equator). Thanks to Matt Lachman.</li>
Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java?rev=1538935&r1=1538934&r2=1538935&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java Tue Nov 5 10:50:35 2013
@@ -1718,4 +1718,27 @@ public class CollectionUtilsTest extends
assertEquals(factorial, permutations.size());
}
+ @Test
+ public void testMatchesAll() {
+ assertFalse(CollectionUtils.matchesAll(null, null));
+ assertFalse(CollectionUtils.matchesAll(collectionA, null));
+
+ Predicate<Integer> lessThanFive = new Predicate<Integer>() {
+ public boolean evaluate(Integer object) {
+ return object < 5;
+ }
+ };
+ assertTrue(CollectionUtils.matchesAll(collectionA, lessThanFive));
+
+ Predicate<Integer> lessThanFour = new Predicate<Integer>() {
+ public boolean evaluate(Integer object) {
+ return object < 4;
+ }
+ };
+ assertFalse(CollectionUtils.matchesAll(collectionA, lessThanFour));
+
+ assertTrue(CollectionUtils.matchesAll(null, lessThanFour));
+ assertTrue(CollectionUtils.matchesAll(emptyCollection, lessThanFour));
+ }
+
}