[COLLECTIONS-361] Added CollectionUtils.filterInverse. Thanks to Jean-Noel Rouvignac for patch.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1454100 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d5d29b625f
commit
8b9a79d087
|
@ -22,6 +22,9 @@
|
|||
<body>
|
||||
|
||||
<release version="4.0" date="TBA" description="Next release">
|
||||
<action issue="COLLECTIONS-361" dev="tn" type="add" due-to="Jean-Noel Rouvignac">
|
||||
Add method "CollectionUtils#filterInverse(Iterable, Predicate)".
|
||||
</action>
|
||||
<action issue="COLLECTIONS-372" dev="tn" type="change">
|
||||
TransformingComparator now supports different types for its input/output values.
|
||||
</action>
|
||||
|
|
|
@ -714,6 +714,24 @@ public class CollectionUtils {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the collection by applying a Predicate to each element. If the
|
||||
* predicate returns true, remove the element.
|
||||
* <p>
|
||||
* This is equivalent to <pre>filter(collection, PredicateUtils.notPredicate(predicate))</pre>
|
||||
* if predicate is != null.
|
||||
* <p>
|
||||
* If the input collection or predicate is null, there is no change made.
|
||||
*
|
||||
* @param <T> the type of object the {@link Iterable} contains
|
||||
* @param collection the collection to get the input from, may be null
|
||||
* @param predicate the predicate to use as a filter, may be null
|
||||
* @return true if the collection is modified by this call, false otherwise.
|
||||
*/
|
||||
public static <T> boolean filterInverse(final Iterable<T> collection, final Predicate<? super T> predicate) {
|
||||
return filter(collection, predicate == null ? null : PredicateUtils.notPredicate(predicate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the collection by applying a Transformer to each element.
|
||||
* <p>
|
||||
|
|
|
@ -997,6 +997,32 @@ public class CollectionUtilsTest extends MockTestCase {
|
|||
assertEquals(4, longs.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterInverse() {
|
||||
List<Integer> ints = new ArrayList<Integer>();
|
||||
ints.add(1);
|
||||
ints.add(2);
|
||||
ints.add(3);
|
||||
ints.add(3);
|
||||
Iterable<Integer> iterable = ints;
|
||||
assertTrue(CollectionUtils.filterInverse(iterable, EQUALS_TWO));
|
||||
assertEquals(3, ints.size());
|
||||
assertEquals(1, (int) ints.get(0));
|
||||
assertEquals(3, (int) ints.get(1));
|
||||
assertEquals(3, (int) ints.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterInverseNullParameters() throws Exception {
|
||||
List<Long> longs = Collections.nCopies(4, 10L);
|
||||
assertFalse(CollectionUtils.filterInverse(longs, null));
|
||||
assertEquals(4, longs.size());
|
||||
assertFalse(CollectionUtils.filterInverse(null, EQUALS_TWO));
|
||||
assertEquals(4, longs.size());
|
||||
assertFalse(CollectionUtils.filterInverse(null, null));
|
||||
assertEquals(4, longs.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void countMatches() {
|
||||
assertEquals(4, CollectionUtils.countMatches(iterableB, EQUALS_TWO));
|
||||
|
|
Loading…
Reference in New Issue