Author: tn Date: Thu Mar 7 21:03:01 2013 New Revision: 1454100 URL: http://svn.apache.org/r1454100 Log: [COLLECTIONS-361] Added CollectionUtils.filterInverse. Thanks to Jean-Noel Rouvignac for patch.
Modified: commons/proper/collections/trunk/src/changes/changes.xml commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java Modified: commons/proper/collections/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1454100&r1=1454099&r2=1454100&view=diff ============================================================================== --- commons/proper/collections/trunk/src/changes/changes.xml (original) +++ commons/proper/collections/trunk/src/changes/changes.xml Thu Mar 7 21:03:01 2013 @@ -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> Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java?rev=1454100&r1=1454099&r2=1454100&view=diff ============================================================================== --- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java (original) +++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/CollectionUtils.java Thu Mar 7 21:03:01 2013 @@ -715,6 +715,24 @@ public class CollectionUtils { } /** + * 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> * If the input collection or transformer is null, there is no change made. Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java?rev=1454100&r1=1454099&r2=1454100&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/CollectionUtilsTest.java Thu Mar 7 21:03:01 2013 @@ -998,6 +998,32 @@ public class CollectionUtilsTest extends } @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)); assertEquals(0, CollectionUtils.countMatches(iterableA, null));