You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Alejandro Carrasco <al...@gmail.com> on 2010/11/17 12:17:54 UTC
[collections] bug in IteratorChain (branch v2.1.1)
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