From dec0641e2f189c89e931b4ab59a06384582316b1 Mon Sep 17 00:00:00 2001 From: Brent Worden Date: Sat, 25 Aug 2012 03:59:34 +0000 Subject: [PATCH] COLLECTIONS-405 added select and selectRejected methods to ListUtils git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1377196 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 3 ++ .../apache/commons/collections/ListUtils.java | 45 +++++++++++++++++++ .../commons/collections/ListUtilsTest.java | 43 +++++++++++++++++- 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 14b3f3280..e6b0b5bda 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -72,6 +72,9 @@ Added "PassiveExpiringMap" map decorator. + + Added "ListUtils#select" and "ListUtils#selectRejected" methods. + diff --git a/src/main/java/org/apache/commons/collections/ListUtils.java b/src/main/java/org/apache/commons/collections/ListUtils.java index 86736f790..30074e682 100644 --- a/src/main/java/org/apache/commons/collections/ListUtils.java +++ b/src/main/java/org/apache/commons/collections/ListUtils.java @@ -143,6 +143,51 @@ public class ListUtils { return result; } + /** + * Selects all elements from input collection which match the given + * predicate into an output list. + *

+ * A null predicate matches no elements. + * + * @param inputCollection + * the collection to get the input from, may not be null + * @param predicate + * the predicate to use, may be null + * @return the elements matching the predicate (new list) + * @throws NullPointerException + * if the input list is null + * + * @since 4.0 + * @see CollectionUtils#select(Collection, Predicate) + */ + public static List select(Collection inputCollection, + Predicate predicate) { + return CollectionUtils.select(inputCollection, predicate, new ArrayList(inputCollection.size())); + } + + /** + * Selects all elements from inputCollection which don't match the given + * predicate into an output collection. + *

+ * If the input predicate is null, the result is an empty + * list. + * + * @param inputCollection + * the collection to get the input from, may not be null + * @param predicate + * the predicate to use, may be null + * @return the elements not matching the predicate (new list) + * @throws NullPointerException + * if the input collection is null + * + * @since 4.0 + * @see CollectionUtils#selectRejected(Collection, Predicate) + */ + public static List selectRejected(Collection inputCollection, + Predicate predicate) { + return CollectionUtils.selectRejected(inputCollection, predicate, new ArrayList(inputCollection.size())); + } + /** * Tests two lists for value-equality as per the equality contract in * {@link java.util.List#equals(java.lang.Object)}. diff --git a/src/test/java/org/apache/commons/collections/ListUtilsTest.java b/src/test/java/org/apache/commons/collections/ListUtilsTest.java index ff364782e..a19a0e0bd 100644 --- a/src/test/java/org/apache/commons/collections/ListUtilsTest.java +++ b/src/test/java/org/apache/commons/collections/ListUtilsTest.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; import junit.framework.Assert; @@ -320,5 +321,45 @@ public class ListUtilsTest extends BulkTest { Assert.fail("failed to check for size argument"); } catch (IllegalArgumentException e) {} - } + } + + private static Predicate EQUALS_TWO = new Predicate() { + public boolean evaluate(Number input) { + return (input.intValue() == 2); + } + }; + + public void testSelect() { + List list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + // Ensure that the collection is the input type or a super type + List output1 = ListUtils.select(list, EQUALS_TWO); + List output2 = ListUtils.select(list, EQUALS_TWO); + HashSet output3 = CollectionUtils.select(list, EQUALS_TWO, new HashSet()); + Assert.assertTrue(CollectionUtils.isEqualCollection(output1, output3)); + Assert.assertEquals(4, list.size()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(2, output2.iterator().next()); + } + + public void testSelectRejected() { + List list = new ArrayList(); + list.add(1L); + list.add(2L); + list.add(3L); + list.add(4L); + List output1 = ListUtils.selectRejected(list, EQUALS_TWO); + List output2 = ListUtils.selectRejected(list, EQUALS_TWO); + HashSet output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet()); + Assert.assertTrue(CollectionUtils.isEqualCollection(output1, output2)); + Assert.assertTrue(CollectionUtils.isEqualCollection(output1, output3)); + Assert.assertEquals(4, list.size()); + Assert.assertEquals(3, output1.size()); + Assert.assertTrue(output1.contains(1L)); + Assert.assertTrue(output1.contains(3L)); + Assert.assertTrue(output1.contains(4L)); + } }