Project

Profile

Help

Bug #4890

DotNetIterator class doesn't implement hasNext() correctly

Added by Michael Kay 22 days ago. Updated 22 days ago.

Status:
New
Priority:
Low
Assignee:
Category:
.NET API
Sprint/Milestone:
-
Start date:
2021-02-04
Due date:
% Done:

0%

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

Description

This problem may be asymptomatic, but the class net.sf.saxon.dotnet.DotNetIterator, which is used on the .NET product to bridge a C# IEnumerator to a Java Iterator, is incorrect. According to the contract for java.lang.Iterator, the hasNext() method should leave the state of the iterator unchanged, but we're calling MoveNext(), which means that if you make two calls on hasNext() without an intervening call on next(), the iterator will change position.

It works so long as the caller calls hasNext() exactly once between successive calls on next().

History

#1 Updated by Michael Kay 22 days ago

  • Subject changed from DotNotIterator doesn't implement hasNext() correctly to DotNetIterator class doesn't implement hasNext() correctly

#2 Updated by Michael Kay 22 days ago

I think the correct logic would be:

public class IteratorFromEnumerator<T> : java.util.Iterator<T> {
        private IEnumerator<T> enumerator;
        private bool moreToCome ;
        public IteratorFromEnumerator(IEnumerator<T> enumerator) {
            this.enumerator = enumerator;
            this.moreToCome = enumerator.MoveNext();
        }
        public bool hasNext() {
            return moreToCome;
        }

        public T next() {
            T nextItem = enumerator.Current;
            this.moreToCome = enumerator.MoveNext();
            return nextItem;
        }
    }

Please register to edit this issue

Also available in: Atom PDF