diff --git a/src/java/org/apache/commons/collections/iterators/IteratorChain.java b/src/java/org/apache/commons/collections/iterators/IteratorChain.java index 0e08fbac0..20651ed9c 100644 --- a/src/java/org/apache/commons/collections/iterators/IteratorChain.java +++ b/src/java/org/apache/commons/collections/iterators/IteratorChain.java @@ -40,10 +40,10 @@ import org.apache.commons.collections.list.UnmodifiableList; *
* NOTE: As from version 3.0, the IteratorChain may contain no * iterators. In this case the class will function as an empty iterator. - * + * * @since Commons Collections 2.1 * @version $Revision$ $Date$ - * + * * @author Morgan Delagrange * @author Stephen Colebourne */ @@ -80,7 +80,7 @@ public class IteratorChain implements Iterator { /** * Construct an IteratorChain with a single Iterator. - * + * * @param iterator first Iterator in the IteratorChain * @throws NullPointerException if the iterator is null */ @@ -135,8 +135,8 @@ public class IteratorChain implements Iterator { //----------------------------------------------------------------------- /** - * Add an Iterator to the end of the chain - * + * Add an Iterator to the end of the chain + * * @param iterator Iterator to add * @throws IllegalStateException if I've already started iterating * @throws NullPointerException if the iterator is null @@ -150,8 +150,8 @@ public class IteratorChain implements Iterator { } /** - * Set the Iterator at the given index - * + * Set the Iterator at the given index + * * @param index index of the Iterator to replace * @param iterator Iterator to place at the given index * @throws IndexOutOfBoundsException if index < 0 or index > size() @@ -168,7 +168,7 @@ public class IteratorChain implements Iterator { /** * Get the list of Iterators (unmodifiable) - * + * * @return the unmodifiable list of iterators added */ public List getIterators() { @@ -177,7 +177,7 @@ public class IteratorChain implements Iterator { /** * Number of Iterators in the current IteratorChain. - * + * * @return Iterator count */ public int size() { @@ -188,8 +188,8 @@ public class IteratorChain implements Iterator { * Determine if modifications can still be made to the IteratorChain. * IteratorChains cannot be modified once they have executed a method * from the Iterator interface. - * - * @return true if IteratorChain cannot be modified, false if it can + * + * @return true if IteratorChain cannot be modified, false if it can */ public boolean isLocked() { return isLocked; @@ -239,7 +239,7 @@ public class IteratorChain implements Iterator { //----------------------------------------------------------------------- /** * Return true if any Iterator in the IteratorChain has a remaining element. - * + * * @return true if elements remain */ public boolean hasNext() { @@ -252,7 +252,7 @@ public class IteratorChain implements Iterator { /** * Returns the next Object of the current Iterator - * + * * @return Object from the current Iterator * @throws java.util.NoSuchElementException if all the Iterators are exhausted */ @@ -265,13 +265,13 @@ public class IteratorChain implements Iterator { } /** - * Removes from the underlying collection the last element + * Removes from the underlying collection the last element * returned by the Iterator. As with next() and hasNext(), * this method calls remove() on the underlying Iterator. - * Therefore, this method may throw an + * Therefore, this method may throw an * UnsupportedOperationException if the underlying - * Iterator does not support this method. - * + * Iterator does not support this method. + * * @throws UnsupportedOperationException * if the remove operator is not supported by the underlying Iterator * @throws IllegalStateException @@ -280,8 +280,9 @@ public class IteratorChain implements Iterator { */ public void remove() { lockChain(); - updateCurrentIterator(); - + if( currentIterator == null ) { + updateCurrentIterator(); + } lastUsedIterator.remove(); } diff --git a/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java b/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java index 3f29fd6a4..f8b04fd2e 100644 --- a/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java +++ b/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java @@ -19,9 +19,13 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; +import java.util.LinkedList; import junit.framework.Test; 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. @@ -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() { Iterator iter = (Iterator) makeFullIterator();