commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alejandro Carrasco <alejandro.carra...@gmail.com>
Subject [collections] bug in IteratorChain (branch v2.1.1)
Date Wed, 17 Nov 2010 11:17:54 GMT
Hi,

I’ve spotted a bug in IteratorChain:

When the first iterator is empty, hasNext() returns false the first time
it’s called even if the next iterators are not empty.

This snippet shows that incorrect behavior:

       Iterator<?> it =
IteratorUtils.chainedIterator(Arrays.asList().iterator(),
Arrays.asList("a").iterator());
        System.out.println(it.hasNext());//incorrectly returns false
       System.out.println(it.hasNext());//returns true

It’s due a premature exit of the method updateCurrentIterator() when called
the first time, and can be easily fixed just removing a return:

   protected void updateCurrentIterator() {
        if (currentIterator == null) {
           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;//THIS LINE MUST GO
        }

       if (currentIteratorIndex == (iteratorChain.size() - 1)) {
            return;
       }

       while (currentIterator.hasNext() == false) {
           ++currentIteratorIndex;
            currentIterator = (Iterator)
iteratorChain.get(currentIteratorIndex);

            if (currentIteratorIndex == (iteratorChain.size() - 1)) {
               return;
            }
       }
    }

The bug is not present anymore in 3.x branch.

Cheers.

-- 
Alejandro Carrasco Yelmo

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message