From 477d8b2d69369b3f050f8b6ecac93a62ace6b456 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 4 Dec 2019 15:50:10 +0300 Subject: [PATCH] Fix Stream.collect. Add some of the missing functional interfaces --- .../util/function/TObjDoubleConsumer.java | 21 ++++++++++ .../java/util/function/TObjIntConsumer.java | 21 ++++++++++ .../java/util/function/TObjLongConsumer.java | 21 ++++++++++ .../util/function/TToDoubleBiFunction.java | 21 ++++++++++ .../java/util/function/TToIntBiFunction.java | 21 ++++++++++ .../java/util/function/TToLongBiFunction.java | 21 ++++++++++ .../util/stream/impl/TSimpleStreamImpl.java | 13 +++++-- .../classlib/java/util/stream/Helper.java | 38 +++++++++++++++++++ 8 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/function/TObjDoubleConsumer.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/function/TObjIntConsumer.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/function/TObjLongConsumer.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/function/TToDoubleBiFunction.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/function/TToIntBiFunction.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/util/function/TToLongBiFunction.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjDoubleConsumer.java b/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjDoubleConsumer.java new file mode 100644 index 000000000..3769a7ed3 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjDoubleConsumer.java @@ -0,0 +1,21 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * 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 org.teavm.classlib.java.util.function; + +@FunctionalInterface +public interface TObjDoubleConsumer { + void accept(T t, double u); +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjIntConsumer.java b/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjIntConsumer.java new file mode 100644 index 000000000..96e900d9d --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjIntConsumer.java @@ -0,0 +1,21 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * 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 org.teavm.classlib.java.util.function; + +@FunctionalInterface +public interface TObjIntConsumer { + void accept(T t, int u); +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjLongConsumer.java b/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjLongConsumer.java new file mode 100644 index 000000000..240a80dd4 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/function/TObjLongConsumer.java @@ -0,0 +1,21 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * 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 org.teavm.classlib.java.util.function; + +@FunctionalInterface +public interface TObjLongConsumer { + void accept(T t, long u); +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/function/TToDoubleBiFunction.java b/classlib/src/main/java/org/teavm/classlib/java/util/function/TToDoubleBiFunction.java new file mode 100644 index 000000000..8a113e309 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/function/TToDoubleBiFunction.java @@ -0,0 +1,21 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * 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 org.teavm.classlib.java.util.function; + +@FunctionalInterface +public interface TToDoubleBiFunction { + double applyAsDouble(T t, U u); +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/function/TToIntBiFunction.java b/classlib/src/main/java/org/teavm/classlib/java/util/function/TToIntBiFunction.java new file mode 100644 index 000000000..3bc22e8ee --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/function/TToIntBiFunction.java @@ -0,0 +1,21 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * 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 org.teavm.classlib.java.util.function; + +@FunctionalInterface +public interface TToIntBiFunction { + int applyAsInt(T t, U u); +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/function/TToLongBiFunction.java b/classlib/src/main/java/org/teavm/classlib/java/util/function/TToLongBiFunction.java new file mode 100644 index 000000000..1c505f864 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/util/function/TToLongBiFunction.java @@ -0,0 +1,21 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * 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 org.teavm.classlib.java.util.function; + +@FunctionalInterface +public interface TToLongBiFunction { + long applyAsLong(T t, U u); +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TSimpleStreamImpl.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TSimpleStreamImpl.java index 00068b078..3adc07282 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TSimpleStreamImpl.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TSimpleStreamImpl.java @@ -212,10 +212,15 @@ public abstract class TSimpleStreamImpl implements TStream { public R collect(TCollector collector) { A collection = collector.supplier().get(); BiConsumer accumulator = collector.accumulator(); - next(e -> { - accumulator.accept(collection, e); - return true; - }); + while (true) { + boolean hasMore = next(e -> { + accumulator.accept(collection, e); + return true; + }); + if (!hasMore) { + break; + } + } return collector.finisher().apply(collection); } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/stream/Helper.java b/tests/src/test/java/org/teavm/classlib/java/util/stream/Helper.java index c794735d2..204e4a1a3 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/stream/Helper.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/stream/Helper.java @@ -18,13 +18,21 @@ package org.teavm.classlib.java.util.stream; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import com.carrotsearch.hppc.DoubleArrayList; +import com.carrotsearch.hppc.IntArrayList; +import com.carrotsearch.hppc.LongArrayList; +import com.carrotsearch.hppc.cursors.DoubleCursor; +import com.carrotsearch.hppc.cursors.IntCursor; +import com.carrotsearch.hppc.cursors.LongCursor; import java.util.Iterator; +import java.util.List; import java.util.PrimitiveIterator; import java.util.function.Consumer; import java.util.function.DoubleConsumer; import java.util.function.IntConsumer; import java.util.function.LongConsumer; import java.util.function.Supplier; +import java.util.stream.Collectors; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; @@ -52,6 +60,13 @@ final class Helper { } assertEquals(expectedText, sb.toString()); + sb.setLength(0); + List list = streamSupplier.get().collect(Collectors.toList()); + for (Integer e : list) { + sb.append(e).append(';'); + } + assertEquals(expectedText, sb.toString()); + assertEquals(expected.length, streamSupplier.get().count()); if (expected.length > 0) { @@ -91,6 +106,13 @@ final class Helper { } assertEquals(expectedText, sb.toString()); + sb.setLength(0); + IntArrayList list = streamSupplier.get().collect(IntArrayList::new, IntArrayList::add, IntArrayList::addAll); + for (IntCursor cursor : list) { + sb.append(cursor.value).append(';'); + } + assertEquals(expectedText, sb.toString()); + assertEquals(expected.length, streamSupplier.get().count()); if (expected.length > 0) { @@ -130,6 +152,14 @@ final class Helper { } assertEquals(expectedText, sb.toString()); + sb.setLength(0); + LongArrayList list = streamSupplier.get().collect(LongArrayList::new, LongArrayList::add, + LongArrayList::addAll); + for (LongCursor cursor : list) { + sb.append(cursor.value).append(';'); + } + assertEquals(expectedText, sb.toString()); + assertEquals(expected.length, streamSupplier.get().count()); if (expected.length > 0) { @@ -169,6 +199,14 @@ final class Helper { } assertEquals(expectedText, sb.toString()); + sb.setLength(0); + DoubleArrayList list = streamSupplier.get().collect(DoubleArrayList::new, DoubleArrayList::add, + DoubleArrayList::addAll); + for (DoubleCursor cursor : list) { + sb.append(cursor.value).append(';'); + } + assertEquals(expectedText, sb.toString()); + assertEquals(expected.length, streamSupplier.get().count()); if (expected.length > 0) {