You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jc...@apache.org on 2005/05/17 13:56:37 UTC
svn commit: r170581 - in /jakarta/commons/proper/collections/trunk/src:
java/org/apache/commons/collections/iterators/IteratorChain.java
test/org/apache/commons/collections/iterators/TestIteratorChain.java
Author: jcarman
Date: Tue May 17 04:56:36 2005
New Revision: 170581
URL: http://svn.apache.org/viewcvs?rev=170581&view=rev
Log:
Bug 34267: IteratorChain.remove() in combination with FilterIterator
Modified:
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/IteratorChain.java
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java
Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/IteratorChain.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/IteratorChain.java?rev=170581&r1=170580&r2=170581&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/IteratorChain.java (original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/iterators/IteratorChain.java Tue May 17 04:56:36 2005
@@ -40,10 +40,10 @@
* <p>
* 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 @@
/**
* 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 @@
//-----------------------------------------------------------------------
/**
- * 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 @@
}
/**
- * 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 @@
/**
* Get the list of Iterators (unmodifiable)
- *
+ *
* @return the unmodifiable list of iterators added
*/
public List getIterators() {
@@ -177,7 +177,7 @@
/**
* Number of Iterators in the current IteratorChain.
- *
+ *
* @return Iterator count
*/
public int size() {
@@ -188,8 +188,8 @@
* 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 @@
//-----------------------------------------------------------------------
/**
* Return true if any Iterator in the IteratorChain has a remaining element.
- *
+ *
* @return true if elements remain
*/
public boolean hasNext() {
@@ -252,7 +252,7 @@
/**
* 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 @@
}
/**
- * 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 void remove() {
lockChain();
- updateCurrentIterator();
-
+ if( currentIterator == null ) {
+ updateCurrentIterator();
+ }
lastUsedIterator.remove();
}
Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java?rev=170581&r1=170580&r2=170581&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java (original)
+++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/iterators/TestIteratorChain.java Tue May 17 04:56:36 2005
@@ -19,9 +19,13 @@
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 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();
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org