[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
This commit is contained in:
Thomas Neidhart 2013-11-05 10:50:35 +00:00
parent f0cd4f84b7
commit 894fd838fb
6 changed files with 63 additions and 0 deletions

View File

@ -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.

View File

@ -168,6 +168,9 @@
<contributor>
<name>Julien Buret</name>
</contributor>
<contributor>
<name>Josh Cain</name>
</contributor>
<contributor>
<name>Jonathan Carlson</name>
</contributor>

View File

@ -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.

View File

@ -855,6 +855,34 @@ public class CollectionUtils {
return false;
}
/**
* 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.

View File

@ -120,6 +120,7 @@ This release is <b>not</b> source or binary compatible with v3.x.
<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>

View File

@ -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<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));
}
}