PR: COLLECTIONS-360

Prevent an NPE in FilterListIterator.next() and FilterListIterator.previous()


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1076034 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jochen Wiedmann 2011-03-01 22:10:10 +00:00
parent 152588c286
commit 0145c16a6f
3 changed files with 25 additions and 0 deletions

View File

@ -350,6 +350,9 @@
<contributor>
<name>Serhiy Yevtushenko</name>
</contributor>
<contributor>
<name>Sai Zhang</name>
</contributor>
<contributor>
<name>Jason van Zyl</name>
</contributor>

View File

@ -227,6 +227,9 @@ public class FilterListIterator<E> implements ListIterator<E> {
clearNextObject();
}
if (iterator == null) {
return false;
}
while (iterator.hasNext()) {
E object = iterator.next();
if (predicate.evaluate(object)) {
@ -256,6 +259,9 @@ public class FilterListIterator<E> implements ListIterator<E> {
clearPreviousObject();
}
if (iterator == null) {
return false;
}
while (iterator.hasPrevious()) {
E object = iterator.previous();
if (predicate.evaluate(object)) {

View File

@ -17,12 +17,16 @@
package org.apache.commons.collections.iterators;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import junit.framework.TestCase;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.list.GrowthList;
import org.junit.Assert;
/**
* Tests the FilterListIterator class.
@ -279,6 +283,18 @@ public class TestFilterListIterator extends TestCase {
assertEquals(expected.previous(), filtered.previous());
}
/**
* Test for {@link https://issues.apache.org/jira/browse/COLLECTIONS-360 COLLECTIONS-360}.
*/
public void testCollections360() throws Throwable {
Collection<Predicate<Object>> var7 = new GrowthList<Predicate<Object>>();
Predicate<Object> var9 = PredicateUtils.anyPredicate(var7);
FilterListIterator<Object> var13 = new FilterListIterator<Object>(var9);
Assert.assertFalse(var13.hasNext());
FilterListIterator<Object> var14 = new FilterListIterator<Object>(var9);
Assert.assertFalse(var14.hasPrevious());
}
// Utilities
private void walkForward(ListIterator<?> expected, ListIterator<?> testing) {