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