Search in sources :

Example 1 with AbstractLongSpliterator

use of java.util.Spliterators.AbstractLongSpliterator in project j2cl by google.

the class LongStreamImpl method sorted.

@Override
public LongStream sorted() {
    throwIfTerminated();
    AbstractLongSpliterator sortedSpliterator = new Spliterators.AbstractLongSpliterator(spliterator.estimateSize(), spliterator.characteristics() | Spliterator.SORTED) {

        Spliterator.OfLong ordered = null;

        @Override
        public Comparator<? super Long> getComparator() {
            return null;
        }

        @Override
        public boolean tryAdvance(LongConsumer action) {
            if (ordered == null) {
                long[] list = new long[0];
                spliterator.forEachRemaining((long item) -> list[list.length] = item);
                Arrays.sort(list);
                ordered = Spliterators.spliterator(list, characteristics());
            }
            return ordered.tryAdvance(action);
        }
    };
    return new LongStreamImpl(this, sortedSpliterator);
}
Also used : LongConsumer(java.util.function.LongConsumer) ObjLongConsumer(java.util.function.ObjLongConsumer) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator)

Example 2 with AbstractLongSpliterator

use of java.util.Spliterators.AbstractLongSpliterator in project j2cl by google.

the class LongStreamImpl method flatMap.

@Override
public LongStream flatMap(LongFunction<? extends LongStream> mapper) {
    throwIfTerminated();
    final Spliterator<? extends LongStream> spliteratorOfStreams = new MapToObjSpliterator<>(mapper, spliterator);
    AbstractLongSpliterator flatMapSpliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, 0) {

        LongStream nextStream;

        Spliterator.OfLong next;

        @Override
        public boolean tryAdvance(LongConsumer 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 LongStreamImpl(this, flatMapSpliterator);
}
Also used : Arrays(java.util.Arrays) LongBinaryOperator(java.util.function.LongBinaryOperator) Spliterators(java.util.Spliterators) OptionalDouble(java.util.OptionalDouble) IntConsumer(java.util.function.IntConsumer) DoubleConsumer(java.util.function.DoubleConsumer) Supplier(java.util.function.Supplier) HashSet(java.util.HashSet) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator) OptionalLong(java.util.OptionalLong) InternalPreconditions.checkNotNull(javaemul.internal.InternalPreconditions.checkNotNull) BiConsumer(java.util.function.BiConsumer) LongPredicate(java.util.function.LongPredicate) LongSummaryStatistics(java.util.LongSummaryStatistics) LongToDoubleFunction(java.util.function.LongToDoubleFunction) LongToIntFunction(java.util.function.LongToIntFunction) LongFunction(java.util.function.LongFunction) LongConsumer(java.util.function.LongConsumer) PrimitiveIterator(java.util.PrimitiveIterator) Consumer(java.util.function.Consumer) InternalPreconditions.checkState(javaemul.internal.InternalPreconditions.checkState) LongUnaryOperator(java.util.function.LongUnaryOperator) ObjLongConsumer(java.util.function.ObjLongConsumer) Comparator(java.util.Comparator) Spliterator(java.util.Spliterator) LongConsumer(java.util.function.LongConsumer) ObjLongConsumer(java.util.function.ObjLongConsumer) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator)

Example 3 with AbstractLongSpliterator

use of java.util.Spliterators.AbstractLongSpliterator in project j2cl by google.

the class StreamImpl method flatMapToLong.

