use of java.util.Spliterators.AbstractIntSpliterator in project j2cl by google.
the class StreamImpl method flatMapToInt.
@Override
public IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) {
throwIfTerminated();
final Spliterator<? extends IntStream> spliteratorOfStreams = new MapToObjSpliterator<>(mapper, spliterator);
AbstractIntSpliterator flatMapSpliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE, 0) {
IntStream nextStream;
Spliterator.OfInt next;
@Override
public boolean tryAdvance(IntConsumer action) {
// look for a new spliterator
while (advanceToNextSpliterator()) {
// if we have one, try to read and use it
if (next.tryAdvance(action)) {
return true;
} else {
nextStream.close();
nextStream = null;
// failed, null it out so we can find another
next = null;
}
}
return false;
}
private boolean advanceToNextSpliterator() {
while (next == null) {
if (!spliteratorOfStreams.tryAdvance(n -> {
if (n != null) {
nextStream = n;
next = n.spliterator();
}
})) {
return false;
}
}
return true;
}
};
return new IntStreamImpl(this, flatMapSpliterator);
}
Aggregations