From 90b1e1b87345d594ca47e84fab493b28d5e791d8 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Mon, 19 Jun 2023 14:52:53 -0500 Subject: [PATCH 01/17] Add Java 9 Stream APIs Partial #9547 --- .../gwt/emul/java/util/stream/Collectors.java | 61 +++++++++++++++++++ .../emul/java/util/stream/DoubleStream.java | 52 ++++++++++++++++ .../gwt/emul/java/util/stream/IntStream.java | 52 ++++++++++++++++ .../gwt/emul/java/util/stream/LongStream.java | 52 ++++++++++++++++ .../gwt/emul/java/util/stream/Stream.java | 60 ++++++++++++++++++ .../google/gwt/emultest/EmulJava9Suite.java | 10 +++ .../java8/util/stream/CollectorsTest.java | 4 +- .../java9/util/stream/CollectorsTest.java | 51 ++++++++++++++++ .../java9/util/stream/DoubleStreamTest.java | 50 +++++++++++++++ .../java9/util/stream/IntStreamTest.java | 50 +++++++++++++++ .../java9/util/stream/LongStreamTest.java | 50 +++++++++++++++ .../java9/util/stream/StreamTest.java | 58 ++++++++++++++++++ 12 files changed, 548 insertions(+), 2 deletions(-) create mode 100644 user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java create mode 100644 user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java create mode 100644 user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java create mode 100644 user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java create mode 100644 user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java diff --git a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java index d19f64cae2b..5b53249799e 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java @@ -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; @@ -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; @@ -170,6 +172,34 @@ public static Collector joining(CharSequence delimiter) { downstream.finisher()); } + public static Collector flatMapping(Function> mapper, Collector downstream) { + return new CollectorImpl<>( + downstream.supplier(), + (A a, T t) -> { + mapper.apply(t).forEach(u -> { + downstream.accumulator().accept(a, u); + }); + }, + downstream.combiner(), + downstream.finisher() + ); + } + + public static Collector filtering(Predicate predicate, + Collector downstream) { + return new CollectorImpl<>( + downstream.supplier(), + (a, t) -> { + if (predicate.test(t)) { + downstream.accumulator().accept(a, t); + } + }, + downstream.combiner(), + downstream.finisher() + ); + } + public static Collector> maxBy(Comparator comparator) { return reducing(BinaryOperator.maxBy(comparator)); } @@ -307,6 +337,11 @@ public static Collector> toList() { return toCollection(ArrayList::new); } + public static Collector> toUnmodifiableList() { + Collector> mapping = mapping(Objects::requireNonNull, toList()); + return collectingAndThen(mapping, Collections::unmodifiableList); + } + public static Collector> toMap( final Function keyMapper, final Function valueMapper) { @@ -325,6 +360,27 @@ public static Collector> toList() { return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new); } + public static Collector> toUnmodifiableMap(Function keyMapper, Function valueMapper) { + return collectingAndThen( + toMap(disallowNulls(keyMapper), disallowNulls(valueMapper)), + Collections::unmodifiableMap + ); + } + + public static Collector> toUnmodifiableMap(Function keyMapper, Function valueMapper, BinaryOperator + mergeFunction) { + return collectingAndThen( + toMap(disallowNulls(keyMapper), disallowNulls(valueMapper), mergeFunction), + Collections::unmodifiableMap + ); + } + + private static Function disallowNulls(Function func) { + return func.andThen(Objects::requireNonNull); + } + public static > Collector toMap( final Function keyMapper, final Function valueMapper, @@ -357,6 +413,11 @@ public static Collector> toSet() { ); } + public static Collector> toUnmodifiableSet() { + Collector> mapping = mapping(Objects::requireNonNull, toSet()); + return collectingAndThen(mapping, Collections::unmodifiableSet); + } + private static D streamAndCollect(Collector downstream, List list) { A a = downstream.supplier().get(); for (T t : list) { diff --git a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java index 012bd0982f7..3ce424b4ef3 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java @@ -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); + return true; + } + }; + return StreamSupport.doubleStream(spliterator, false); + } + static DoubleStream of(double... values) { return Arrays.stream(values); } @@ -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(); @@ -239,5 +277,19 @@ static DoubleStream of(double t) { DoubleSummaryStatistics summaryStatistics(); + default DoubleStream takeWhile(DoublePredicate predicate) { + 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(); } diff --git a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java index e661b5ca997..c48342d385b 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java @@ -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); } @@ -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(); @@ -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(); } diff --git a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java index 63861467c92..7f7063f6064 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java @@ -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); } @@ -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(); @@ -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(); } diff --git a/user/super/com/google/gwt/emul/java/util/stream/Stream.java b/user/super/com/google/gwt/emul/java/util/stream/Stream.java index 70a9dd9c827..bcec1632608 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Stream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Stream.java @@ -165,6 +165,30 @@ public boolean tryAdvance(Consumer action) { return StreamSupport.stream(spliterator, false); } + static Stream iterate(T seed, Predicate hasNext, UnaryOperator f) { + AbstractSpliterator spliterator = + new Spliterators.AbstractSpliterator( + Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) { + private T next = seed; + private boolean terminated = false; + + @Override + public boolean tryAdvance(Consumer action) { + if (terminated) { + return false; + } + if (!hasNext.test(next)) { + terminated = true; + return false; + } + action.accept(next); + next = f.apply(next); + return true; + } + }; + return StreamSupport.stream(spliterator, false); + } + static Stream of(T t) { // TODO consider a splittable that returns only a single value, either for use here or in the // singleton collection types @@ -175,6 +199,14 @@ static Stream of(T... values) { return Arrays.stream(values); } + static Stream ofNullable(T t) { + if (t == null) { + return empty(); + } else { + return of(t); + } + } + boolean allMatch(Predicate predicate); boolean anyMatch(Predicate predicate); @@ -188,6 +220,20 @@ R collect( Stream distinct(); + default Stream dropWhile(Predicate predicate) { + return filter(new Predicate() { + private boolean drop = true; + @Override + public boolean test(T t) { + if (!drop) { + return true; + } + drop = predicate.test(t); + return !drop; + } + }); + } + Stream filter(Predicate predicate); Optional findAny(); @@ -236,6 +282,20 @@ R collect( Stream sorted(Comparator comparator); + default Stream takeWhile(Predicate predicate) { + return filter(new Predicate() { + private boolean take = true; + @Override + public boolean test(T t) { + if (!take) { + return false; + } + take = predicate.test(t); + return take; + } + }); + } + Object[] toArray(); A[] toArray(IntFunction generator); diff --git a/user/test/com/google/gwt/emultest/EmulJava9Suite.java b/user/test/com/google/gwt/emultest/EmulJava9Suite.java index db677087f04..720af93d7af 100644 --- a/user/test/com/google/gwt/emultest/EmulJava9Suite.java +++ b/user/test/com/google/gwt/emultest/EmulJava9Suite.java @@ -15,6 +15,11 @@ */ package com.google.gwt.emultest; +import com.google.gwt.emultest.java9.util.stream.CollectorsTest; +import com.google.gwt.emultest.java9.util.stream.DoubleStreamTest; +import com.google.gwt.emultest.java9.util.stream.IntStreamTest; +import com.google.gwt.emultest.java9.util.stream.LongStreamTest; +import com.google.gwt.emultest.java9.util.stream.StreamTest; import com.google.gwt.emultest.java9.util.ListTest; import com.google.gwt.emultest.java9.util.MapTest; import com.google.gwt.emultest.java9.util.SetTest; @@ -25,6 +30,11 @@ /** Test JRE emulations. */ @RunWith(Suite.class) @SuiteClasses({ + CollectorsTest.class, + DoubleStreamTest.class, + IntStreamTest.class, + LongStreamTest.class, + StreamTest.class, ListTest.class, SetTest.class, MapTest.class diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/CollectorsTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/CollectorsTest.java index bfafad0f990..28fbf0da71c 100644 --- a/user/test/com/google/gwt/emultest/java8/util/stream/CollectorsTest.java +++ b/user/test/com/google/gwt/emultest/java8/util/stream/CollectorsTest.java @@ -466,7 +466,7 @@ public void testSet() { * This method attempts to apply a collector to items as a stream might do, so that we can simply * verify the output. Taken from the Collector class's javadoc. */ - private static void applyItems( + public static void applyItems( R expected, Collector collector, T t1, T t2, BiPredicate equals) { assertTrue( "failed without splitting", @@ -479,7 +479,7 @@ private static void applyItems( * This method attempts to apply a collector to items as a stream might do, so that we * can simply verify the output. Taken from the Collector class's javadoc. */ - private static void applyItems(R expected, Collector collector, T t1, T t2) { + public static void applyItems(R expected, Collector collector, T t1, T t2) { applyItems(expected, collector, t1, t2, Object::equals); } diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java new file mode 100644 index 00000000000..536af4b909f --- /dev/null +++ b/user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2023 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.emultest.java9.util.stream; + +import static com.google.gwt.emultest.java8.util.stream.CollectorsTest.applyItems; +import static java.util.stream.Collectors.filtering; +import static java.util.stream.Collectors.flatMapping; +import static java.util.stream.Collectors.toList; + +import com.google.gwt.emultest.java.util.EmulTestBase; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collector; + +/** + * Tests for java.util.stream.Collectors Java 9 API emulation. + */ +public class CollectorsTest extends EmulTestBase { + + public void testFlatMapping() { + // since applyItems tests the same inputs multiple times, we need fresh stream instances as they can't be reused + Collector, ?, List> flatMapping = flatMapping(Collection::stream, + toList()); + applyItems(Arrays.asList("a", "b"), flatMapping, Collections.singletonList("a"), + Collections.singletonList("b")); + applyItems(Arrays.asList("c", "d"), flatMapping, Collections.emptyList(), Arrays.asList("c", "d")); + } + + public void testFiltering() { + Collector> filtering = filtering(s -> s.equals("a"), toList()); + applyItems(Collections.singletonList("a"), filtering, "a", "b"); + applyItems(Collections.emptyList(), filtering, "c", "d"); + applyItems(Arrays.asList("a", "a"), filtering, "a", "a"); + } +} diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java new file mode 100644 index 00000000000..85c18706752 --- /dev/null +++ b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.emultest.java9.util.stream; + +import com.google.gwt.emultest.java.util.EmulTestBase; + +import java.util.stream.DoubleStream; + +/** + * Tests for java.util.stream.DoubleStream Java 9 API emulation. + */ +public class DoubleStreamTest extends EmulTestBase { + public void testIterate() { + assertEquals( + new double[] {10, 11, 12, 13, 14}, + DoubleStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); + } + + public void testTakeWhile() { + assertEquals( + new double[] {1, 2}, + DoubleStream.of(1, 2, 3, 4, 5).takeWhile(i -> i < 3).toArray() + ); + assertEquals(0, DoubleStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); + } + + public void testDropWhile() { + assertEquals( + new double[] {3, 4, 5}, + DoubleStream.of(1, 2, 3, 4, 5).dropWhile(i -> i < 3).toArray() + ); + assertEquals( + new double[] {1, 2, 3, 4, 5}, + DoubleStream.of(1, 2, 3, 4, 5).dropWhile(i -> i > 2).toArray() + ); + } +} diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java new file mode 100644 index 00000000000..3929845392c --- /dev/null +++ b/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.emultest.java9.util.stream; + +import com.google.gwt.emultest.java.util.EmulTestBase; + +import java.util.stream.IntStream; + +/** + * Tests for java.util.stream.IntStream Java 9 API emulation. + */ +public class IntStreamTest extends EmulTestBase { + public void testIterate() { + assertEquals( + new int[] {10, 11, 12, 13, 14}, + IntStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); + } + + public void testTakeWhile() { + assertEquals( + new int[] {1, 2}, + IntStream.of(1, 2, 3, 4, 5).takeWhile(i -> i < 3).toArray() + ); + assertEquals(0, IntStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); + } + + public void testDropWhile() { + assertEquals( + new int[] {3, 4, 5}, + IntStream.of(1, 2, 3, 4, 5).dropWhile(i -> i < 3).toArray() + ); + assertEquals( + new int[] {1, 2, 3, 4, 5}, + IntStream.of(1, 2, 3, 4, 5).dropWhile(i -> i > 2).toArray() + ); + } +} diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java new file mode 100644 index 00000000000..e7e48d921e8 --- /dev/null +++ b/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.emultest.java9.util.stream; + +import com.google.gwt.emultest.java.util.EmulTestBase; + +import java.util.stream.LongStream; + +/** + * Tests for java.util.stream.IntStream Java 9 API emulation. + */ +public class LongStreamTest extends EmulTestBase { + public void testIterate() { + assertEquals( + new long[] {10, 11, 12, 13, 14}, + LongStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); + } + + public void testTakeWhile() { + assertEquals( + new long[] {1, 2}, + LongStream.of(1, 2, 3, 4, 5).takeWhile(i -> i < 3).toArray() + ); + assertEquals(0, LongStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); + } + + public void testDropWhile() { + assertEquals( + new long[] {3, 4, 5}, + LongStream.of(1, 2, 3, 4, 5).dropWhile(i -> i < 3).toArray() + ); + assertEquals( + new long[] {1, 2, 3, 4, 5}, + LongStream.of(1, 2, 3, 4, 5).dropWhile(i -> i > 2).toArray() + ); + } +} diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java new file mode 100644 index 00000000000..66a728f9a30 --- /dev/null +++ b/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2023 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.emultest.java9.util.stream; + +import com.google.gwt.emultest.java.util.EmulTestBase; + +import java.util.stream.Stream; + +/** + * Tests for java.util.stream.Stream Java 9 API emulation. + */ +public class StreamTest extends EmulTestBase { + public void testIterate() { + assertEquals( + new Integer[] {10, 11, 12, 13, 14}, + Stream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray(Integer[]::new)); + } + + public void testOfNullable() { + assertEquals(0, Stream.ofNullable(null).count()); + assertEquals( + new String[] {"abc"}, + Stream.ofNullable("abc").toArray(String[]::new) + ); + } + + public void testTakeWhile() { + assertEquals( + new Integer[] {1, 2}, + Stream.of(1, 2, 3, 4, 5).takeWhile(i -> i < 3).toArray(Integer[]::new) + ); + assertEquals(0, Stream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); + } + + public void testDropWhile() { + assertEquals( + new Integer[] {3, 4, 5}, + Stream.of(1, 2, 3, 4, 5).dropWhile(i -> i < 3).toArray(Integer[]::new) + ); + assertEquals( + new Integer[] {1, 2, 3, 4, 5}, + Stream.of(1, 2, 3, 4, 5).dropWhile(i -> i > 2).toArray(Integer[]::new) + ); + } +} From 26faec52c6f3766ddf45c23b7a3ae5c187f7e33b Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Wed, 28 Jun 2023 11:44:03 -0500 Subject: [PATCH 02/17] Remove unmodifiable methods, which belong in a later PR --- .../gwt/emul/java/util/stream/Collectors.java | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java index 5b53249799e..9129e5318a7 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java @@ -337,11 +337,6 @@ public static Collector> toList() { return toCollection(ArrayList::new); } - public static Collector> toUnmodifiableList() { - Collector> mapping = mapping(Objects::requireNonNull, toList()); - return collectingAndThen(mapping, Collections::unmodifiableList); - } - public static Collector> toMap( final Function keyMapper, final Function valueMapper) { @@ -360,27 +355,6 @@ public static Collector> toList() { return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new); } - public static Collector> toUnmodifiableMap(Function keyMapper, Function valueMapper) { - return collectingAndThen( - toMap(disallowNulls(keyMapper), disallowNulls(valueMapper)), - Collections::unmodifiableMap - ); - } - - public static Collector> toUnmodifiableMap(Function keyMapper, Function valueMapper, BinaryOperator - mergeFunction) { - return collectingAndThen( - toMap(disallowNulls(keyMapper), disallowNulls(valueMapper), mergeFunction), - Collections::unmodifiableMap - ); - } - - private static Function disallowNulls(Function func) { - return func.andThen(Objects::requireNonNull); - } - public static > Collector toMap( final Function keyMapper, final Function valueMapper, @@ -413,11 +387,6 @@ public static Collector> toSet() { ); } - public static Collector> toUnmodifiableSet() { - Collector> mapping = mapping(Objects::requireNonNull, toSet()); - return collectingAndThen(mapping, Collections::unmodifiableSet); - } - private static D streamAndCollect(Collector downstream, List list) { A a = downstream.supplier().get(); for (T t : list) { From b07159311da53bfe27400c73ffb577bc9a24fbf6 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Wed, 28 Jun 2023 13:05:15 -0500 Subject: [PATCH 03/17] Remove extra import --- user/super/com/google/gwt/emul/java/util/stream/Collectors.java | 1 - 1 file changed, 1 deletion(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java index 9129e5318a7..9176b018943 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java @@ -18,7 +18,6 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.DoubleSummaryStatistics; import java.util.HashMap; From 2af231cd4b4d865f8cbbfca39e612c3340793c96 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Wed, 28 Jun 2023 13:24:20 -0500 Subject: [PATCH 04/17] Another extra import removed --- user/super/com/google/gwt/emul/java/util/stream/Collectors.java | 1 - 1 file changed, 1 deletion(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java index 9176b018943..c2af8966c9b 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java @@ -27,7 +27,6 @@ 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; From d04d6cb932dc1abf038a0945be0866175ef06496 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Sat, 8 Jul 2023 20:10:17 -0500 Subject: [PATCH 05/17] Test case for null flatmap stream --- .../gwt/emultest/java9/util/stream/CollectorsTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java index 536af4b909f..1e7adea0814 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/CollectorsTest.java @@ -40,6 +40,15 @@ public void testFlatMapping() { applyItems(Arrays.asList("a", "b"), flatMapping, Collections.singletonList("a"), Collections.singletonList("b")); applyItems(Arrays.asList("c", "d"), flatMapping, Collections.emptyList(), Arrays.asList("c", "d")); + + Collector, ?, List> flatMappingToNull = flatMapping(items -> { + if (items.size() % 2 == 0) { + // Return null instead of empty + return null; + } + return items.stream(); + }, toList()); + applyItems(Arrays.asList("a"), flatMappingToNull, Arrays.asList("a"), Arrays.asList("b", "c")); } public void testFiltering() { From 111130eacfc6e7dee5ace84e6ef04fa4aacdd5f5 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Sat, 8 Jul 2023 19:21:16 -0500 Subject: [PATCH 06/17] "if a mapped stream is null..." there are no items to pass to acc, return early --- .../com/google/gwt/emul/java/util/stream/Collectors.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java index c2af8966c9b..60d908edb95 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Collectors.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Collectors.java @@ -175,7 +175,11 @@ public static Collector joining(CharSequence delimiter) { return new CollectorImpl<>( downstream.supplier(), (A a, T t) -> { - mapper.apply(t).forEach(u -> { + Stream stream = mapper.apply(t); + if (stream == null) { + return; + } + stream.forEach(u -> { downstream.accumulator().accept(a, u); }); }, From 26ff4290ed0ca8882bf468aac5efbfa48e6dee05 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Sun, 9 Jul 2023 11:52:09 -0500 Subject: [PATCH 07/17] Add tests for DoubleStream for infinite streams --- .../java9/util/stream/DoubleStreamTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java index 85c18706752..9e6df4b9894 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java @@ -24,9 +24,15 @@ */ public class DoubleStreamTest extends EmulTestBase { public void testIterate() { + // terminating stream, based on a predicate assertEquals( new double[] {10, 11, 12, 13, 14}, DoubleStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); + + // infinite stream, verify that it is limited by a downstream step + assertEquals( + new double[] {0, 1, 2, 3, 4}, + DoubleStream.iterate(0, i -> i + 1).limit(5).toArray()); } public void testTakeWhile() { @@ -35,6 +41,11 @@ public void testTakeWhile() { DoubleStream.of(1, 2, 3, 4, 5).takeWhile(i -> i < 3).toArray() ); assertEquals(0, DoubleStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); + + assertEquals( + new double[] {0, 1, 2, 3, 4}, + DoubleStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() + ); } public void testDropWhile() { @@ -46,5 +57,11 @@ public void testDropWhile() { new double[] {1, 2, 3, 4, 5}, DoubleStream.of(1, 2, 3, 4, 5).dropWhile(i -> i > 2).toArray() ); + + // pass an infinite stream to dropWhile, ensure it handles it + assertEquals( + new double[] {6, 7, 8, 9, 10}, + DoubleStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() + ); } } From 34c9cc2190fb6d470aed8cbeec42e8166bd82a78 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Sun, 13 Aug 2023 14:44:53 -0500 Subject: [PATCH 08/17] Fix doublestream impl, correct test off-by-one --- .../emul/java/util/stream/DoubleStream.java | 83 ++++++++++++++----- .../java9/util/stream/DoubleStreamTest.java | 2 +- 2 files changed, 62 insertions(+), 23 deletions(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java index 3ce424b4ef3..57a7b6455eb 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java @@ -210,17 +210,41 @@ 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; - } - }); + Spliterator.OfDouble prev = spliterator(); + Spliterator.OfDouble spliterator = + new Spliterators.AbstractDoubleSpliterator( + prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean drop = true; + private boolean found; + + @Override + public boolean tryAdvance(DoubleConsumer action) { + found = false; + if (drop) { + // drop items until we find one that matches + while (drop && prev.tryAdvance((double item) -> { + if (!predicate.test(item)) { + drop = false; + found = true; + action.accept(item); + } + })) { + // do nothing, work is done in tryAdvance + } + return found; + } else { + // accept one item + return prev.tryAdvance((double item) -> { + found = true; + action.accept(item); + }); + } + + // only return true if we accepted at least one item +// return found; + } + }; + return StreamSupport.doubleStream(spliterator, false); } DoubleStream filter(DoublePredicate predicate); @@ -278,17 +302,32 @@ public boolean test(double value) { DoubleSummaryStatistics summaryStatistics(); default DoubleStream takeWhile(DoublePredicate predicate) { - return filter(new DoublePredicate() { - private boolean take = true; - @Override - public boolean test(double value) { - if (!take) { - return false; - } - take = predicate.test(value); - return take; - } - }); + Spliterator.OfDouble original = spliterator(); + Spliterator.OfDouble spliterator = + new Spliterators.AbstractDoubleSpliterator( + original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean take = true; + private boolean found; + + @Override + public boolean tryAdvance(DoubleConsumer action) { + found = false; + if (!take) { + // already failed the check + return false; + } + original.tryAdvance((double item) -> { + if (predicate.test(item)) { + found = true; + action.accept(item); + } else { + take = false; + } + }); + return found; + } + }; + return StreamSupport.doubleStream(spliterator, false); } double[] toArray(); diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java index 9e6df4b9894..1ad78e037cc 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java @@ -60,7 +60,7 @@ public void testDropWhile() { // pass an infinite stream to dropWhile, ensure it handles it assertEquals( - new double[] {6, 7, 8, 9, 10}, + new double[] {5, 6, 7, 8, 9}, DoubleStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() ); } From 719e309402d59fc6a88e301c2eaec1605ca867a6 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Sun, 13 Aug 2023 17:18:03 -0500 Subject: [PATCH 09/17] Replicate tests to other types --- .../java9/util/stream/IntStreamTest.java | 16 ++++++++++++++++ .../java9/util/stream/LongStreamTest.java | 16 ++++++++++++++++ .../emultest/java9/util/stream/StreamTest.java | 15 +++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java index 3929845392c..3c45877b851 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java @@ -27,6 +27,11 @@ public void testIterate() { assertEquals( new int[] {10, 11, 12, 13, 14}, IntStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); + + // infinite stream, verify that it is limited by a downstream step + assertEquals( + new int[] {0, 1, 2, 3, 4}, + IntStream.iterate(0, i -> i + 1).limit(5).toArray()); } public void testTakeWhile() { @@ -35,6 +40,11 @@ public void testTakeWhile() { IntStream.of(1, 2, 3, 4, 5).takeWhile(i -> i < 3).toArray() ); assertEquals(0, IntStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); + + assertEquals( + new int[] {0, 1, 2, 3, 4}, + IntStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() + ); } public void testDropWhile() { @@ -46,5 +56,11 @@ public void testDropWhile() { new int[] {1, 2, 3, 4, 5}, IntStream.of(1, 2, 3, 4, 5).dropWhile(i -> i > 2).toArray() ); + + // pass an infinite stream to dropWhile, ensure it handles it + assertEquals( + new int[] {5, 6, 7, 8, 9}, + IntStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() + ); } } diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java index e7e48d921e8..5bb1061f1d2 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java @@ -27,6 +27,11 @@ public void testIterate() { assertEquals( new long[] {10, 11, 12, 13, 14}, LongStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); + + // infinite stream, verify that it is limited by a downstream step + assertEquals( + new long[] {0, 1, 2, 3, 4}, + LongStream.iterate(0, i -> i + 1).limit(5).toArray()); } public void testTakeWhile() { @@ -35,6 +40,11 @@ public void testTakeWhile() { LongStream.of(1, 2, 3, 4, 5).takeWhile(i -> i < 3).toArray() ); assertEquals(0, LongStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); + + assertEquals( + new long[] {0, 1, 2, 3, 4}, + LongStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() + ); } public void testDropWhile() { @@ -46,5 +56,11 @@ public void testDropWhile() { new long[] {1, 2, 3, 4, 5}, LongStream.of(1, 2, 3, 4, 5).dropWhile(i -> i > 2).toArray() ); + + // pass an infinite stream to dropWhile, ensure it handles it + assertEquals( + new long[] {5, 6, 7, 8, 9}, + LongStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() + ); } } diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java index 66a728f9a30..a8db0c05817 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java @@ -27,6 +27,10 @@ public void testIterate() { assertEquals( new Integer[] {10, 11, 12, 13, 14}, Stream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray(Integer[]::new)); + // infinite stream, verify that it is limited by a downstream step + assertEquals( + new Integer[] {0, 1, 2, 3, 4}, + Stream.iterate(0, i -> i + 1).limit(5).toArray()); } public void testOfNullable() { @@ -43,6 +47,11 @@ public void testTakeWhile() { Stream.of(1, 2, 3, 4, 5).takeWhile(i -> i < 3).toArray(Integer[]::new) ); assertEquals(0, Stream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); + + assertEquals( + new Integer[] {0, 1, 2, 3, 4}, + Stream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() + ); } public void testDropWhile() { @@ -54,5 +63,11 @@ public void testDropWhile() { new Integer[] {1, 2, 3, 4, 5}, Stream.of(1, 2, 3, 4, 5).dropWhile(i -> i > 2).toArray(Integer[]::new) ); + + // pass an infinite stream to dropWhile, ensure it handles it + assertEquals( + new Integer[] {5, 6, 7, 8, 9}, + Stream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() + ); } } From 8209fdd8a998b4433b2f9e6d812b8b2b1ff66276 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Sun, 13 Aug 2023 21:35:48 -0500 Subject: [PATCH 10/17] Cleanup before replicating impl Note especially that this avoids an extra lambda, saving a extra type and makeFunction call --- .../gwt/emul/java/util/stream/DoubleStream.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java index 57a7b6455eb..9af6ad5a229 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java @@ -231,17 +231,12 @@ public boolean tryAdvance(DoubleConsumer action) { })) { // do nothing, work is done in tryAdvance } + // only return true if we accepted at least one item return found; } else { - // accept one item - return prev.tryAdvance((double item) -> { - found = true; - action.accept(item); - }); + // accept one item, return result + return prev.tryAdvance(action); } - - // only return true if we accepted at least one item -// return found; } }; return StreamSupport.doubleStream(spliterator, false); From 3e901a20c2f58568f99c0b859e0d5fd4018c466c Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Sun, 13 Aug 2023 21:56:41 -0500 Subject: [PATCH 11/17] Replicate implementation, passes tests --- .../gwt/emul/java/util/stream/IntStream.java | 78 +++++++++++++------ .../gwt/emul/java/util/stream/LongStream.java | 78 +++++++++++++------ .../gwt/emul/java/util/stream/Stream.java | 78 +++++++++++++------ 3 files changed, 168 insertions(+), 66 deletions(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java index c48342d385b..ac504afd687 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java @@ -260,17 +260,36 @@ 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; - } - }); + Spliterator.OfInt prev = spliterator(); + Spliterator.OfInt spliterator = + new Spliterators.AbstractIntSpliterator( + prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean drop = true; + private boolean found; + + @Override + public boolean tryAdvance(IntConsumer action) { + found = false; + if (drop) { + // drop items until we find one that matches + while (drop && prev.tryAdvance((int item) -> { + if (!predicate.test(item)) { + drop = false; + found = true; + action.accept(item); + } + })) { + // do nothing, work is done in tryAdvance + } + // only return true if we accepted at least one item + return found; + } else { + // accept one item, return result + return prev.tryAdvance(action); + } + } + }; + return StreamSupport.intStream(spliterator, false); } IntStream filter(IntPredicate predicate); @@ -328,17 +347,32 @@ public boolean test(int value) { 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; - } - }); + Spliterator.OfInt original = spliterator(); + Spliterator.OfInt spliterator = + new Spliterators.AbstractIntSpliterator( + original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean take = true; + private boolean found; + + @Override + public boolean tryAdvance(IntConsumer action) { + found = false; + if (!take) { + // already failed the check + return false; + } + original.tryAdvance((int item) -> { + if (predicate.test(item)) { + found = true; + action.accept(item); + } else { + take = false; + } + }); + return found; + } + }; + return StreamSupport.intStream(spliterator, false); } int[] toArray(); diff --git a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java index 7f7063f6064..9be6cd99044 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java @@ -256,17 +256,36 @@ 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; - } - }); + Spliterator.OfLong prev = spliterator(); + Spliterator.OfLong spliterator = + new Spliterators.AbstractLongSpliterator( + prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean drop = true; + private boolean found; + + @Override + public boolean tryAdvance(LongConsumer action) { + found = false; + if (drop) { + // drop items until we find one that matches + while (drop && prev.tryAdvance((long item) -> { + if (!predicate.test(item)) { + drop = false; + found = true; + action.accept(item); + } + })) { + // do nothing, work is done in tryAdvance + } + // only return true if we accepted at least one item + return found; + } else { + // accept one item, return result + return prev.tryAdvance(action); + } + } + }; + return StreamSupport.longStream(spliterator, false); } LongStream filter(LongPredicate predicate); @@ -324,17 +343,32 @@ public boolean test(long value) { 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; - } - }); + Spliterator.OfLong original = spliterator(); + Spliterator.OfLong spliterator = + new Spliterators.AbstractLongSpliterator( + original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean take = true; + private boolean found; + + @Override + public boolean tryAdvance(LongConsumer action) { + found = false; + if (!take) { + // already failed the check + return false; + } + original.tryAdvance((long item) -> { + if (predicate.test(item)) { + found = true; + action.accept(item); + } else { + take = false; + } + }); + return found; + } + }; + return StreamSupport.longStream(spliterator, false); } long[] toArray(); diff --git a/user/super/com/google/gwt/emul/java/util/stream/Stream.java b/user/super/com/google/gwt/emul/java/util/stream/Stream.java index bcec1632608..a993f83e018 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Stream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Stream.java @@ -221,17 +221,36 @@ R collect( Stream distinct(); default Stream dropWhile(Predicate predicate) { - return filter(new Predicate() { - private boolean drop = true; - @Override - public boolean test(T t) { - if (!drop) { - return true; - } - drop = predicate.test(t); - return !drop; - } - }); + Spliterator prev = spliterator(); + Spliterator spliterator = + new Spliterators.AbstractSpliterator( + prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean drop = true; + private boolean found; + + @Override + public boolean tryAdvance(Consumer action) { + found = false; + if (drop) { + // drop items until we find one that matches + while (drop && prev.tryAdvance(item -> { + if (!predicate.test(item)) { + drop = false; + found = true; + action.accept(item); + } + })) { + // do nothing, work is done in tryAdvance + } + // only return true if we accepted at least one item + return found; + } else { + // accept one item, return result + return prev.tryAdvance(action); + } + } + }; + return StreamSupport.stream(spliterator, false); } Stream filter(Predicate predicate); @@ -283,17 +302,32 @@ public boolean test(T t) { Stream sorted(Comparator comparator); default Stream takeWhile(Predicate predicate) { - return filter(new Predicate() { - private boolean take = true; - @Override - public boolean test(T t) { - if (!take) { - return false; - } - take = predicate.test(t); - return take; - } - }); + Spliterator original = spliterator(); + Spliterator spliterator = + new Spliterators.AbstractSpliterator( + original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean take = true; + private boolean found; + + @Override + public boolean tryAdvance(Consumer action) { + found = false; + if (!take) { + // already failed the check + return false; + } + original.tryAdvance(item -> { + if (predicate.test(item)) { + found = true; + action.accept(item); + } else { + take = false; + } + }); + return found; + } + }; + return StreamSupport.stream(spliterator, false); } Object[] toArray(); From bbc554eb9ef248681b7b6d7de16795252d364a4f Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Sun, 13 Aug 2023 22:04:38 -0500 Subject: [PATCH 12/17] Make indentation more consistent with existing code --- .../emul/java/util/stream/DoubleStream.java | 94 +++++++++---------- .../gwt/emul/java/util/stream/IntStream.java | 94 +++++++++---------- .../gwt/emul/java/util/stream/LongStream.java | 94 +++++++++---------- .../gwt/emul/java/util/stream/Stream.java | 94 +++++++++---------- .../java9/util/stream/DoubleStreamTest.java | 8 +- .../java9/util/stream/IntStreamTest.java | 8 +- .../java9/util/stream/LongStreamTest.java | 12 +-- .../java9/util/stream/StreamTest.java | 12 +-- 8 files changed, 208 insertions(+), 208 deletions(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java index 9af6ad5a229..bb71f0d70d8 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java @@ -212,33 +212,33 @@ static DoubleStream of(double t) { default DoubleStream dropWhile(DoublePredicate predicate) { Spliterator.OfDouble prev = spliterator(); Spliterator.OfDouble spliterator = - new Spliterators.AbstractDoubleSpliterator( - prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { - private boolean drop = true; - private boolean found; - - @Override - public boolean tryAdvance(DoubleConsumer action) { - found = false; - if (drop) { - // drop items until we find one that matches - while (drop && prev.tryAdvance((double item) -> { - if (!predicate.test(item)) { - drop = false; - found = true; - action.accept(item); - } - })) { - // do nothing, work is done in tryAdvance - } - // only return true if we accepted at least one item - return found; - } else { - // accept one item, return result - return prev.tryAdvance(action); + new Spliterators.AbstractDoubleSpliterator( + prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean drop = true; + private boolean found; + + @Override + public boolean tryAdvance(DoubleConsumer action) { + found = false; + if (drop) { + // drop items until we find one that matches + while (drop && prev.tryAdvance((double item) -> { + if (!predicate.test(item)) { + drop = false; + found = true; + action.accept(item); } + })) { + // do nothing, work is done in tryAdvance } - }; + // only return true if we accepted at least one item + return found; + } else { + // accept one item, return result + return prev.tryAdvance(action); + } + } + }; return StreamSupport.doubleStream(spliterator, false); } @@ -299,29 +299,29 @@ public boolean tryAdvance(DoubleConsumer action) { default DoubleStream takeWhile(DoublePredicate predicate) { Spliterator.OfDouble original = spliterator(); Spliterator.OfDouble spliterator = - new Spliterators.AbstractDoubleSpliterator( - original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { - private boolean take = true; - private boolean found; - - @Override - public boolean tryAdvance(DoubleConsumer action) { - found = false; - if (!take) { - // already failed the check - return false; - } - original.tryAdvance((double item) -> { - if (predicate.test(item)) { - found = true; - action.accept(item); - } else { - take = false; - } - }); - return found; + new Spliterators.AbstractDoubleSpliterator( + original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean take = true; + private boolean found; + + @Override + public boolean tryAdvance(DoubleConsumer action) { + found = false; + if (!take) { + // already failed the check + return false; + } + original.tryAdvance((double item) -> { + if (predicate.test(item)) { + found = true; + action.accept(item); + } else { + take = false; } - }; + }); + return found; + } + }; return StreamSupport.doubleStream(spliterator, false); } diff --git a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java index ac504afd687..0060ee50f03 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java @@ -262,33 +262,33 @@ public boolean tryAdvance(IntConsumer action) { default IntStream dropWhile(IntPredicate predicate) { Spliterator.OfInt prev = spliterator(); Spliterator.OfInt spliterator = - new Spliterators.AbstractIntSpliterator( - prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { - private boolean drop = true; - private boolean found; - - @Override - public boolean tryAdvance(IntConsumer action) { - found = false; - if (drop) { - // drop items until we find one that matches - while (drop && prev.tryAdvance((int item) -> { - if (!predicate.test(item)) { - drop = false; - found = true; - action.accept(item); - } - })) { - // do nothing, work is done in tryAdvance - } - // only return true if we accepted at least one item - return found; - } else { - // accept one item, return result - return prev.tryAdvance(action); + new Spliterators.AbstractIntSpliterator( + prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean drop = true; + private boolean found; + + @Override + public boolean tryAdvance(IntConsumer action) { + found = false; + if (drop) { + // drop items until we find one that matches + while (drop && prev.tryAdvance((int item) -> { + if (!predicate.test(item)) { + drop = false; + found = true; + action.accept(item); } + })) { + // do nothing, work is done in tryAdvance } - }; + // only return true if we accepted at least one item + return found; + } else { + // accept one item, return result + return prev.tryAdvance(action); + } + } + }; return StreamSupport.intStream(spliterator, false); } @@ -349,29 +349,29 @@ public boolean tryAdvance(IntConsumer action) { default IntStream takeWhile(IntPredicate predicate) { Spliterator.OfInt original = spliterator(); Spliterator.OfInt spliterator = - new Spliterators.AbstractIntSpliterator( - original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { - private boolean take = true; - private boolean found; - - @Override - public boolean tryAdvance(IntConsumer action) { - found = false; - if (!take) { - // already failed the check - return false; - } - original.tryAdvance((int item) -> { - if (predicate.test(item)) { - found = true; - action.accept(item); - } else { - take = false; - } - }); - return found; + new Spliterators.AbstractIntSpliterator( + original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean take = true; + private boolean found; + + @Override + public boolean tryAdvance(IntConsumer action) { + found = false; + if (!take) { + // already failed the check + return false; + } + original.tryAdvance((int item) -> { + if (predicate.test(item)) { + found = true; + action.accept(item); + } else { + take = false; } - }; + }); + return found; + } + }; return StreamSupport.intStream(spliterator, false); } diff --git a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java index 9be6cd99044..488241c8fc5 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java @@ -258,33 +258,33 @@ public boolean tryAdvance(LongConsumer action) { default LongStream dropWhile(LongPredicate predicate) { Spliterator.OfLong prev = spliterator(); Spliterator.OfLong spliterator = - new Spliterators.AbstractLongSpliterator( - prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { - private boolean drop = true; - private boolean found; - - @Override - public boolean tryAdvance(LongConsumer action) { - found = false; - if (drop) { - // drop items until we find one that matches - while (drop && prev.tryAdvance((long item) -> { - if (!predicate.test(item)) { - drop = false; - found = true; - action.accept(item); - } - })) { - // do nothing, work is done in tryAdvance - } - // only return true if we accepted at least one item - return found; - } else { - // accept one item, return result - return prev.tryAdvance(action); + new Spliterators.AbstractLongSpliterator( + prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean drop = true; + private boolean found; + + @Override + public boolean tryAdvance(LongConsumer action) { + found = false; + if (drop) { + // drop items until we find one that matches + while (drop && prev.tryAdvance((long item) -> { + if (!predicate.test(item)) { + drop = false; + found = true; + action.accept(item); } + })) { + // do nothing, work is done in tryAdvance } - }; + // only return true if we accepted at least one item + return found; + } else { + // accept one item, return result + return prev.tryAdvance(action); + } + } + }; return StreamSupport.longStream(spliterator, false); } @@ -345,29 +345,29 @@ public boolean tryAdvance(LongConsumer action) { default LongStream takeWhile(LongPredicate predicate) { Spliterator.OfLong original = spliterator(); Spliterator.OfLong spliterator = - new Spliterators.AbstractLongSpliterator( - original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { - private boolean take = true; - private boolean found; - - @Override - public boolean tryAdvance(LongConsumer action) { - found = false; - if (!take) { - // already failed the check - return false; - } - original.tryAdvance((long item) -> { - if (predicate.test(item)) { - found = true; - action.accept(item); - } else { - take = false; - } - }); - return found; + new Spliterators.AbstractLongSpliterator( + original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean take = true; + private boolean found; + + @Override + public boolean tryAdvance(LongConsumer action) { + found = false; + if (!take) { + // already failed the check + return false; + } + original.tryAdvance((long item) -> { + if (predicate.test(item)) { + found = true; + action.accept(item); + } else { + take = false; } - }; + }); + return found; + } + }; return StreamSupport.longStream(spliterator, false); } diff --git a/user/super/com/google/gwt/emul/java/util/stream/Stream.java b/user/super/com/google/gwt/emul/java/util/stream/Stream.java index a993f83e018..5cedd7bc9f1 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Stream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Stream.java @@ -223,33 +223,33 @@ R collect( default Stream dropWhile(Predicate predicate) { Spliterator prev = spliterator(); Spliterator spliterator = - new Spliterators.AbstractSpliterator( - prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { - private boolean drop = true; - private boolean found; - - @Override - public boolean tryAdvance(Consumer action) { - found = false; - if (drop) { - // drop items until we find one that matches - while (drop && prev.tryAdvance(item -> { - if (!predicate.test(item)) { - drop = false; - found = true; - action.accept(item); - } - })) { - // do nothing, work is done in tryAdvance - } - // only return true if we accepted at least one item - return found; - } else { - // accept one item, return result - return prev.tryAdvance(action); + new Spliterators.AbstractSpliterator( + prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean drop = true; + private boolean found; + + @Override + public boolean tryAdvance(Consumer action) { + found = false; + if (drop) { + // drop items until we find one that matches + while (drop && prev.tryAdvance(item -> { + if (!predicate.test(item)) { + drop = false; + found = true; + action.accept(item); } + })) { + // do nothing, work is done in tryAdvance } - }; + // only return true if we accepted at least one item + return found; + } else { + // accept one item, return result + return prev.tryAdvance(action); + } + } + }; return StreamSupport.stream(spliterator, false); } @@ -304,29 +304,29 @@ public boolean tryAdvance(Consumer action) { default Stream takeWhile(Predicate predicate) { Spliterator original = spliterator(); Spliterator spliterator = - new Spliterators.AbstractSpliterator( - original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { - private boolean take = true; - private boolean found; - - @Override - public boolean tryAdvance(Consumer action) { - found = false; - if (!take) { - // already failed the check - return false; - } - original.tryAdvance(item -> { - if (predicate.test(item)) { - found = true; - action.accept(item); - } else { - take = false; - } - }); - return found; + new Spliterators.AbstractSpliterator( + original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + private boolean take = true; + private boolean found; + + @Override + public boolean tryAdvance(Consumer action) { + found = false; + if (!take) { + // already failed the check + return false; + } + original.tryAdvance(item -> { + if (predicate.test(item)) { + found = true; + action.accept(item); + } else { + take = false; } - }; + }); + return found; + } + }; return StreamSupport.stream(spliterator, false); } diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java index 1ad78e037cc..48bfe3aa16d 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java @@ -43,8 +43,8 @@ public void testTakeWhile() { assertEquals(0, DoubleStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); assertEquals( - new double[] {0, 1, 2, 3, 4}, - DoubleStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() + new double[] {0, 1, 2, 3, 4}, + DoubleStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() ); } @@ -60,8 +60,8 @@ public void testDropWhile() { // pass an infinite stream to dropWhile, ensure it handles it assertEquals( - new double[] {5, 6, 7, 8, 9}, - DoubleStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() + new double[] {5, 6, 7, 8, 9}, + DoubleStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() ); } } diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java index 3c45877b851..c6e9c537ffa 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java @@ -42,8 +42,8 @@ public void testTakeWhile() { assertEquals(0, IntStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); assertEquals( - new int[] {0, 1, 2, 3, 4}, - IntStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() + new int[] {0, 1, 2, 3, 4}, + IntStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() ); } @@ -59,8 +59,8 @@ public void testDropWhile() { // pass an infinite stream to dropWhile, ensure it handles it assertEquals( - new int[] {5, 6, 7, 8, 9}, - IntStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() + new int[] {5, 6, 7, 8, 9}, + IntStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() ); } } diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java index 5bb1061f1d2..089b0c61f57 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java @@ -30,8 +30,8 @@ public void testIterate() { // infinite stream, verify that it is limited by a downstream step assertEquals( - new long[] {0, 1, 2, 3, 4}, - LongStream.iterate(0, i -> i + 1).limit(5).toArray()); + new long[] {0, 1, 2, 3, 4}, + LongStream.iterate(0, i -> i + 1).limit(5).toArray()); } public void testTakeWhile() { @@ -42,8 +42,8 @@ public void testTakeWhile() { assertEquals(0, LongStream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); assertEquals( - new long[] {0, 1, 2, 3, 4}, - LongStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() + new long[] {0, 1, 2, 3, 4}, + LongStream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() ); } @@ -59,8 +59,8 @@ public void testDropWhile() { // pass an infinite stream to dropWhile, ensure it handles it assertEquals( - new long[] {5, 6, 7, 8, 9}, - LongStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() + new long[] {5, 6, 7, 8, 9}, + LongStream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() ); } } diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java index a8db0c05817..04622bf38dd 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java @@ -29,8 +29,8 @@ public void testIterate() { Stream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray(Integer[]::new)); // infinite stream, verify that it is limited by a downstream step assertEquals( - new Integer[] {0, 1, 2, 3, 4}, - Stream.iterate(0, i -> i + 1).limit(5).toArray()); + new Integer[] {0, 1, 2, 3, 4}, + Stream.iterate(0, i -> i + 1).limit(5).toArray()); } public void testOfNullable() { @@ -49,8 +49,8 @@ public void testTakeWhile() { assertEquals(0, Stream.of(1, 2, 3, 4, 5).takeWhile(i -> i > 2).count()); assertEquals( - new Integer[] {0, 1, 2, 3, 4}, - Stream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() + new Integer[] {0, 1, 2, 3, 4}, + Stream.iterate(0, i -> i + 1).takeWhile(i -> i < 5).toArray() ); } @@ -66,8 +66,8 @@ public void testDropWhile() { // pass an infinite stream to dropWhile, ensure it handles it assertEquals( - new Integer[] {5, 6, 7, 8, 9}, - Stream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() + new Integer[] {5, 6, 7, 8, 9}, + Stream.iterate(0, i -> i + 1).dropWhile(i -> i < 5).limit(5).toArray() ); } } From a4eeaf153f7b07ea14bde9e32c531bae70edaedc Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Tue, 7 Nov 2023 12:11:16 -0600 Subject: [PATCH 13/17] Add tests for old/new iterate functions being called correctly Also moved newly created test for old behavior to java8 class --- .../java8/util/stream/DoubleStreamTest.java | 16 +++++++ .../java8/util/stream/IntStreamTest.java | 16 +++++++ .../java8/util/stream/LongStreamTest.java | 16 +++++++ .../java8/util/stream/StreamTest.java | 16 +++++++ .../java9/util/stream/DoubleStreamTest.java | 47 +++++++++++++++++-- .../java9/util/stream/IntStreamTest.java | 45 ++++++++++++++++-- .../java9/util/stream/LongStreamTest.java | 45 ++++++++++++++++-- .../java9/util/stream/StreamTest.java | 46 ++++++++++++++++-- 8 files changed, 230 insertions(+), 17 deletions(-) diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/DoubleStreamTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/DoubleStreamTest.java index 68140c7ada3..66389b4665a 100644 --- a/user/test/com/google/gwt/emultest/java8/util/stream/DoubleStreamTest.java +++ b/user/test/com/google/gwt/emultest/java8/util/stream/DoubleStreamTest.java @@ -117,6 +117,22 @@ public void testIterate() { assertEquals( new double[] {10d, 11d, 12d, 13d, 14d}, DoubleStream.iterate(0d, l -> l + 1d).skip(10).limit(5).toArray()); + + // Infinite stream, verify that it is correctly limited by a downstream step + assertEquals( + new double[] {0, 1, 2, 3, 4}, + DoubleStream.iterate(0, i -> i + 1).limit(5).toArray()); + + // Check that the function is called the correct number of times + int[] calledCount = {0}; + double[] array = DoubleStream.iterate(0, val -> { + calledCount[0]++; + return val + 1; + }).limit(5).toArray(); + // Verify that the function was called for each value after the seed + assertEquals(array.length - 1, calledCount[0]); + // Sanity check the values returned + assertEquals(new double[] {0, 1, 2, 3, 4}, array); } public void testGenerate() { diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/IntStreamTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/IntStreamTest.java index 5fea722124a..a386fd24775 100644 --- a/user/test/com/google/gwt/emultest/java8/util/stream/IntStreamTest.java +++ b/user/test/com/google/gwt/emultest/java8/util/stream/IntStreamTest.java @@ -116,6 +116,22 @@ public void testIterate() { assertEquals( new int[] {10, 11, 12, 13, 14}, IntStream.iterate(0, i -> i + 1).skip(10).limit(5).toArray()); + + // Infinite stream, verify that it is correctly limited by a downstream step + assertEquals( + new int[] {0, 1, 2, 3, 4}, + IntStream.iterate(0, i -> i + 1).limit(5).toArray()); + + // Check that the function is called the correct number of times + int[] calledCount = {0}; + int[] array = IntStream.iterate(0, val -> { + calledCount[0]++; + return val + 1; + }).limit(5).toArray(); + // Verify that the function was called for each value after the seed + assertEquals(array.length - 1, calledCount[0]); + // Sanity check the values returned + assertEquals(new int[] {0, 1, 2, 3, 4}, array); } public void testGenerate() { diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/LongStreamTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/LongStreamTest.java index ff764775474..e73cf0af015 100644 --- a/user/test/com/google/gwt/emultest/java8/util/stream/LongStreamTest.java +++ b/user/test/com/google/gwt/emultest/java8/util/stream/LongStreamTest.java @@ -115,6 +115,22 @@ public void testIterate() { assertEquals( new long[] {10L, 11L, 12L, 13L, 14L}, LongStream.iterate(0L, l -> l + 1L).skip(10).limit(5).toArray()); + + // Infinite stream, verify that it is correctly limited by a downstream step + assertEquals( + new long[] {0, 1, 2, 3, 4}, + LongStream.iterate(0, i -> i + 1).limit(5).toArray()); + + // Check that the function is called the correct number of times + int[] calledCount = {0}; + long[] array = LongStream.iterate(0, val -> { + calledCount[0]++; + return val + 1; + }).limit(5).toArray(); + // Verify that the function was called for each value after the seed + assertEquals(array.length - 1, calledCount[0]); + // Sanity check the values returned + assertEquals(new long[] {0, 1, 2, 3, 4}, array); } public void testGenerate() { diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/StreamTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/StreamTest.java index 08c4c897398..f1aea123f02 100644 --- a/user/test/com/google/gwt/emultest/java8/util/stream/StreamTest.java +++ b/user/test/com/google/gwt/emultest/java8/util/stream/StreamTest.java @@ -132,6 +132,22 @@ public void testIterate() { assertEquals( new Integer[] {10, 11, 12, 13, 14}, Stream.iterate(0, i -> i + 1).skip(10).limit(5).toArray(Integer[]::new)); + + // Infinite stream, verify that it is correctly limited by a downstream step + assertEquals( + new Integer[] {0, 1, 2, 3, 4}, + Stream.iterate(0, i -> i + 1).limit(5).toArray()); + + // Check that the function is called the correct number of times + int[] calledCount = {0}; + Integer[] array = Stream.iterate(0, val -> { + calledCount[0]++; + return val + 1; + }).limit(5).toArray(Integer[]::new); + // Verify that the function was called for each value after the seed + assertEquals(array.length - 1, calledCount[0]); + // Sanity check the values returned + assertEquals(new Integer[] {0, 1, 2, 3, 4}, array); } public void testGenerate() { diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java index 48bfe3aa16d..d3b1585567f 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/DoubleStreamTest.java @@ -24,15 +24,52 @@ */ public class DoubleStreamTest extends EmulTestBase { public void testIterate() { - // terminating stream, based on a predicate + // Terminating stream, based on a predicate assertEquals( new double[] {10, 11, 12, 13, 14}, DoubleStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); - // infinite stream, verify that it is limited by a downstream step - assertEquals( - new double[] {0, 1, 2, 3, 4}, - DoubleStream.iterate(0, i -> i + 1).limit(5).toArray()); + // Check that the functions are called the correct number of times with a hasNext limiting + // stream size + { + int[] nextCalledCount = {0}; + int[] hasNextCalledCount = {0}; + double[] array = DoubleStream.iterate(0, val -> { + hasNextCalledCount[0]++; + return val < 5; + }, val -> { + nextCalledCount[0]++; + return val + 1; + }).toArray(); + + // Verify that the next was called for each value after the seed (plus one for testing that + // the stream is over) + assertEquals(array.length, nextCalledCount[0]); + // Verify that the hasNext function was called once for each value (plus one as above) + assertEquals(array.length + 1, hasNextCalledCount[0]); + // Sanity check the values returned + assertEquals(new double[]{0, 1, 2, 3, 4}, array); + } + + // Same test repeated, but instead limit() will stop the stream from continuing + { + int[] nextCalledCount = {0}; + int[] hasNextCalledCount = {0}; + double[] array = DoubleStream.iterate(0, val -> { + hasNextCalledCount[0]++; + return val < 5; + }, val -> { + nextCalledCount[0]++; + return val + 1; + }).limit(3).toArray(); + + // Verify that the next was called for each value after the seed + assertEquals(array.length - 1, nextCalledCount[0]); + // Verify that the hasNext function was called once for each value + assertEquals(array.length, hasNextCalledCount[0]); + // Sanity check the values returned + assertEquals(new double[]{0, 1, 2}, array); + } } public void testTakeWhile() { diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java index c6e9c537ffa..ced92ef0d76 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/IntStreamTest.java @@ -28,10 +28,47 @@ public void testIterate() { new int[] {10, 11, 12, 13, 14}, IntStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); - // infinite stream, verify that it is limited by a downstream step - assertEquals( - new int[] {0, 1, 2, 3, 4}, - IntStream.iterate(0, i -> i + 1).limit(5).toArray()); + // Check that the functions are called the correct number of times with a hasNext limiting + // stream size + { + int[] nextCalledCount = {0}; + int[] hasNextCalledCount = {0}; + int[] array = IntStream.iterate(0, val -> { + hasNextCalledCount[0]++; + return val < 5; + }, val -> { + nextCalledCount[0]++; + return val + 1; + }).toArray(); + + // Verify that the next was called for each value after the seed (plus one for testing that + // the stream is over) + assertEquals(array.length, nextCalledCount[0]); + // Verify that the hasNext function was called once for each value (plus one as above) + assertEquals(array.length + 1, hasNextCalledCount[0]); + // Sanity check the values returned + assertEquals(new int[]{0, 1, 2, 3, 4}, array); + } + + // Same test repeated, but instead limit() will stop the stream from continuing + { + int[] nextCalledCount = {0}; + int[] hasNextCalledCount = {0}; + int[] array = IntStream.iterate(0, val -> { + hasNextCalledCount[0]++; + return val < 5; + }, val -> { + nextCalledCount[0]++; + return val + 1; + }).limit(3).toArray(); + + // Verify that the next was called for each value after the seed + assertEquals(array.length - 1, nextCalledCount[0]); + // Verify that the hasNext function was called once for each value + assertEquals(array.length, hasNextCalledCount[0]); + // Sanity check the values returned + assertEquals(new int[]{0, 1, 2}, array); + } } public void testTakeWhile() { diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java index 089b0c61f57..26ff6b66757 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/LongStreamTest.java @@ -28,10 +28,47 @@ public void testIterate() { new long[] {10, 11, 12, 13, 14}, LongStream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray()); - // infinite stream, verify that it is limited by a downstream step - assertEquals( - new long[] {0, 1, 2, 3, 4}, - LongStream.iterate(0, i -> i + 1).limit(5).toArray()); + // Check that the functions are called the correct number of times with a hasNext limiting + // stream size + { + int[] nextCalledCount = {0}; + int[] hasNextCalledCount = {0}; + long[] array = LongStream.iterate(0, val -> { + hasNextCalledCount[0]++; + return val < 5; + }, val -> { + nextCalledCount[0]++; + return val + 1; + }).toArray(); + + // Verify that the next was called for each value after the seed (plus one for testing that + // the stream is over) + assertEquals(array.length, nextCalledCount[0]); + // Verify that the hasNext function was called once for each value (plus one as above) + assertEquals(array.length + 1, hasNextCalledCount[0]); + // Sanity check the values returned + assertEquals(new long[]{0, 1, 2, 3, 4}, array); + } + + // Same test repeated, but instead limit() will stop the stream from continuing + { + int[] nextCalledCount = {0}; + int[] hasNextCalledCount = {0}; + long[] array = LongStream.iterate(0, val -> { + hasNextCalledCount[0]++; + return val < 5; + }, val -> { + nextCalledCount[0]++; + return val + 1; + }).limit(3).toArray(); + + // Verify that the next was called for each value after the seed + assertEquals(array.length - 1, nextCalledCount[0]); + // Verify that the hasNext function was called once for each value + assertEquals(array.length, hasNextCalledCount[0]); + // Sanity check the values returned + assertEquals(new long[]{0, 1, 2}, array); + } } public void testTakeWhile() { diff --git a/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java b/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java index 04622bf38dd..fb712ec7806 100644 --- a/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java +++ b/user/test/com/google/gwt/emultest/java9/util/stream/StreamTest.java @@ -27,10 +27,48 @@ public void testIterate() { assertEquals( new Integer[] {10, 11, 12, 13, 14}, Stream.iterate(0, i -> i < 15, i -> i + 1).skip(10).toArray(Integer[]::new)); - // infinite stream, verify that it is limited by a downstream step - assertEquals( - new Integer[] {0, 1, 2, 3, 4}, - Stream.iterate(0, i -> i + 1).limit(5).toArray()); + + // Check that the functions are called the correct number of times with a hasNext limiting + // stream size + { + int[] nextCalledCount = {0}; + int[] hasNextCalledCount = {0}; + Integer[] array = Stream.iterate(0, val -> { + hasNextCalledCount[0]++; + return val < 5; + }, val -> { + nextCalledCount[0]++; + return val + 1; + }).toArray(Integer[]::new); + + // Verify that the next was called for each value after the seed (plus one for testing that + // the stream is over) + assertEquals(array.length, nextCalledCount[0]); + // Verify that the hasNext function was called once for each value (plus one as above) + assertEquals(array.length + 1, hasNextCalledCount[0]); + // Sanity check the values returned + assertEquals(new Integer[]{0, 1, 2, 3, 4}, array); + } + + // Same test repeated, but instead limit() will stop the stream from continuing + { + int[] nextCalledCount = {0}; + int[] hasNextCalledCount = {0}; + Integer[] array = Stream.iterate(0, val -> { + hasNextCalledCount[0]++; + return val < 5; + }, val -> { + nextCalledCount[0]++; + return val + 1; + }).limit(3).toArray(Integer[]::new); + + // Verify that the next was called for each value after the seed + assertEquals(array.length - 1, nextCalledCount[0]); + // Verify that the hasNext function was called once for each value + assertEquals(array.length, hasNextCalledCount[0]); + // Sanity check the values returned + assertEquals(new Integer[]{0, 1, 2}, array); + } } public void testOfNullable() { From c79ddd89b579b6320a3ee616f7aeb82c63b0db26 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Tue, 7 Nov 2023 13:09:29 -0600 Subject: [PATCH 14/17] Correct Stream impl, to be generalized --- .../google/gwt/emul/java/util/stream/Stream.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/Stream.java b/user/super/com/google/gwt/emul/java/util/stream/Stream.java index 5cedd7bc9f1..68bc6372a83 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Stream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Stream.java @@ -153,12 +153,17 @@ static Stream iterate(T seed, UnaryOperator f) { AbstractSpliterator spliterator = new Spliterators.AbstractSpliterator( Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) { + private boolean first = true; private T next = seed; @Override public boolean tryAdvance(Consumer action) { + if (!first) { + next = f.apply(next); + } + first = false; + action.accept(next); - next = f.apply(next); return true; } }; @@ -169,6 +174,7 @@ static Stream iterate(T seed, Predicate hasNext, UnaryOperator AbstractSpliterator spliterator = new Spliterators.AbstractSpliterator( Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) { + private boolean first = true; private T next = seed; private boolean terminated = false; @@ -177,12 +183,16 @@ public boolean tryAdvance(Consumer 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; } }; From e86b8144885925c7b9df616bc1efa48dfc1b344f Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Tue, 7 Nov 2023 13:20:23 -0600 Subject: [PATCH 15/17] Replicate impl to other types, share iterate() implementation --- .../emul/java/util/stream/DoubleStream.java | 22 ++++++------------ .../gwt/emul/java/util/stream/IntStream.java | 23 ++++++------------- .../gwt/emul/java/util/stream/LongStream.java | 21 ++++++----------- .../gwt/emul/java/util/stream/Stream.java | 19 +-------------- 4 files changed, 22 insertions(+), 63 deletions(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java index bb71f0d70d8..e5b4c098b4f 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java @@ -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; @@ -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; } }; diff --git a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java index 0060ee50f03..4aa4f864d85 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java @@ -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; @@ -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; } }; diff --git a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java index 488241c8fc5..d1589fb4f94 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java @@ -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; @@ -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; } }; diff --git a/user/super/com/google/gwt/emul/java/util/stream/Stream.java b/user/super/com/google/gwt/emul/java/util/stream/Stream.java index 68bc6372a83..3f377c72bed 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Stream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Stream.java @@ -150,24 +150,7 @@ public boolean tryAdvance(Consumer action) { } static Stream iterate(T seed, UnaryOperator f) { - AbstractSpliterator spliterator = - new Spliterators.AbstractSpliterator( - Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED) { - private boolean first = true; - private T next = seed; - - @Override - public boolean tryAdvance(Consumer 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 Stream iterate(T seed, Predicate hasNext, UnaryOperator f) { From 8f3fc96596dcb84f48b2fe3c68e9302d26ae55e4 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Tue, 7 Nov 2023 13:41:51 -0600 Subject: [PATCH 16/17] Fix checkstyle line wraps --- .../google/gwt/emul/java/util/stream/DoubleStream.java | 8 ++++---- .../com/google/gwt/emul/java/util/stream/IntStream.java | 8 ++++---- .../com/google/gwt/emul/java/util/stream/LongStream.java | 8 ++++---- .../com/google/gwt/emul/java/util/stream/Stream.java | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java index e5b4c098b4f..c0d81589a49 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/DoubleStream.java @@ -204,8 +204,8 @@ static DoubleStream of(double t) { default DoubleStream dropWhile(DoublePredicate predicate) { Spliterator.OfDouble prev = spliterator(); Spliterator.OfDouble spliterator = - new Spliterators.AbstractDoubleSpliterator( - prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + new Spliterators.AbstractDoubleSpliterator(prev.estimateSize(), + prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { private boolean drop = true; private boolean found; @@ -291,8 +291,8 @@ public boolean tryAdvance(DoubleConsumer action) { default DoubleStream takeWhile(DoublePredicate predicate) { Spliterator.OfDouble original = spliterator(); Spliterator.OfDouble spliterator = - new Spliterators.AbstractDoubleSpliterator( - original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + new Spliterators.AbstractDoubleSpliterator(original.estimateSize(), + original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { private boolean take = true; private boolean found; diff --git a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java index 4aa4f864d85..bac1b49844d 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/IntStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/IntStream.java @@ -253,8 +253,8 @@ public boolean tryAdvance(IntConsumer action) { default IntStream dropWhile(IntPredicate predicate) { Spliterator.OfInt prev = spliterator(); Spliterator.OfInt spliterator = - new Spliterators.AbstractIntSpliterator( - prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + new Spliterators.AbstractIntSpliterator(prev.estimateSize(), + prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { private boolean drop = true; private boolean found; @@ -340,8 +340,8 @@ public boolean tryAdvance(IntConsumer action) { default IntStream takeWhile(IntPredicate predicate) { Spliterator.OfInt original = spliterator(); Spliterator.OfInt spliterator = - new Spliterators.AbstractIntSpliterator( - original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + new Spliterators.AbstractIntSpliterator(original.estimateSize(), + original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { private boolean take = true; private boolean found; diff --git a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java index d1589fb4f94..821c2f7f40e 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/LongStream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/LongStream.java @@ -251,8 +251,8 @@ public boolean tryAdvance(LongConsumer action) { default LongStream dropWhile(LongPredicate predicate) { Spliterator.OfLong prev = spliterator(); Spliterator.OfLong spliterator = - new Spliterators.AbstractLongSpliterator( - prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + new Spliterators.AbstractLongSpliterator(prev.estimateSize(), + prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { private boolean drop = true; private boolean found; @@ -338,8 +338,8 @@ public boolean tryAdvance(LongConsumer action) { default LongStream takeWhile(LongPredicate predicate) { Spliterator.OfLong original = spliterator(); Spliterator.OfLong spliterator = - new Spliterators.AbstractLongSpliterator( - original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + new Spliterators.AbstractLongSpliterator(original.estimateSize(), + original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { private boolean take = true; private boolean found; diff --git a/user/super/com/google/gwt/emul/java/util/stream/Stream.java b/user/super/com/google/gwt/emul/java/util/stream/Stream.java index 3f377c72bed..386d3293915 100644 --- a/user/super/com/google/gwt/emul/java/util/stream/Stream.java +++ b/user/super/com/google/gwt/emul/java/util/stream/Stream.java @@ -216,8 +216,8 @@ R collect( default Stream dropWhile(Predicate predicate) { Spliterator prev = spliterator(); Spliterator spliterator = - new Spliterators.AbstractSpliterator( - prev.estimateSize(), prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + new Spliterators.AbstractSpliterator(prev.estimateSize(), + prev.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { private boolean drop = true; private boolean found; @@ -297,8 +297,8 @@ public boolean tryAdvance(Consumer action) { default Stream takeWhile(Predicate predicate) { Spliterator original = spliterator(); Spliterator spliterator = - new Spliterators.AbstractSpliterator( - original.estimateSize(), original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { + new Spliterators.AbstractSpliterator(original.estimateSize(), + original.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED)) { private boolean take = true; private boolean found; From f0eb1dfbf1eaef252a23060b1895d61149dc7e97 Mon Sep 17 00:00:00 2001 From: Colin Alworth Date: Tue, 7 Nov 2023 19:02:22 -0600 Subject: [PATCH 17/17] Exclude tests that fail in Java8 legacy dev mode Note that these would pass in legacy dev mode in Java 11/17, but we don't run legacy dev mode tests for those JVMs for a variety of reasons. --- .../gwt/emultest/java8/util/stream/DoubleStreamTest.java | 6 ++++++ .../gwt/emultest/java8/util/stream/IntStreamTest.java | 6 ++++++ .../gwt/emultest/java8/util/stream/LongStreamTest.java | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/DoubleStreamTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/DoubleStreamTest.java index 66389b4665a..1cc6fbce97a 100644 --- a/user/test/com/google/gwt/emultest/java8/util/stream/DoubleStreamTest.java +++ b/user/test/com/google/gwt/emultest/java8/util/stream/DoubleStreamTest.java @@ -17,6 +17,8 @@ package com.google.gwt.emultest.java8.util.stream; import com.google.gwt.emultest.java.util.EmulTestBase; +import com.google.gwt.junit.DoNotRunWith; +import com.google.gwt.junit.Platform; import java.util.ArrayList; import java.util.Arrays; @@ -113,6 +115,10 @@ public void testConcat() { assertEquals(Arrays.asList("first", "second"), closed); } + // Java8 has the same bug that GWT previously had here, so we're excluding Java8 from running + // this test. Presently, legacy dev mode is the only way to actually run this test using the + // JVM's own implementation, so marking this DoNotRunWith(Devel) is sufficient to avoid this. + @DoNotRunWith(Platform.Devel) public void testIterate() { assertEquals( new double[] {10d, 11d, 12d, 13d, 14d}, diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/IntStreamTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/IntStreamTest.java index a386fd24775..d9b4ebb8304 100644 --- a/user/test/com/google/gwt/emultest/java8/util/stream/IntStreamTest.java +++ b/user/test/com/google/gwt/emultest/java8/util/stream/IntStreamTest.java @@ -17,6 +17,8 @@ package com.google.gwt.emultest.java8.util.stream; import com.google.gwt.emultest.java.util.EmulTestBase; +import com.google.gwt.junit.DoNotRunWith; +import com.google.gwt.junit.Platform; import java.util.ArrayList; import java.util.Arrays; @@ -112,6 +114,10 @@ public void testConcat() { assertEquals(Arrays.asList("first", "second"), closed); } + // Java8 has the same bug that GWT previously had here, so we're excluding Java8 from running + // this test. Presently, legacy dev mode is the only way to actually run this test using the + // JVM's own implementation, so marking this DoNotRunWith(Devel) is sufficient to avoid this. + @DoNotRunWith(Platform.Devel) public void testIterate() { assertEquals( new int[] {10, 11, 12, 13, 14}, diff --git a/user/test/com/google/gwt/emultest/java8/util/stream/LongStreamTest.java b/user/test/com/google/gwt/emultest/java8/util/stream/LongStreamTest.java index e73cf0af015..6297afdf71d 100644 --- a/user/test/com/google/gwt/emultest/java8/util/stream/LongStreamTest.java +++ b/user/test/com/google/gwt/emultest/java8/util/stream/LongStreamTest.java @@ -17,6 +17,8 @@ package com.google.gwt.emultest.java8.util.stream; import com.google.gwt.emultest.java.util.EmulTestBase; +import com.google.gwt.junit.DoNotRunWith; +import com.google.gwt.junit.Platform; import java.util.ArrayList; import java.util.Arrays; @@ -111,6 +113,10 @@ public void testConcat() { assertEquals(Arrays.asList("first", "second"), closed); } + // Java8 has the same bug that GWT previously had here, so we're excluding Java8 from running + // this test. Presently, legacy dev mode is the only way to actually run this test using the + // JVM's own implementation, so marking this DoNotRunWith(Devel) is sufficient to avoid this. + @DoNotRunWith(Platform.Devel) public void testIterate() { assertEquals( new long[] {10L, 11L, 12L, 13L, 14L},