From 99be993a400eea807afe8bc0441c5c95ac88cca2 Mon Sep 17 00:00:00 2001 From: Brent Worden Date: Tue, 24 May 2011 16:11:07 +0000 Subject: [PATCH] COLLECTIONS-306. Added predicated subtract method to CollectionUtils. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1127123 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/CollectionUtils.java | 24 ++++++++++++++++++- .../collections/TestCollectionUtils.java | 18 ++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index b8d892418..a3490f2d0 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -33,6 +33,7 @@ import org.apache.commons.collections.collection.SynchronizedCollection; import org.apache.commons.collections.collection.TransformedCollection; import org.apache.commons.collections.collection.UnmodifiableBoundedCollection; import org.apache.commons.collections.collection.UnmodifiableCollection; +import org.apache.commons.collections.functors.TruePredicate; /** * Provides utility methods and decorators for {@link Collection} instances. @@ -235,10 +236,31 @@ public class CollectionUtils { * @see Collection#removeAll */ public static Collection subtract(final Iterable a, final Iterable b) { + Predicate p = TruePredicate.truePredicate(); + return subtract(a, b, p); + } + + /** + * Returns a new {@link Collection} containing a minus a subset of + * b. Only the elements of b that satisfy the predicate + * condition, p are subtracted from a. + * + * @param a the collection to subtract from, must not be null + * @param b the collection to subtract, must not be null + * @param p the condition used to determine which elements of b are + * subtracted. + * @param the generic type that is able to represent the types contained + * in both input collections. + * @return a new collection with the results + * @see Collection#removeAll + */ + public static Collection subtract(final Iterable a, final Iterable b, final Predicate p) { ArrayList list = new ArrayList(); addAll(list, a); for (O element : b) { - list.remove(element); + if (p.evaluate(element)) { + list.remove(element); + } } return list; } diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index e4a174ac3..a5b3bd42a 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -361,6 +361,24 @@ public class TestCollectionUtils extends MockTestCase { assertNull(freq2.get(1)); } + @Test + public void testSubtractWithPredicate() { + // greater than 3 + Predicate predicate = new Predicate() { + public boolean evaluate(Number n) { + return n.longValue() > 3L; + } + }; + + Collection col = CollectionUtils.subtract(iterableA, collectionC, predicate); + Map freq2 = CollectionUtils.getCardinalityMap(col); + assertEquals(Integer.valueOf(1), freq2.get(1)); + assertEquals(Integer.valueOf(2), freq2.get(2)); + assertEquals(Integer.valueOf(3), freq2.get(3)); + assertEquals(Integer.valueOf(2), freq2.get(4)); + assertNull(freq2.get(5)); + } + @Test public void testIsSubCollectionOfSelf() { assertTrue(CollectionUtils.isSubCollection(collectionA, collectionA));