Bug #4907
closedIntHashMap.IntHashMapValueIterator - hasNext() is not stateless
0%
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().
Updated by Michael Kay about 3 years ago
IntHashSetIterator
suffers the same problem.
Updated by Michael Kay about 3 years ago
- 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).
Updated by Michael Kay about 3 years ago
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.
Updated by Michael Kay about 3 years ago
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.
Updated by Michael Kay about 3 years ago
- 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