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 2011/05/24 18:11:07 UTC
svn commit: r1127123 - in /commons/proper/collections/trunk/src:
java/org/apache/commons/collections/CollectionUtils.java
test/org/apache/commons/collections/TestCollectionUtils.java
Author: brentworden
Date: Tue May 24 16:11:07 2011
New Revision: 1127123
URL: http://svn.apache.org/viewvc?rev=1127123&view=rev
Log:
COLLECTIONS-306. Added predicated subtract method to CollectionUtils.
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java?rev=1127123&r1=1127122&r2=1127123&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java Tue May 24 16:11:07 2011
@@ -33,6 +33,7 @@ import org.apache.commons.collections.co
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 <O> Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b) {
+ Predicate<O> p = TruePredicate.truePredicate();
+ return subtract(a, b, p);
+ }
+
+ /**
+ * Returns a new {@link Collection} containing <i>a</i> minus a subset of
+ * <i>b</i>. Only the elements of <i>b</i> that satisfy the predicate
+ * condition, <i>p</i> are subtracted from <i>a</i>.
+ *
+ * @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 <i>b</i> are
+ * subtracted.
+ * @param <O> 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 <O> Collection<O> subtract(final Iterable<? extends O> a, final Iterable<? extends O> b, final Predicate<O> p) {
ArrayList<O> list = new ArrayList<O>();
addAll(list, a);
for (O element : b) {
- list.remove(element);
+ if (p.evaluate(element)) {
+ list.remove(element);
+ }
}
return list;
}
Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java?rev=1127123&r1=1127122&r2=1127123&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java (original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java Tue May 24 16:11:07 2011
@@ -362,6 +362,24 @@ public class TestCollectionUtils extends
}
@Test
+ public void testSubtractWithPredicate() {
+ // greater than 3
+ Predicate<Number> predicate = new Predicate<Number>() {
+ public boolean evaluate(Number n) {
+ return n.longValue() > 3L;
+ }
+ };
+
+ Collection<Number> col = CollectionUtils.subtract(iterableA, collectionC, predicate);
+ Map<Number, Integer> 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));
assertTrue(CollectionUtils.isSubCollection(collectionB, collectionB));