Skip to content

Commit

Permalink
Replicate impl to other types, share iterate() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed Nov 7, 2023
1 parent c79ddd8 commit e86b814
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 63 deletions.
22 changes: 7 additions & 15 deletions user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,14 @@ public boolean tryAdvance(DoubleConsumer action) {
}

static DoubleStream iterate(double seed, DoubleUnaryOperator f) {
Spliterator.OfDouble spliterator =
new Spliterators.AbstractDoubleSpliterator(
Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {
private double next = seed;

@Override
public boolean tryAdvance(DoubleConsumer action) {
action.accept(next);
next = f.applyAsDouble(next);
return true;
}
};

return StreamSupport.doubleStream(spliterator, false);
return iterate(seed, ignore -> true, f);
}

static DoubleStream iterate(double seed, DoublePredicate hasNext, DoubleUnaryOperator f) {
Spliterator.OfDouble spliterator =
new Spliterators.AbstractDoubleSpliterator(
Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {
private boolean first = true;
private double next = seed;
private boolean terminated = false;

Expand All @@ -174,12 +162,16 @@ public boolean tryAdvance(DoubleConsumer action) {
if (terminated) {
return false;
}
if (!first) {
next = f.applyAsDouble(next);
}
first = false;

if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.applyAsDouble(next);
return true;
}
};
Expand Down
23 changes: 7 additions & 16 deletions user/super/com/google/gwt/emul/java/util/stream/IntStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,14 @@ public boolean tryAdvance(IntConsumer action) {
}

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);
return iterate(seed, ignore -> true, f);
}

static IntStream iterate(int seed, IntPredicate hasNext, IntUnaryOperator f) {
Spliterator.OfInt spliterator =
new Spliterators.AbstractIntSpliterator(
Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {
private boolean first = true;
private int next = seed;
private boolean terminated = false;

Expand All @@ -178,12 +165,16 @@ public boolean tryAdvance(IntConsumer action) {
if (terminated) {
return false;
}
if (!first) {
next = f.applyAsInt(next);
}
first = false;

if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.applyAsInt(next);
return true;
}
};
Expand Down
21 changes: 7 additions & 14 deletions user/super/com/google/gwt/emul/java/util/stream/LongStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,14 @@ public boolean tryAdvance(LongConsumer action) {
}

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);
return iterate(seed, ignore -> true, f);
}

static LongStream iterate(long seed, LongPredicate hasNext, LongUnaryOperator f) {
Spliterator.OfLong spliterator =
new Spliterators.AbstractLongSpliterator(
Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) {
private boolean first = true;
private long next = seed;
private boolean terminated = false;

Expand All @@ -176,12 +165,16 @@ public boolean tryAdvance(LongConsumer action) {
if (terminated) {
return false;
}
if (!first) {
next = f.applyAsLong(next);
}
first = false;

if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.applyAsLong(next);
return true;
}
};
Expand Down
19 changes: 1 addition & 18 deletions user/super/com/google/gwt/emul/java/util/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,7 @@ public boolean tryAdvance(Consumer<? super T> action) {
}

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);
return true;
}
};
return StreamSupport.stream(spliterator, false);
return iterate(seed, ignore -> true, f);
}

static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> f) {
Expand Down

0 comments on commit e86b814

Please sign in to comment.