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);
}
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);
}
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);
}
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);
}
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 -> (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);
}
});
}
Aggregations