@Override
public LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper) {
    throwIfTerminated();
    final Spliterator<? extends LongStream> spliteratorOfStreams = new MapToObjSpliterator<>(mapper, spliterator);
    AbstractLongSpliterator flatMapSpliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, 0) {

        LongStream nextStream;

        Spliterator.OfLong next;

        @Override
        public boolean tryAdvance(LongConsumer 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 LongStreamImpl(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) LongConsumer(java.util.function.LongConsumer) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator)

Example 4 with AbstractLongSpliterator

use of java.util.Spliterators.AbstractLongSpliterator in project j2cl by google.

the class LongStream method concat.

static LongStream concat(LongStream a, LongStream b) {
    // This is nearly the same as flatMap, but inlined, wrapped around a single spliterator of
    // these two objects, and without close() called as the stream progresses. Instead, close is
    // invoked as part of the resulting stream's own onClose, so that either can fail without
    // affecting the other, and correctly collecting suppressed exceptions.
    // TODO replace this flatMap-ish spliterator with one that directly combines the two root
    // streams
    Spliterator<? extends LongStream> spliteratorOfStreams = Arrays.asList(a, b).spliterator();
    AbstractLongSpliterator spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, 0) {

        Spliterator.OfLong next;

        @Override
        public boolean tryAdvance(LongConsumer 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 {
                    // 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) {
                        next = n.spliterator();
                    }
                })) {
                    return false;
                }
            }
            return true;
        }
    };
    LongStream result = new LongStreamImpl(null, spliterator);
    return result.onClose(a::close).onClose(b::close);
}
Also used : LongToIntFunction(java.util.function.LongToIntFunction) Arrays(java.util.Arrays) LongFunction(java.util.function.LongFunction) LongSupplier(java.util.function.LongSupplier) LongBinaryOperator(java.util.function.LongBinaryOperator) Spliterators(java.util.Spliterators) OptionalDouble(java.util.OptionalDouble) Supplier(java.util.function.Supplier) LongConsumer(java.util.function.LongConsumer) PrimitiveIterator(java.util.PrimitiveIterator) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator) OptionalLong(java.util.OptionalLong) InternalPreconditions.checkState(javaemul.internal.InternalPreconditions.checkState) LongUnaryOperator(java.util.function.LongUnaryOperator) ObjLongConsumer(java.util.function.ObjLongConsumer) BiConsumer(java.util.function.BiConsumer) LongPredicate(java.util.function.LongPredicate) Comparator(java.util.Comparator) Spliterator(java.util.Spliterator) LongSummaryStatistics(java.util.LongSummaryStatistics) LongToDoubleFunction(java.util.function.LongToDoubleFunction) LongConsumer(java.util.function.LongConsumer) ObjLongConsumer(java.util.function.ObjLongConsumer) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator)

Example 5 with AbstractLongSpliterator

use of java.util.Spliterators.AbstractLongSpliterator in project j2cl by google.

the class LongStream method iterate.

static LongStream iterate(long seed, LongUnaryOperator f) {
    AbstractLongSpliterator spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {

        private long next = seed;

        @Override
        public boolean tryAdvance(LongConsumer action) {
            action.accept(next);
            next = f.applyAsLong(next);
            return true;
        }
    };
    return StreamSupport.longStream(spliterator, false);
}
Also used : LongConsumer(java.util.function.LongConsumer) ObjLongConsumer(java.util.function.ObjLongConsumer) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator)

Aggregations

AbstractLongSpliterator (java.util.Spliterators.AbstractLongSpliterator)7 LongConsumer (java.util.function.LongConsumer)7 ObjLongConsumer (java.util.function.ObjLongConsumer)6 Comparator (java.util.Comparator)3 Spliterator (java.util.Spliterator)3 Spliterators (java.util.Spliterators)3 BiConsumer (java.util.function.BiConsumer)3 Supplier (java.util.function.Supplier)3 InternalPreconditions.checkState (javaemul.internal.InternalPreconditions.checkState)3 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 LongSummaryStatistics (java.util.LongSummaryStatistics)2 OptionalDouble (java.util.OptionalDouble)2 OptionalLong (java.util.OptionalLong)2 PrimitiveIterator (java.util.PrimitiveIterator)2 Consumer (java.util.function.Consumer)2 DoubleConsumer (java.util.function.DoubleConsumer)2 IntConsumer (java.util.function.IntConsumer)2 LongBinaryOperator (java.util.function.LongBinaryOperator)2 LongFunction (java.util.function.LongFunction)2