diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index 29d2461b9..b8d892418 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -454,20 +454,24 @@ public class CollectionUtils { * predicate returns false, remove the element. *

* If the input collection or predicate is null, there is no change made. - * + * * @param collection * the collection to get the input from, may be null * @param predicate * the predicate to use as a filter, may be null + * @return true if the collection is modified by this call, false otherwise. */ - public static void filter(Iterable collection, Predicate predicate) { + public static boolean filter(Iterable collection, Predicate predicate) { + boolean result = false; if (collection != null && predicate != null) { for (Iterator it = collection.iterator(); it.hasNext();) { if (!predicate.evaluate(it.next())) { it.remove(); + result = true; } } } + return result; } /** diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index 6473b22ee..e4a174ac3 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -20,8 +20,8 @@ import static junit.framework.Assert.assertFalse; import static org.apache.commons.collections.functors.EqualPredicate.equalPredicate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.*; @@ -873,7 +873,7 @@ public class TestCollectionUtils extends MockTestCase { ints.add(3); ints.add(3); Iterable iterable = ints; - CollectionUtils.filter(iterable, EQUALS_TWO); + assertTrue(CollectionUtils.filter(iterable, EQUALS_TWO)); assertEquals(1, (int) ints.size()); assertEquals(2, (int) ints.get(0)); } @@ -881,11 +881,11 @@ public class TestCollectionUtils extends MockTestCase { @Test public void filterNullParameters() throws Exception { List longs = Collections.nCopies(4, 10L); - CollectionUtils.filter(longs, null); + assertFalse(CollectionUtils.filter(longs, null)); assertEquals(4, longs.size()); - CollectionUtils.filter(null, EQUALS_TWO); + assertFalse(CollectionUtils.filter(null, EQUALS_TWO)); assertEquals(4, longs.size()); - CollectionUtils.filter(null, null); + assertFalse(CollectionUtils.filter(null, null)); assertEquals(4, longs.size()); }