Project

Profile

Help

Bug #4907

closed

IntHashMap.IntHashMapValueIterator - hasNext() is not stateless

Added by Michael Kay about 3 years ago. Updated about 3 years ago.

Status:
Closed
Priority:
Low
Assignee:
Category:
-
Sprint/Milestone:
-
Start date:
2021-02-16
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:
Platforms:

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().

Actions #1

Updated by Michael Kay about 3 years ago

IntHashSetIterator suffers the same problem.

Actions #2

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).

Actions #3

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.

Actions #4

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.

Actions #5

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

Also available in: Atom PDF