use of java.util.Spliterators.AbstractDoubleSpliterator in project j2cl by google.
the class StreamImpl method flatMapToDouble.
@Override
public DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper) {
throwIfTerminated();
final Spliterator<? extends DoubleStream> spliteratorOfStreams = new MapToObjSpliterator<>(mapper, spliterator);
AbstractDoubleSpliterator flatMapSpliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, 0) {
DoubleStream nextStream;
Spliterator.OfDouble next;
@Override
public boolean tryAdvance(DoubleConsumer 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 DoubleStreamImpl(this, flatMapSpliterator);
}
Aggregations