You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2012/08/25 05:59:35 UTC

svn commit: r1377196 - in /commons/proper/collections/trunk/src: changes/changes.xml main/java/org/apache/commons/collections/ListUtils.java test/java/org/apache/commons/collections/ListUtilsTest.java

Author: brentworden
Date: Sat Aug 25 03:59:34 2012
New Revision: 1377196

URL: http://svn.apache.org/viewvc?rev=1377196&view=rev
Log:
COLLECTIONS-405 added select and selectRejected methods to ListUtils

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.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=1377196&r1=1377195&r2=1377196&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Sat Aug 25 03:59:34 2012
@@ -72,6 +72,9 @@
     <action issue="COLLECTIONS-241" dev="brentworden" type="add" due-to="Elifarley Callado Coelho">
       Added "PassiveExpiringMap" map decorator.
     </action>
+    <action issue="COLLECTIONS-405" dev="brentworden" type="add" due-to="Adam Dyga">
+      Added "ListUtils#select" and "ListUtils#selectRejected" methods.
+    </action>
   </release>
   </body>
 </document>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java?rev=1377196&r1=1377195&r2=1377196&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ListUtils.java Sat Aug 25 03:59:34 2012
@@ -144,6 +144,51 @@ public class ListUtils {
     }
 
     /**
+     * Selects all elements from input collection which match the given
+     * predicate into an output list.
+     * <p>
+     * A <code>null</code> 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 <O> List<O> select(Collection<? extends O> inputCollection,
+            Predicate<? super O> predicate) {
+        return CollectionUtils.select(inputCollection, predicate, new ArrayList<O>(inputCollection.size()));
+    }
+
+    /**
+     * Selects all elements from inputCollection which don't match the given
+     * predicate into an output collection.
+     * <p>
+     * If the input predicate is <code>null</code>, 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 <b>not</b> matching the predicate (new list)
+     * @throws NullPointerException
+     *             if the input collection is null
+     *          
+     * @since 4.0
+     * @see CollectionUtils#selectRejected(Collection, Predicate)
+     */
+    public static <O> List<O> selectRejected(Collection<? extends O> inputCollection,
+            Predicate<? super O> predicate) {
+        return CollectionUtils.selectRejected(inputCollection, predicate, new ArrayList<O>(inputCollection.size()));
+    }
+
+    /**
      * Tests two lists for value-equality as per the equality contract in
      * {@link java.util.List#equals(java.lang.Object)}.
      * <p>

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java?rev=1377196&r1=1377195&r2=1377196&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/ListUtilsTest.java Sat Aug 25 03:59:34 2012
@@ -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 BulkT
             Assert.fail("failed to check for size argument");
         } catch (IllegalArgumentException e) {}
         
-    }    
+    }
+    
+    private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() {
+        public boolean evaluate(Number input) {
+            return (input.intValue() == 2);
+        }
+    };
+
+    public void testSelect() {
+        List<Integer> list = new ArrayList<Integer>();
+        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<Integer> output1 = ListUtils.select(list, EQUALS_TWO);
+        List<Number> output2 = ListUtils.<Number>select(list, EQUALS_TWO);
+        HashSet<Number> output3 = CollectionUtils.select(list, EQUALS_TWO, new HashSet<Number>());
+        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<Long> list = new ArrayList<Long>();
+        list.add(1L);
+        list.add(2L);
+        list.add(3L);
+        list.add(4L);
+        List<Long> output1 = ListUtils.selectRejected(list, EQUALS_TWO);
+        List<? extends Number> output2 = ListUtils.selectRejected(list, EQUALS_TWO);
+        HashSet<Number> output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet<Number>());
+        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));
+    }
 }