[COLLECTIONS-362] changed filter return value to boolean.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1125466 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brent Worden 2011-05-20 16:56:34 +00:00
parent 501b94143a
commit 07947bd69e
2 changed files with 11 additions and 7 deletions

View File

@ -459,15 +459,19 @@ public class CollectionUtils {
* the collection to get the input from, may be null * the collection to get the input from, may be null
* @param predicate * @param predicate
* the predicate to use as a filter, may be null * the predicate to use as a filter, may be null
* @return true if the collection is modified by this call, false otherwise.
*/ */
public static <T> void filter(Iterable<T> collection, Predicate<? super T> predicate) { public static <T> boolean filter(Iterable<T> collection, Predicate<? super T> predicate) {
boolean result = false;
if (collection != null && predicate != null) { if (collection != null && predicate != null) {
for (Iterator<T> it = collection.iterator(); it.hasNext();) { for (Iterator<T> it = collection.iterator(); it.hasNext();) {
if (!predicate.evaluate(it.next())) { if (!predicate.evaluate(it.next())) {
it.remove(); it.remove();
result = true;
} }
} }
} }
return result;
} }
/** /**

View File

@ -20,8 +20,8 @@ import static junit.framework.Assert.assertFalse;
import static org.apache.commons.collections.functors.EqualPredicate.equalPredicate; import static org.apache.commons.collections.functors.EqualPredicate.equalPredicate;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.util.*; import java.util.*;
@ -873,7 +873,7 @@ public class TestCollectionUtils extends MockTestCase {
ints.add(3); ints.add(3);
ints.add(3); ints.add(3);
Iterable<Integer> iterable = ints; Iterable<Integer> iterable = ints;
CollectionUtils.filter(iterable, EQUALS_TWO); assertTrue(CollectionUtils.filter(iterable, EQUALS_TWO));
assertEquals(1, (int) ints.size()); assertEquals(1, (int) ints.size());
assertEquals(2, (int) ints.get(0)); assertEquals(2, (int) ints.get(0));
} }
@ -881,11 +881,11 @@ public class TestCollectionUtils extends MockTestCase {
@Test @Test
public void filterNullParameters() throws Exception { public void filterNullParameters() throws Exception {
List<Long> longs = Collections.nCopies(4, 10L); List<Long> longs = Collections.nCopies(4, 10L);
CollectionUtils.filter(longs, null); assertFalse(CollectionUtils.filter(longs, null));
assertEquals(4, longs.size()); assertEquals(4, longs.size());
CollectionUtils.filter(null, EQUALS_TWO); assertFalse(CollectionUtils.filter(null, EQUALS_TWO));
assertEquals(4, longs.size()); assertEquals(4, longs.size());
CollectionUtils.filter(null, null); assertFalse(CollectionUtils.filter(null, null));
assertEquals(4, longs.size()); assertEquals(4, longs.size());
} }