Search in sources :

Example 1 with AbstractDoubleSpliterator

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);
}
Also used : AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) AbstractSpliterator(java.util.Spliterators.AbstractSpliterator) Spliterators(java.util.Spliterators) BiFunction(java.util.function.BiFunction) IntConsumer(java.util.function.IntConsumer) DoubleConsumer(java.util.function.DoubleConsumer) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator) InternalPreconditions.checkNotNull(javaemul.internal.InternalPreconditions.checkNotNull) BiConsumer(java.util.function.BiConsumer) ToLongFunction(java.util.function.ToLongFunction) IntFunction(java.util.function.IntFunction) AbstractDoubleSpliterator(java.util.Spliterators.AbstractDoubleSpliterator) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) ToIntFunction(java.util.function.ToIntFunction) BinaryOperator(java.util.function.BinaryOperator) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) List(java.util.List) InternalPreconditions.checkState(javaemul.internal.InternalPreconditions.checkState) ToDoubleFunction(java.util.function.ToDoubleFunction) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) Spliterator(java.util.Spliterator) DoubleConsumer(java.util.function.DoubleConsumer) AbstractDoubleSpliterator(java.util.Spliterators.AbstractDoubleSpliterator)

Aggregations

ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Optional (java.util.Optional)1 Spliterator (java.util.Spliterator)1 Spliterators (java.util.Spliterators)1 AbstractDoubleSpliterator (java.util.Spliterators.AbstractDoubleSpliterator)1 AbstractIntSpliterator (java.util.Spliterators.AbstractIntSpliterator)1 AbstractLongSpliterator (java.util.Spliterators.AbstractLongSpliterator)1 AbstractSpliterator (java.util.Spliterators.AbstractSpliterator)1 BiConsumer (java.util.function.BiConsumer)1 BiFunction (java.util.function.BiFunction)1 BinaryOperator (java.util.function.BinaryOperator)1 Consumer (java.util.function.Consumer)1 DoubleConsumer (java.util.function.DoubleConsumer)1 Function (java.util.function.Function)1 IntConsumer (java.util.function.IntConsumer)1