Search in sources :

Example 1 with AbstractIntSpliterator

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

the class IntStream method iterate.

static IntStream iterate(int seed, IntUnaryOperator f) {
    AbstractIntSpliterator spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {

        private int next = seed;

        @Override
        public boolean tryAdvance(IntConsumer action) {
            action.accept(next);
            next = f.applyAsInt(next);
            return true;
        }
    };
    return StreamSupport.intStream(spliterator, false);
}
Also used : AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) IntConsumer(java.util.function.IntConsumer) ObjIntConsumer(java.util.function.ObjIntConsumer)

Example 2 with AbstractIntSpliterator

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

the class IntStream method rangeClosed.

static IntStream rangeClosed(int startInclusive, int endInclusive) {
    if (startInclusive > endInclusive) {
        return empty();
    }
    int count = endInclusive - startInclusive + 1;
    AbstractIntSpliterator spliterator = new Spliterators.AbstractIntSpliterator(count, Spliterator.IMMUTABLE | Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED | Spliterator.SORTED | Spliterator.DISTINCT) {

        private int next = startInclusive;

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

        @Override
        public boolean tryAdvance(IntConsumer action) {
            if (next <= endInclusive) {
                action.accept(next++);
                return true;
            }
            return false;
        }
    };
    return StreamSupport.intStream(spliterator, false);
}
Also used : AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) IntConsumer(java.util.function.IntConsumer) ObjIntConsumer(java.util.function.ObjIntConsumer)

Example 3 with AbstractIntSpliterator

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

the class IntStreamImpl method peek.

@Override
public IntStream peek(IntConsumer action) {
    checkNotNull(action);
    throwIfTerminated();
    AbstractIntSpliterator peekSpliterator = new Spliterators.AbstractIntSpliterator(spliterator.estimateSize(), spliterator.characteristics()) {

        @Override
        public boolean tryAdvance(final IntConsumer innerAction) {
            return spliterator.tryAdvance(action.andThen(innerAction));
        }
    };
    return new IntStreamImpl(this, peekSpliterator);
}
Also used : AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) IntConsumer(java.util.function.IntConsumer) ObjIntConsumer(java.util.function.ObjIntConsumer)

Example 4 with AbstractIntSpliterator

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

the class IntStreamImpl method sorted.

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

        Spliterator.OfInt ordered = null;

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

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

Example 5 with AbstractIntSpliterator

use of java.util.Spliterators.AbstractIntSpliterator in project streamex by amaembo.

the class IntStreamEx method of.

/**
 * Returns a sequential ordered {@code IntStreamEx} backed by the content of
 * given {@link InputStream}.
 *
 * <p>
 * The resulting stream contains int values between 0 and 255 (0xFF)
 * inclusive, as they are returned by the {@link InputStream#read()} method.
 * If you want to get <code>byte</code> values (e.g. -1 instead of 255),
 * simply cast the stream elements like <code>.map(b -&gt; (byte)b)</code>.
 * The terminal -1 value is excluded from the resulting stream.
 *
 * <p>
 * If the underlying {@code InputStream} throws an {@link IOException}
 * during the stream traversal, it will be rethrown as
 * {@link UncheckedIOException}.
 *
 * <p>
 * When the returned {@code IntStreamEx} is closed the original
 * {@code InputStream} is closed as well. If {@link InputStream#close()}
 * method throws an {@code IOException}, it will be rethrown as
 * {@link UncheckedIOException}.
 *
 * @param is an {@code InputStream} to create an {@code IntStreamEx} on.
 * @return the new stream
 * @see #asByteInputStream()
 * @since 0.6.1
 */
public static IntStreamEx of(InputStream is) {
    Spliterator.OfInt spliterator = new AbstractIntSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.NONNULL) {

        @Override
        public boolean tryAdvance(IntConsumer action) {
            try {
                int next = is.read();
                if (next == -1)
                    return false;
                action.accept(next);
                return true;
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    };
    return of(spliterator).onClose(() -> {
        try {
            is.close();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
}
Also used : UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) Spliterator(java.util.Spliterator) AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) IntConsumer(java.util.function.IntConsumer) ObjIntConsumer(java.util.function.ObjIntConsumer)

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