diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java index 7c9702ffc..f97c4230e 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/TCollectors.java @@ -19,7 +19,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; -import java.util.DoubleSummaryStatistics; import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; @@ -35,8 +34,6 @@ import java.util.function.Supplier; import java.util.function.ToDoubleFunction; import java.util.function.ToIntFunction; import java.util.function.ToLongFunction; -import java.util.stream.Collector; -import java.util.stream.Collectors; import org.teavm.classlib.java.util.TDoubleSummaryStatistics; import org.teavm.classlib.java.util.TIntSummaryStatistics; import org.teavm.classlib.java.util.TLongSummaryStatistics; @@ -105,7 +102,7 @@ public final class TCollectors { } }, (m1, m2) -> { - for (Map.Entry e : m2.entrySet()) { + for (var e : m2.entrySet()) { V newV = TObjects.requireNonNull(e.getValue()); V oldV = m1.putIfAbsent(e.getKey(), newV); if (oldV != null) { @@ -128,7 +125,7 @@ public final class TCollectors { return TCollector.of(mapFactory, (map, el) -> map.merge(keyMapper.apply(el), valueMapper.apply(el), mergeFunction), (m1, m2) -> { - for (Map.Entry e : m2.entrySet()) { + for (var e : m2.entrySet()) { m1.merge(e.getKey(), e.getValue(), mergeFunction); } return m1; @@ -156,7 +153,7 @@ public final class TCollectors { downstream.accumulator().accept(container, t); }; BinaryOperator> mapMerger = (m1, m2) -> { - for (Map.Entry e : m2.entrySet()) { + for (var e : m2.entrySet()) { m1.merge(e.getKey(), e.getValue(), downstream.combiner()); } return m1; @@ -169,7 +166,9 @@ public final class TCollectors { Function replacer = castFunction(downstream.finisher()); Function, M> finisher = toReplace -> { toReplace.replaceAll((k, v) -> replacer.apply(v)); - return (M) toReplace; + @SuppressWarnings("unchecked") + var result = (M) toReplace; + return result; }; return TCollector.of(castFactory(mapFactory), mapAppender, mapMerger, finisher); } @@ -189,7 +188,7 @@ public final class TCollectors { TCollector downstream, Function finisher) { - EnumSet newCharacteristics = EnumSet.copyOf(downstream.characteristics()); + var newCharacteristics = EnumSet.copyOf(downstream.characteristics()); newCharacteristics.remove(TCollector.Characteristics.IDENTITY_FINISH); return new TCollectorImpl<>(downstream.supplier(), @@ -299,21 +298,36 @@ public final class TCollectors { return TCollector.of( TIntSummaryStatistics::new, (r, t) -> r.accept(mapper.applyAsInt(t)), - (l, r) -> { l.combine(r); return l; }, TCollector.Characteristics.IDENTITY_FINISH); + (l, r) -> { + l.combine(r); + return l; + }, + TCollector.Characteristics.IDENTITY_FINISH + ); } public static TCollector summarizingLong(ToLongFunction mapper) { return TCollector.of( TLongSummaryStatistics::new, (r, t) -> r.accept(mapper.applyAsLong(t)), - (l, r) -> { l.combine(r); return l; }, TCollector.Characteristics.IDENTITY_FINISH); + (l, r) -> { + l.combine(r); + return l; + }, + TCollector.Characteristics.IDENTITY_FINISH + ); } public static TCollector summarizingDouble(ToDoubleFunction mapper) { return TCollector.of( TDoubleSummaryStatistics::new, (r, t) -> r.accept(mapper.applyAsDouble(t)), - (l, r) -> { l.combine(r); return l; }, TCollector.Characteristics.IDENTITY_FINISH); + (l, r) -> { + l.combine(r); + return l; + }, + TCollector.Characteristics.IDENTITY_FINISH + ); } private static TCollector teeingUnwrap(TCollector left, diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index efcddfc2d..c0675bde1 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -61,7 +61,9 @@ - + + + diff --git a/tests/src/test/java/org/teavm/classlib/java/util/stream/CollectorsTest.java b/tests/src/test/java/org/teavm/classlib/java/util/stream/CollectorsTest.java index 71a3f0126..6d5b9be2f 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/stream/CollectorsTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/stream/CollectorsTest.java @@ -22,9 +22,7 @@ import java.util.Comparator; import java.util.DoubleSummaryStatistics; import java.util.HashMap; import java.util.HashSet; -import java.util.IntSummaryStatistics; import java.util.List; -import java.util.LongSummaryStatistics; import java.util.Map; import java.util.Optional; import java.util.function.Function; @@ -34,8 +32,10 @@ import java.util.stream.Stream; import org.junit.Test; import org.junit.runner.RunWith; import org.teavm.junit.TeaVMTestRunner; +import org.teavm.junit.WholeClassCompilation; @RunWith(TeaVMTestRunner.class) +@WholeClassCompilation public class CollectorsTest { @Test public void joining() { @@ -120,15 +120,15 @@ public class CollectorsTest { @Test public void summaryInt() { - assertEquals(Integer.valueOf(6), Stream.of("a", "bb", "ccc").collect(Collectors.summingInt(String::length))); - assertEquals(Double.valueOf(2.0), Stream.of("a", "bb", "ccc").collect(Collectors.averagingInt(String::length))); - IntSummaryStatistics statistics = Stream.of("a", "bb", "ccc").collect(Collectors.summarizingInt(String::length)); + assertEquals(6L, (int) Stream.of("a", "bb", "ccc").collect(Collectors.summingInt(String::length))); + assertEquals(2.0, Stream.of("a", "bb", "ccc").collect(Collectors.averagingInt(String::length)), 0.001); + var statistics = Stream.of("a", "bb", "ccc").collect(Collectors.summarizingInt(String::length)); assertEquals(3L, statistics.getCount()); assertEquals(2.0, statistics.getAverage(), 0.0); assertEquals(1, statistics.getMin()); assertEquals(3, statistics.getMax()); assertEquals(6L, statistics.getSum()); - IntSummaryStatistics empty = Stream.of().collect(Collectors.summarizingInt(String::length)); + var empty = Stream.of().collect(Collectors.summarizingInt(String::length)); assertEquals(0L, empty.getCount()); assertEquals(0.0, empty.getAverage(), 0.0); assertEquals(Integer.MAX_VALUE, empty.getMin()); @@ -138,15 +138,15 @@ public class CollectorsTest { @Test public void summaryLong() { - assertEquals(Long.valueOf(6L), Stream.of("a", "bb", "ccc").collect(Collectors.summingLong(String::length))); - assertEquals(Double.valueOf(2.0), Stream.of("a", "bb", "ccc").collect(Collectors.averagingLong(String::length))); - LongSummaryStatistics statistics = Stream.of("a", "bb", "ccc").collect(Collectors.summarizingLong(String::length)); + assertEquals(6L, (long) Stream.of("a", "bb", "ccc").collect(Collectors.summingLong(String::length))); + assertEquals(2.0, Stream.of("a", "bb", "ccc").collect(Collectors.averagingLong(String::length)), 0.001); + var statistics = Stream.of("a", "bb", "ccc").collect(Collectors.summarizingLong(String::length)); assertEquals(3L, statistics.getCount()); assertEquals(2.0, statistics.getAverage(), 0.0); assertEquals(1L, statistics.getMin()); assertEquals(3L, statistics.getMax()); assertEquals(6L, statistics.getSum()); - LongSummaryStatistics empty = Stream.of().collect(Collectors.summarizingLong(String::length)); + var empty = Stream.of().collect(Collectors.summarizingLong(String::length)); assertEquals(0L, empty.getCount()); assertEquals(0.0, empty.getAverage(), 0.0); assertEquals(Long.MAX_VALUE, empty.getMin()); @@ -156,9 +156,9 @@ public class CollectorsTest { @Test public void summaryDouble() { - assertEquals(Double.valueOf(6.0), Stream.of("a", "bb", "ccc").collect(Collectors.summingDouble(String::length))); - assertEquals(Double.valueOf(2.0), Stream.of("a", "bb", "ccc").collect(Collectors.averagingDouble(String::length))); - DoubleSummaryStatistics statistics = Stream.of("a", "bb", "ccc").collect(Collectors.summarizingDouble(String::length)); + assertEquals(6.0, Stream.of("a", "bb", "ccc").collect(Collectors.summingDouble(String::length)), 0.001); + assertEquals(2.0, Stream.of("a", "bb", "ccc").collect(Collectors.averagingDouble(String::length)), 0.001); + var statistics = Stream.of("a", "bb", "ccc").collect(Collectors.summarizingDouble(String::length)); assertEquals(3L, statistics.getCount()); assertEquals(2.0, statistics.getAverage(), 0.0); assertEquals(1.0, statistics.getMin(), 0.0); @@ -174,7 +174,8 @@ public class CollectorsTest { @Test public void teeing() { - assertEquals(Double.valueOf(3.0d), Stream.of("a", "bb", "ccc").collect(Collectors.teeing(Collectors.summingInt(String::length), + assertEquals(Double.valueOf(3.0d), Stream.of("a", "bb", "ccc") + .collect(Collectors.teeing(Collectors.summingInt(String::length), Collectors.averagingInt(String::length), (sum, avg) -> sum / avg))); } }