Bug 34267: IteratorChain.remove() in combination with FilterIterator

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@170581 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James W. Carman 2005-05-17 11:56:36 +00:00
parent d65e2938fc
commit 51c9effb79
2 changed files with 56 additions and 19 deletions

View File

@ -280,8 +280,9 @@ public class IteratorChain implements Iterator {
*/ */
public void remove() { public void remove() {
lockChain(); lockChain();
updateCurrentIterator(); if( currentIterator == null ) {
updateCurrentIterator();
}
lastUsedIterator.remove(); lastUsedIterator.remove();
} }

View File

@ -19,9 +19,13 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.LinkedList;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.collections.Predicate;
/** /**
* Tests the IteratorChain class. * Tests the IteratorChain class.
@ -95,6 +99,38 @@ public class TestIteratorChain extends AbstractTestIterator {
} }
} }
public void testRemoveFromFilteredIterator() {
final Predicate myPredicate = new Predicate() {
public boolean evaluate( Object object ) {
Integer i = (Integer) object;
if (i.compareTo(new Integer(4)) < 0)
return true;
return false;
}
};
List list1 = new ArrayList();
List list2 = new ArrayList();
list1.add(new Integer(1));
list1.add(new Integer(2));
list2.add(new Integer(3));
list2.add(new Integer(4)); // will be ignored by the predicate
Iterator it1 = IteratorUtils.filteredIterator(list1.iterator(), myPredicate );
Iterator it2 = IteratorUtils.filteredIterator(list2.iterator(), myPredicate );
Iterator it = IteratorUtils.chainedIterator(it1, it2);
while (it.hasNext()) {
it.next();
it.remove();
}
assertEquals( 0, list1.size() );
assertEquals( 1, list2.size() );
}
public void testRemove() { public void testRemove() {
Iterator iter = (Iterator) makeFullIterator(); Iterator iter = (Iterator) makeFullIterator();