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
This commit is contained in:
parent
677f9b4e26
commit
dec0641e2f
|
@ -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>
|
||||
|
|
|
@ -143,6 +143,51 @@ public class ListUtils {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)}.
|
||||
|
|
|
@ -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;
|
||||
|
@ -321,4 +322,44 @@ public class ListUtilsTest extends BulkTest {
|
|||
} 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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue