From 78e60cf3c021c89cd5ebb88013e9bbb3bec250ea Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 24 Apr 2023 06:39:33 +0200 Subject: [PATCH] Suggestions --- .../errorprone/refasterrules/StreamRules.java | 135 ++++++++++++------ .../refasterrules/StreamRulesTestInput.java | 102 +++++++++---- .../refasterrules/StreamRulesTestOutput.java | 91 ++++++++---- 3 files changed, 231 insertions(+), 97 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java index ce76c6e5610..abd0feba064 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java @@ -5,11 +5,16 @@ import static java.util.Comparator.reverseOrder; import static java.util.function.Predicate.not; import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.filtering; +import static java.util.stream.Collectors.flatMapping; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.mapping; import static java.util.stream.Collectors.maxBy; import static java.util.stream.Collectors.minBy; import static java.util.stream.Collectors.reducing; +import static java.util.stream.Collectors.summarizingDouble; +import static java.util.stream.Collectors.summarizingInt; +import static java.util.stream.Collectors.summarizingLong; import static java.util.stream.Collectors.summingDouble; import static java.util.stream.Collectors.summingInt; import static java.util.stream.Collectors.summingLong; @@ -24,6 +29,9 @@ import com.google.errorprone.refaster.annotation.UseImportPolicy; import java.util.Arrays; import java.util.Comparator; +import java.util.DoubleSummaryStatistics; +import java.util.IntSummaryStatistics; +import java.util.LongSummaryStatistics; import java.util.Objects; import java.util.Optional; import java.util.function.BinaryOperator; @@ -400,7 +408,12 @@ boolean after(Stream stream) { static final class StreamMapToIntSum { @BeforeTemplate - int before( + long before(Stream stream, ToIntFunction mapper) { + return stream.collect(summingInt(mapper)); + } + + @BeforeTemplate + int before2( Stream stream, @Matches(IsLambdaExpressionOrMethodReference.class) Function mapper) { return stream.map(mapper).reduce(0, Integer::sum); @@ -412,23 +425,14 @@ int after(Stream stream, ToIntFunction mapper) { } } - static final class StreamCollectSummingInt { + static final class StreamMapToDoubleSum { @BeforeTemplate - long before( - Stream stream, - @Matches(IsLambdaExpressionOrMethodReference.class) ToIntFunction mapper) { - return stream.collect(summingInt(mapper)); - } - - @AfterTemplate - long after(Stream stream, ToIntFunction mapper) { - return stream.mapToInt(mapper).sum(); + double before(Stream stream, ToDoubleFunction mapper) { + return stream.collect(summingDouble(mapper)); } - } - static final class StreamMapToDoubleSum { @BeforeTemplate - double before( + double before2( Stream stream, @Matches(IsLambdaExpressionOrMethodReference.class) Function mapper) { return stream.map(mapper).reduce(0.0, Double::sum); @@ -440,23 +444,14 @@ static final class StreamMapToDoubleSum { } } - static final class StreamCollectSummingDouble { + static final class StreamMapToLongSum { @BeforeTemplate - double before( - Stream stream, - @Matches(IsLambdaExpressionOrMethodReference.class) ToDoubleFunction mapper) { - return stream.collect(summingDouble(mapper)); - } - - @AfterTemplate - double after(Stream stream, ToDoubleFunction mapper) { - return stream.mapToDouble(mapper).sum(); + long before(Stream stream, ToLongFunction mapper) { + return stream.collect(summingLong(mapper)); } - } - static final class StreamMapToLongSum { @BeforeTemplate - long before( + long before2( Stream stream, @Matches(IsLambdaExpressionOrMethodReference.class) Function mapper) { return stream.map(mapper).reduce(0L, Long::sum); @@ -468,17 +463,39 @@ long after(Stream stream, ToLongFunction mapper) { } } - static final class StreamCollectSummingLong { + static final class StreamMapToIntSummaryStatistics { @BeforeTemplate - long before( - Stream stream, - @Matches(IsLambdaExpressionOrMethodReference.class) ToLongFunction mapper) { - return stream.collect(summingLong(mapper)); + IntSummaryStatistics before(Stream stream, ToIntFunction mapper) { + return stream.collect(summarizingInt(mapper)); } @AfterTemplate - long after(Stream stream, ToLongFunction mapper) { - return stream.mapToLong(mapper).sum(); + IntSummaryStatistics after(Stream stream, ToIntFunction mapper) { + return stream.mapToInt(mapper).summaryStatistics(); + } + } + + static final class StreamMapToDoubleSummaryStatistics { + @BeforeTemplate + DoubleSummaryStatistics before(Stream stream, ToDoubleFunction mapper) { + return stream.collect(summarizingDouble(mapper)); + } + + @AfterTemplate + DoubleSummaryStatistics after(Stream stream, ToDoubleFunction mapper) { + return stream.mapToDouble(mapper).summaryStatistics(); + } + } + + static final class StreamMapToLongSummaryStatistics { + @BeforeTemplate + LongSummaryStatistics before(Stream stream, ToLongFunction mapper) { + return stream.collect(summarizingLong(mapper)); + } + + @AfterTemplate + LongSummaryStatistics after(Stream stream, ToLongFunction mapper) { + return stream.mapToLong(mapper).summaryStatistics(); } } @@ -495,6 +512,18 @@ long after(Stream stream) { } static final class StreamReduce { + @BeforeTemplate + Optional before(Stream stream, BinaryOperator accumulator) { + return stream.collect(reducing(accumulator)); + } + + @AfterTemplate + Optional after(Stream stream, BinaryOperator accumulator) { + return stream.reduce(accumulator); + } + } + + static final class StreamReduceWithIdentity { @BeforeTemplate T before(Stream stream, T identity, BinaryOperator accumulator) { return stream.collect(reducing(identity, accumulator)); @@ -506,24 +535,26 @@ T after(Stream stream, T identity, BinaryOperator accumulator) { } } - static final class StreamReduceOptional { + static final class StreamFilterCollect { @BeforeTemplate - Optional before(Stream stream, BinaryOperator accumulator) { - return stream.collect(reducing(accumulator)); + R before( + Stream stream, Predicate predicate, Collector collector) { + return stream.collect(filtering(predicate, collector)); } @AfterTemplate - Optional after(Stream stream, BinaryOperator accumulator) { - return stream.reduce(accumulator); + R after( + Stream stream, Predicate predicate, Collector collector) { + return stream.filter(predicate).collect(collector); } } - static final class StreamMap { + static final class StreamMapCollect { @BeforeTemplate R before( Stream stream, - @Matches(IsLambdaExpressionOrMethodReference.class) Function mapper, - Collector collector) { + Function mapper, + Collector collector) { return stream.collect(mapping(mapper, collector)); } @@ -531,8 +562,26 @@ R before( R after( Stream stream, Function mapper, - Collector collector) { + Collector collector) { return stream.map(mapper).collect(collector); } } + + static final class StreamFlatMapCollect { + @BeforeTemplate + R before( + Stream stream, + Function> mapper, + Collector collector) { + return stream.collect(flatMapping(mapper, collector)); + } + + @AfterTemplate + R after( + Stream stream, + Function> mapper, + Collector collector) { + return stream.flatMap(mapper).collect(collector); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java index 2c4fdeea989..dc8fa361bf9 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java @@ -4,10 +4,26 @@ import static java.util.Comparator.comparingInt; import static java.util.Comparator.reverseOrder; import static java.util.function.Predicate.not; +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.filtering; +import static java.util.stream.Collectors.flatMapping; import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.mapping; +import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.minBy; +import static java.util.stream.Collectors.reducing; +import static java.util.stream.Collectors.summarizingDouble; +import static java.util.stream.Collectors.summarizingInt; +import static java.util.stream.Collectors.summarizingLong; +import static java.util.stream.Collectors.summingDouble; +import static java.util.stream.Collectors.summingInt; +import static java.util.stream.Collectors.summingLong; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; +import java.util.DoubleSummaryStatistics; +import java.util.IntSummaryStatistics; +import java.util.LongSummaryStatistics; import java.util.Objects; import java.util.Optional; import java.util.function.Function; @@ -18,7 +34,23 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Objects.class, Streams.class, not(null)); + return ImmutableSet.of( + Objects.class, + Streams.class, + counting(), + filtering(null, null), + flatMapping(null, null), + mapping(null, null), + maxBy(null), + minBy(null), + not(null), + reducing(null), + summarizingDouble(null), + summarizingInt(null), + summarizingLong(null), + summingDouble(null), + summingInt(null), + summingLong(null)); } String testJoining() { @@ -92,7 +124,7 @@ ImmutableSet> testStreamMin() { return ImmutableSet.of( Stream.of("foo").max(comparingInt(String::length).reversed()), Stream.of("bar").sorted(comparingInt(String::length)).findFirst(), - Stream.of("baz").collect(java.util.stream.Collectors.minBy(comparingInt(String::length)))); + Stream.of("baz").collect(minBy(comparingInt(String::length)))); } ImmutableSet> testStreamMinNaturalOrder() { @@ -104,7 +136,7 @@ ImmutableSet> testStreamMax() { return ImmutableSet.of( Stream.of("foo").min(comparingInt(String::length).reversed()), Streams.findLast(Stream.of("bar").sorted(comparingInt(String::length))), - Stream.of("baz").collect(java.util.stream.Collectors.maxBy(comparingInt(String::length)))); + Stream.of("baz").collect(maxBy(comparingInt(String::length)))); } ImmutableSet> testStreamMaxNaturalOrder() { @@ -146,53 +178,63 @@ boolean testStreamAllMatch2() { ImmutableSet testStreamMapToIntSum() { Function parseIntFunction = Integer::parseInt; return ImmutableSet.of( - Stream.of(1).map(i -> i * 2).reduce(0, Integer::sum), - Stream.of("2").map(Integer::parseInt).reduce(0, Integer::sum), - Stream.of("3").map(parseIntFunction).reduce(0, Integer::sum)); - } - - Integer testStreamCollectSummingInt() { - return Stream.of("1").collect(java.util.stream.Collectors.summingInt(Integer::parseInt)); + Stream.of("1").collect(summingInt(Integer::parseInt)), + Stream.of(2).map(i -> i * 2).reduce(0, Integer::sum), + Stream.of("3").map(Integer::parseInt).reduce(0, Integer::sum), + Stream.of("4").map(parseIntFunction).reduce(0, Integer::sum)); } ImmutableSet testStreamMapToDoubleSum() { Function parseDoubleFunction = Double::parseDouble; return ImmutableSet.of( - Stream.of(1).map(i -> i * 2.0).reduce(0.0, Double::sum), - Stream.of("2").map(Double::parseDouble).reduce(0.0, Double::sum), - Stream.of("3").map(parseDoubleFunction).reduce(0.0, Double::sum)); - } - - Double testStreamCollectSummingDouble() { - return Stream.of("1").collect(java.util.stream.Collectors.summingDouble(Double::parseDouble)); + Stream.of("1").collect(summingDouble(Double::parseDouble)), + Stream.of(2).map(i -> i * 2.0).reduce(0.0, Double::sum), + Stream.of("3").map(Double::parseDouble).reduce(0.0, Double::sum), + Stream.of("4").map(parseDoubleFunction).reduce(0.0, Double::sum)); } ImmutableSet testStreamMapToLongSum() { Function parseLongFunction = Long::parseLong; return ImmutableSet.of( - Stream.of(1).map(i -> i * 2L).reduce(0L, Long::sum), - Stream.of("2").map(Long::parseLong).reduce(0L, Long::sum), - Stream.of("3").map(parseLongFunction).reduce(0L, Long::sum)); + Stream.of("1").collect(summingLong(Long::parseLong)), + Stream.of(2).map(i -> i * 2L).reduce(0L, Long::sum), + Stream.of("3").map(Long::parseLong).reduce(0L, Long::sum), + Stream.of("4").map(parseLongFunction).reduce(0L, Long::sum)); + } + + IntSummaryStatistics testStreamMapToIntSummaryStatistics() { + return Stream.of("1").collect(summarizingInt(Integer::parseInt)); } - Long testStreamCollectSummingLong() { - return Stream.of("1").collect(java.util.stream.Collectors.summingLong(Long::parseLong)); + DoubleSummaryStatistics testStreamMapToDoubleSummaryStatistics() { + return Stream.of("1").collect(summarizingDouble(Double::parseDouble)); + } + + LongSummaryStatistics testStreamMapToLongSummaryStatistics() { + return Stream.of("1").collect(summarizingLong(Long::parseLong)); } Long testStreamCount() { - return Stream.of(1).collect(java.util.stream.Collectors.counting()); + return Stream.of(1).collect(counting()); + } + + Optional testStreamReduce() { + return Stream.of(1).collect(reducing(Integer::sum)); + } + + Integer testStreamReduceWithIdentity() { + return Stream.of(1).collect(reducing(0, Integer::sum)); } - Integer testStreamReduce() { - return Stream.of(1).collect(java.util.stream.Collectors.reducing(0, Integer::sum)); + ImmutableSet testStreamFilterCollect() { + return Stream.of("1").collect(filtering(String::isEmpty, toImmutableSet())); } - Optional testStreamReduceOptional() { - return Stream.of(1).collect(java.util.stream.Collectors.reducing(Integer::sum)); + ImmutableSet testStreamMapCollect() { + return Stream.of("1").collect(mapping(Integer::parseInt, toImmutableSet())); } - ImmutableSet testStreamMap() { - return Stream.of("1") - .collect(java.util.stream.Collectors.mapping(Integer::parseInt, toImmutableSet())); + ImmutableSet testStreamFlatMapCollect() { + return Stream.of(1).collect(flatMapping(n -> Stream.of(n, n), toImmutableSet())); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java index 2a21bd6f3fc..586097a4038 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java @@ -5,11 +5,27 @@ import static java.util.Comparator.naturalOrder; import static java.util.Comparator.reverseOrder; import static java.util.function.Predicate.not; +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.filtering; +import static java.util.stream.Collectors.flatMapping; import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.mapping; +import static java.util.stream.Collectors.maxBy; +import static java.util.stream.Collectors.minBy; +import static java.util.stream.Collectors.reducing; +import static java.util.stream.Collectors.summarizingDouble; +import static java.util.stream.Collectors.summarizingInt; +import static java.util.stream.Collectors.summarizingLong; +import static java.util.stream.Collectors.summingDouble; +import static java.util.stream.Collectors.summingInt; +import static java.util.stream.Collectors.summingLong; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; import java.util.Arrays; +import java.util.DoubleSummaryStatistics; +import java.util.IntSummaryStatistics; +import java.util.LongSummaryStatistics; import java.util.Objects; import java.util.Optional; import java.util.function.Function; @@ -20,7 +36,23 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Objects.class, Streams.class, not(null)); + return ImmutableSet.of( + Objects.class, + Streams.class, + counting(), + filtering(null, null), + flatMapping(null, null), + mapping(null, null), + maxBy(null), + minBy(null), + not(null), + reducing(null), + summarizingDouble(null), + summarizingInt(null), + summarizingLong(null), + summingDouble(null), + summingInt(null), + summingLong(null)); } String testJoining() { @@ -145,52 +177,63 @@ boolean testStreamAllMatch2() { ImmutableSet testStreamMapToIntSum() { Function parseIntFunction = Integer::parseInt; return ImmutableSet.of( - Stream.of(1).mapToInt(i -> i * 2).sum(), - Stream.of("2").mapToInt(Integer::parseInt).sum(), - Stream.of("3").map(parseIntFunction).reduce(0, Integer::sum)); - } - - Integer testStreamCollectSummingInt() { - return Stream.of("1").mapToInt(Integer::parseInt).sum(); + Stream.of("1").mapToInt(Integer::parseInt).sum(), + Stream.of(2).mapToInt(i -> i * 2).sum(), + Stream.of("3").mapToInt(Integer::parseInt).sum(), + Stream.of("4").map(parseIntFunction).reduce(0, Integer::sum)); } ImmutableSet testStreamMapToDoubleSum() { Function parseDoubleFunction = Double::parseDouble; return ImmutableSet.of( - Stream.of(1).mapToDouble(i -> i * 2.0).sum(), - Stream.of("2").mapToDouble(Double::parseDouble).sum(), - Stream.of("3").map(parseDoubleFunction).reduce(0.0, Double::sum)); - } - - Double testStreamCollectSummingDouble() { - return Stream.of("1").mapToDouble(Double::parseDouble).sum(); + Stream.of("1").mapToDouble(Double::parseDouble).sum(), + Stream.of(2).mapToDouble(i -> i * 2.0).sum(), + Stream.of("3").mapToDouble(Double::parseDouble).sum(), + Stream.of("4").map(parseDoubleFunction).reduce(0.0, Double::sum)); } ImmutableSet testStreamMapToLongSum() { Function parseLongFunction = Long::parseLong; return ImmutableSet.of( - Stream.of(1).mapToLong(i -> i * 2L).sum(), - Stream.of("2").mapToLong(Long::parseLong).sum(), - Stream.of("3").map(parseLongFunction).reduce(0L, Long::sum)); + Stream.of("1").mapToLong(Long::parseLong).sum(), + Stream.of(2).mapToLong(i -> i * 2L).sum(), + Stream.of("3").mapToLong(Long::parseLong).sum(), + Stream.of("4").map(parseLongFunction).reduce(0L, Long::sum)); + } + + IntSummaryStatistics testStreamMapToIntSummaryStatistics() { + return Stream.of("1").mapToInt(Integer::parseInt).summaryStatistics(); } - Long testStreamCollectSummingLong() { - return Stream.of("1").mapToLong(Long::parseLong).sum(); + DoubleSummaryStatistics testStreamMapToDoubleSummaryStatistics() { + return Stream.of("1").mapToDouble(Double::parseDouble).summaryStatistics(); + } + + LongSummaryStatistics testStreamMapToLongSummaryStatistics() { + return Stream.of("1").mapToLong(Long::parseLong).summaryStatistics(); } Long testStreamCount() { return Stream.of(1).count(); } - Integer testStreamReduce() { + Optional testStreamReduce() { + return Stream.of(1).reduce(Integer::sum); + } + + Integer testStreamReduceWithIdentity() { return Stream.of(1).reduce(0, Integer::sum); } - Optional testStreamReduceOptional() { - return Stream.of(1).reduce(Integer::sum); + ImmutableSet testStreamFilterCollect() { + return Stream.of("1").filter(String::isEmpty).collect(toImmutableSet()); } - ImmutableSet testStreamMap() { + ImmutableSet testStreamMapCollect() { return Stream.of("1").map(Integer::parseInt).collect(toImmutableSet()); } + + ImmutableSet testStreamFlatMapCollect() { + return Stream.of(1).flatMap(n -> Stream.of(n, n)).collect(toImmutableSet()); + } }