Search in sources :

Example 6 with AbstractIntSpliterator

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);
}
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) AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) IntConsumer(java.util.function.IntConsumer)

Aggregations

AbstractIntSpliterator (java.util.Spliterators.AbstractIntSpliterator)6 IntConsumer (java.util.function.IntConsumer)6 ObjIntConsumer (java.util.function.ObjIntConsumer)5 Spliterator (java.util.Spliterator)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 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 Spliterators (java.util.Spliterators)1 AbstractDoubleSpliterator (java.util.Spliterators.AbstractDoubleSpliterator)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