Skip to content

Commit

Permalink
Correct Stream impl, to be generalized
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed Nov 7, 2023
1 parent a4eeaf1 commit c79ddd8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions user/super/com/google/gwt/emul/java/util/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,17 @@ static <T> Stream<T> iterate(T seed, UnaryOperator<T> f) {
AbstractSpliterator<T> spliterator =
new Spliterators.AbstractSpliterator<T>(
Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {
private boolean first = true;
private T next = seed;

@Override
public boolean tryAdvance(Consumer<? super T> action) {
if (!first) {
next = f.apply(next);
}
first = false;

action.accept(next);
next = f.apply(next);
return true;
}
};
Expand All @@ -169,6 +174,7 @@ static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator
AbstractSpliterator<T> spliterator =
new Spliterators.AbstractSpliterator<T>(
Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {
private boolean first = true;
private T next = seed;
private boolean terminated = false;

Expand All @@ -177,12 +183,16 @@ public boolean tryAdvance(Consumer<? super T> action) {
if (terminated) {
return false;
}
if (!first) {
next = f.apply(next);
}
first = false;

if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.apply(next);
return true;
}
};
Expand Down

0 comments on commit c79ddd8

Please sign in to comment.