You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Yandell (JIRA)" <ji...@apache.org> on 2008/03/19 07:52:24 UTC

[jira] Issue Comment Edited: (COLLECTIONS-111) IteratorChain skips over elements in iterator

    [ https://issues.apache.org/jira/browse/COLLECTIONS-111?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12526533#action_12526533 ] 

bayard edited comment on COLLECTIONS-111 at 3/18/08 11:52 PM:
---------------------------------------------------------------------

Here's the 2.1.1 to trunk diff for that method:

{code:java}
     protected void updateCurrentIterator() {
         if (currentIterator == null) {
-            currentIterator = (Iterator) iteratorChain.get(0);
+            if (iteratorChain.isEmpty()) {
+                currentIterator = EmptyIterator.INSTANCE;
+            } else {
+                currentIterator = (Iterator) iteratorChain.get(0);
+            }
             // set last used iterator here, in case the user calls remove
             // before calling hasNext() or next() (although they shouldn't)
             lastUsedIterator = currentIterator;
-            return;
         }

-        if (currentIteratorIndex == (iteratorChain.size() - 1)) {
-            return;
-        }
-
-        while (currentIterator.hasNext() == false) {
-            ++currentIteratorIndex;
+        while (currentIterator.hasNext() == false && currentIteratorIndex < iteratorChain.size() - 1) {
+            currentIteratorIndex++;
             currentIterator = (Iterator) iteratorChain.get(currentIteratorIndex);
-
-            if (currentIteratorIndex == (iteratorChain.size() - 1)) {
-                return;
-            }
         }
     }
{code}

      was (Author: bayard):
    Here's the 2.1.1 to trunk diff for that method:

********
     protected void updateCurrentIterator() {
         if (currentIterator == null) {
-            currentIterator = (Iterator) iteratorChain.get(0);
+            if (iteratorChain.isEmpty()) {
+                currentIterator = EmptyIterator.INSTANCE;
+            } else {
+                currentIterator = (Iterator) iteratorChain.get(0);
+            }
             // set last used iterator here, in case the user calls remove
             // before calling hasNext() or next() (although they shouldn't)
             lastUsedIterator = currentIterator;
-            return;
         }

-        if (currentIteratorIndex == (iteratorChain.size() - 1)) {
-            return;
-        }
-
-        while (currentIterator.hasNext() == false) {
-            ++currentIteratorIndex;
+        while (currentIterator.hasNext() == false && currentIteratorIndex < iteratorChain.size() - 1) {
+            currentIteratorIndex++;
             currentIterator = (Iterator) iteratorChain.get(currentIteratorIndex);
-
-            if (currentIteratorIndex == (iteratorChain.size() - 1)) {
-                return;
-            }
         }
     }
***********
  
> IteratorChain skips over elements in iterator
> ---------------------------------------------
>
>                 Key: COLLECTIONS-111
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-111
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Iterator
>    Affects Versions: 3.1
>         Environment: Operating System: Windows XP
> Platform: PC
>            Reporter: Jonathan Giles
>             Fix For: 3.3
>
>
> Hi there,
> When using the IteratorChain class to add multiple iterators, it appears that
> using itChain.hasNext() and itChain.next() skips a number of elements in the
> iterator at each step.
> Given a single iterator of 7 elements, and using the following code:
> private IteratorChain buildIterator() {
> // this iterator contains the children of the current object only
> Iterator it = getChildren(p);
> 		
> // we use an IteratorChain to add multiple iterators together without the
> overhead of copying
> IteratorChain itChain = new IteratorChain(it);
> return itChain;
> }
> and then simply
> IteratorChain it = treeModel.getAllTreeNodes(obj);
> 	
> // FIXME this only prints one or two of the results, which is a bug!
> while (it.hasNext())
>     System.out.println(": " + it.next().getClass());
> I put in 7 elements, but only get 2 out - the 2nd and the last elements. It
> appears that through my debugging that the nextClause variable is updated even
> when the hasNext() function is called.
> Also, if I put 7 system.out.println statements, all elements are printed as normal.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.