Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java 9 Stream APIs #9837

Merged
merged 19 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions user/super/com/google/gwt/emul/java/util/stream/Collectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.HashMap;
Expand All @@ -27,6 +28,7 @@
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
Expand Down Expand Up @@ -170,6 +172,34 @@ public static Collector<CharSequence,?,String> joining(CharSequence delimiter) {
downstream.finisher());
}

public static <T, U, A, R> Collector<T, ?, R> flatMapping(Function<? super T,? extends Stream<?
extends U>> mapper, Collector<? super U, A, R> downstream) {
return new CollectorImpl<>(
downstream.supplier(),
(A a, T t) -> {
mapper.apply(t).forEach(u -> {
downstream.accumulator().accept(a, u);
niloc132 marked this conversation as resolved.
Show resolved Hide resolved
});
},
downstream.combiner(),
downstream.finisher()
);
}

public static <T, A, R> Collector<T, ?, R> filtering(Predicate<? super T> predicate,
Collector<? super T, A, R> downstream) {
return new CollectorImpl<>(
downstream.supplier(),
(a, t) -> {
if (predicate.test(t)) {
downstream.accumulator().accept(a, t);
}
},
downstream.combiner(),
downstream.finisher()
);
}

public static <T> Collector<T,?,Optional<T>> maxBy(Comparator<? super T> comparator) {
return reducing(BinaryOperator.maxBy(comparator));
}
Expand Down Expand Up @@ -307,6 +337,11 @@ public static <T> Collector<T,?,List<T>> toList() {
return toCollection(ArrayList::new);
}

public static <T> Collector<T, ?, List<T>> toUnmodifiableList() {
niloc132 marked this conversation as resolved.
Show resolved Hide resolved
Collector<T, ?, List<T>> mapping = mapping(Objects::requireNonNull, toList());
return collectingAndThen(mapping, Collections::unmodifiableList);
}

public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(
final Function<? super T, ? extends K> keyMapper,
final Function<? super T, ? extends U> valueMapper) {
Expand All @@ -325,6 +360,27 @@ public static <T> Collector<T,?,List<T>> toList() {
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}

public static <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(Function<? super T, ?
extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
return collectingAndThen(
toMap(disallowNulls(keyMapper), disallowNulls(valueMapper)),
Collections::unmodifiableMap
);
}

public static <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(Function<? super T, ?
extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U>
mergeFunction) {
return collectingAndThen(
toMap(disallowNulls(keyMapper), disallowNulls(valueMapper), mergeFunction),
Collections::unmodifiableMap
);
}

private static <T, R> Function<T, R> disallowNulls(Function<T, R> func) {
return func.andThen(Objects::requireNonNull);
}

public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(
final Function<? super T, ? extends K> keyMapper,
final Function<? super T, ? extends U> valueMapper,
Expand Down Expand Up @@ -357,6 +413,11 @@ public static <T> Collector<T,?,Set<T>> toSet() {
);
}

public static <T> Collector<T, ?, Set<T>> toUnmodifiableSet() {
Collector<T, ?, Set<T>> mapping = mapping(Objects::requireNonNull, toSet());
return collectingAndThen(mapping, Collections::unmodifiableSet);
}

private static <T, D, A> D streamAndCollect(Collector<? super T, A, D> downstream, List<T> list) {
A a = downstream.supplier().get();
for (T t : list) {
Expand Down
52 changes: 52 additions & 0 deletions user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,30 @@ public boolean tryAdvance(DoubleConsumer action) {
return StreamSupport.doubleStream(spliterator, false);
}

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

@Override
public boolean tryAdvance(DoubleConsumer action) {
if (terminated) {
return false;
}
if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.applyAsDouble(next);
niloc132 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
};
return StreamSupport.doubleStream(spliterator, false);
}

static DoubleStream of(double... values) {
return Arrays.stream(values);
}
Expand All @@ -185,6 +209,20 @@ static DoubleStream of(double t) {

DoubleStream distinct();

default DoubleStream dropWhile(DoublePredicate predicate) {
return filter(new DoublePredicate() {
private boolean drop = true;
@Override
public boolean test(double value) {
if (!drop) {
return true;
}
drop = predicate.test(value);
return !drop;
}
});
}

DoubleStream filter(DoublePredicate predicate);

OptionalDouble findAny();
Expand Down Expand Up @@ -239,5 +277,19 @@ static DoubleStream of(double t) {

DoubleSummaryStatistics summaryStatistics();

default DoubleStream takeWhile(DoublePredicate predicate) {
niloc132 marked this conversation as resolved.
Show resolved Hide resolved
return filter(new DoublePredicate() {
private boolean take = true;
@Override
public boolean test(double value) {
if (!take) {
return false;
}
take = predicate.test(value);
return take;
}
});
}

double[] toArray();
}
52 changes: 52 additions & 0 deletions user/super/com/google/gwt/emul/java/util/stream/IntStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,30 @@ public boolean tryAdvance(IntConsumer action) {
return StreamSupport.intStream(spliterator, false);
}

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

@Override
public boolean tryAdvance(IntConsumer action) {
if (terminated) {
return false;
}
if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.applyAsInt(next);
return true;
}
};
return StreamSupport.intStream(spliterator, false);
}

static IntStream of(int... values) {
return Arrays.stream(values);
}
Expand Down Expand Up @@ -235,6 +259,20 @@ public boolean tryAdvance(IntConsumer action) {

IntStream distinct();

default IntStream dropWhile(IntPredicate predicate) {
return filter(new IntPredicate() {
private boolean drop = true;
@Override
public boolean test(int value) {
if (!drop) {
return true;
}
drop = predicate.test(value);
return !drop;
}
});
}

IntStream filter(IntPredicate predicate);

OptionalInt findAny();
Expand Down Expand Up @@ -289,5 +327,19 @@ public boolean tryAdvance(IntConsumer action) {

IntSummaryStatistics summaryStatistics();

default IntStream takeWhile(IntPredicate predicate) {
return filter(new IntPredicate() {
private boolean take = true;
@Override
public boolean test(int value) {
if (!take) {
return false;
}
take = predicate.test(value);
return take;
}
});
}

int[] toArray();
}
52 changes: 52 additions & 0 deletions user/super/com/google/gwt/emul/java/util/stream/LongStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ public boolean tryAdvance(LongConsumer action) {
return StreamSupport.longStream(spliterator, false);
}

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

@Override
public boolean tryAdvance(LongConsumer action) {
if (terminated) {
return false;
}
if (!hasNext.test(next)) {
terminated = true;
return false;
}
action.accept(next);
next = f.applyAsLong(next);
return true;
}
};
return StreamSupport.longStream(spliterator, false);
}

static LongStream of(long... values) {
return Arrays.stream(values);
}
Expand Down Expand Up @@ -231,6 +255,20 @@ public boolean tryAdvance(LongConsumer action) {

LongStream distinct();

default LongStream dropWhile(LongPredicate predicate) {
return filter(new LongPredicate() {
private boolean drop = true;
@Override
public boolean test(long value) {
if (!drop) {
return true;
}
drop = predicate.test(value);
return !drop;
}
});
}

LongStream filter(LongPredicate predicate);

OptionalLong findAny();
Expand Down Expand Up @@ -285,5 +323,19 @@ public boolean tryAdvance(LongConsumer action) {

LongSummaryStatistics summaryStatistics();

default LongStream takeWhile(LongPredicate predicate) {
return filter(new LongPredicate() {
private boolean take = true;
@Override
public boolean test(long value) {
if (!take) {
return false;
}
take = predicate.test(value);
return take;
}
});
}

long[] toArray();
}
Loading