Reputation: 9543
I'll start with the code:
import java.util.stream.Stream
import java.util.stream.StreamSupport
def hasMore1 = true
def iter1 = new Iterator<Integer>() {
boolean hasNext() { def retVal = hasMore1; hasMore1 = false; retVal }
Integer next() { 42 }
}
def hasMore2 = true
def iter2 = [
hasNext: { -> def retVal = hasMore2; hasMore2 = false; retVal },
next: { -> 42 }
] as Iterator<Integer>
def stream1 = StreamSupport.stream(Spliterators.spliterator(iter1, 1, 0), false)
def stream2 = StreamSupport.stream(Spliterators.spliterator(iter2, 1, 0), false)
stream1.forEach { println it } // prints 42
stream2.forEach { println it } // throws java.lang.UnsupportedOperationException
These two methods of implementing an iterator in groovy seem to be semantically equivalent, and in normal use cases, like iterating with foreach, and use in list comprehensions all seem to treat them the same. The Java 8 Streams api however seems to treat them different as demonstrated above. How and why? Is there anything I can do to the map-implementing interface version to make it behave correctly?
Upvotes: 1
Views: 340
Reputation: 9543
Seems this has been fixed in groovy 2.3.8
It may have been similar to this bug that was fixed in the same release: https://issues.apache.org/jira/browse/GROOVY-7104
Upvotes: 1