Bug #4907
closed
IntHashMap.IntHashMapValueIterator - hasNext() is not stateless
Fixed in Maintenance Release:
Description
The class IntHashMap.IntHashMapValueIterator
does not correctly implement the java.lang.Iterator interface because its hasNext()
method is stateful - the iterator works only if hasNext()
is called exactly once before every call of next().
IntHashSetIterator
suffers the same problem.
- Status changed from New to In Progress
Added some unit tests for IntHashMapValueIterator
. These also reveal (a) Sometimes ArrayIndexOutOfBoundsException
is thrown in place of NoSuchElementException
, (b) a call on next()
does not work unless preceded by a call on hasNext()
(which the spec does not require).
IntHashSet.keyIterator() has the same problem.
The JavaDoc for IntIterator
does say "The result is undefined unless hasNext() has been called and has returned true." which isn't quite the same contract as for java.util.Iterator() -- but we're not even following that spec precisely because even under that rule, hasNext()
should be callable twice without changing the iterator position.
Change of plan. I've been looking at the implementations of IntIterator
used extensively in the regular expression engine, which I really don't want to change because it's too risky. Instead I'm going to change the specification to make it clear that the contract isn't the same as java.util.Iterator: hasNext()
is more like the MoveNext()
in C#. Basically, the behaviour of an alternating sequence of hasNext()
and next()
calls is well defined, and anything else is unpredictable.
- Status changed from In Progress to Closed
Closed with no change to the code, other than clarifications to the Javadoc, and added unit tests.
Please register to edit this issue
Also available in: Atom
PDF