Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Apr 24, 2023
1 parent 863e2db commit 78e60cf
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -400,7 +408,12 @@ boolean after(Stream<T> stream) {

static final class StreamMapToIntSum<T> {
@BeforeTemplate
int before(
long before(Stream<T> stream, ToIntFunction<T> mapper) {
return stream.collect(summingInt(mapper));
}

@BeforeTemplate
int before2(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) Function<? super T, Integer> mapper) {
return stream.map(mapper).reduce(0, Integer::sum);
Expand All @@ -412,23 +425,14 @@ int after(Stream<T> stream, ToIntFunction<T> mapper) {
}
}

static final class StreamCollectSummingInt<T> {
static final class StreamMapToDoubleSum<T> {
@BeforeTemplate
long before(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) ToIntFunction<T> mapper) {
return stream.collect(summingInt(mapper));
}

@AfterTemplate
long after(Stream<T> stream, ToIntFunction<T> mapper) {
return stream.mapToInt(mapper).sum();
double before(Stream<T> stream, ToDoubleFunction<T> mapper) {
return stream.collect(summingDouble(mapper));
}
}

static final class StreamMapToDoubleSum<T> {
@BeforeTemplate
double before(
double before2(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) Function<? super T, Double> mapper) {
return stream.map(mapper).reduce(0.0, Double::sum);
Expand All @@ -440,23 +444,14 @@ static final class StreamMapToDoubleSum<T> {
}
}

static final class StreamCollectSummingDouble<T> {
static final class StreamMapToLongSum<T> {
@BeforeTemplate
double before(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) ToDoubleFunction<T> mapper) {
return stream.collect(summingDouble(mapper));
}

@AfterTemplate
double after(Stream<T> stream, ToDoubleFunction<T> mapper) {
return stream.mapToDouble(mapper).sum();
long before(Stream<T> stream, ToLongFunction<T> mapper) {
return stream.collect(summingLong(mapper));
}
}

static final class StreamMapToLongSum<T> {
@BeforeTemplate
long before(
long before2(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) Function<? super T, Long> mapper) {
return stream.map(mapper).reduce(0L, Long::sum);
Expand All @@ -468,17 +463,39 @@ long after(Stream<T> stream, ToLongFunction<T> mapper) {
}
}

static final class StreamCollectSummingLong<T> {
static final class StreamMapToIntSummaryStatistics<T> {
@BeforeTemplate
long before(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) ToLongFunction<T> mapper) {
return stream.collect(summingLong(mapper));
IntSummaryStatistics before(Stream<T> stream, ToIntFunction<T> mapper) {
return stream.collect(summarizingInt(mapper));
}

@AfterTemplate
long after(Stream<T> stream, ToLongFunction<T> mapper) {
return stream.mapToLong(mapper).sum();
IntSummaryStatistics after(Stream<T> stream, ToIntFunction<T> mapper) {
return stream.mapToInt(mapper).summaryStatistics();
}
}

static final class StreamMapToDoubleSummaryStatistics<T> {
@BeforeTemplate
DoubleSummaryStatistics before(Stream<T> stream, ToDoubleFunction<T> mapper) {
return stream.collect(summarizingDouble(mapper));
}

@AfterTemplate
DoubleSummaryStatistics after(Stream<T> stream, ToDoubleFunction<T> mapper) {
return stream.mapToDouble(mapper).summaryStatistics();
}
}

static final class StreamMapToLongSummaryStatistics<T> {
@BeforeTemplate
LongSummaryStatistics before(Stream<T> stream, ToLongFunction<T> mapper) {
return stream.collect(summarizingLong(mapper));
}

@AfterTemplate
LongSummaryStatistics after(Stream<T> stream, ToLongFunction<T> mapper) {
return stream.mapToLong(mapper).summaryStatistics();
}
}

Expand All @@ -495,6 +512,18 @@ long after(Stream<T> stream) {
}

static final class StreamReduce<T> {
@BeforeTemplate
Optional<T> before(Stream<T> stream, BinaryOperator<T> accumulator) {
return stream.collect(reducing(accumulator));
}

@AfterTemplate
Optional<T> after(Stream<T> stream, BinaryOperator<T> accumulator) {
return stream.reduce(accumulator);
}
}

static final class StreamReduceWithIdentity<T> {
@BeforeTemplate
T before(Stream<T> stream, T identity, BinaryOperator<T> accumulator) {
return stream.collect(reducing(identity, accumulator));
Expand All @@ -506,33 +535,53 @@ T after(Stream<T> stream, T identity, BinaryOperator<T> accumulator) {
}
}

static final class StreamReduceOptional<T> {
static final class StreamFilterCollect<T, R> {
@BeforeTemplate
Optional<T> before(Stream<T> stream, BinaryOperator<T> accumulator) {
return stream.collect(reducing(accumulator));
R before(
Stream<T> stream, Predicate<? super T> predicate, Collector<? super T, ?, R> collector) {
return stream.collect(filtering(predicate, collector));
}

@AfterTemplate
Optional<T> after(Stream<T> stream, BinaryOperator<T> accumulator) {
return stream.reduce(accumulator);
R after(
Stream<T> stream, Predicate<? super T> predicate, Collector<? super T, ?, R> collector) {
return stream.filter(predicate).collect(collector);
}
}

static final class StreamMap<T, U, A, R> {
static final class StreamMapCollect<T, U, R> {
@BeforeTemplate
R before(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) Function<? super T, ? extends U> mapper,
Collector<? super U, A, R> collector) {
Function<? super T, ? extends U> mapper,
Collector<? super U, ?, R> collector) {
return stream.collect(mapping(mapper, collector));
}

@AfterTemplate
R after(
Stream<T> stream,
Function<? super T, ? extends U> mapper,
Collector<? super U, A, R> collector) {
Collector<? super U, ?, R> collector) {
return stream.map(mapper).collect(collector);
}
}

static final class StreamFlatMapCollect<T, U, R> {
@BeforeTemplate
R before(
Stream<T> stream,
Function<? super T, ? extends Stream<? extends U>> mapper,
Collector<? super U, ?, R> collector) {
return stream.collect(flatMapping(mapper, collector));
}

@AfterTemplate
R after(
Stream<T> stream,
Function<? super T, ? extends Stream<? extends U>> mapper,
Collector<? super U, ?, R> collector) {
return stream.flatMap(mapper).collect(collector);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -92,7 +124,7 @@ ImmutableSet<Optional<String>> 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<Optional<String>> testStreamMinNaturalOrder() {
Expand All @@ -104,7 +136,7 @@ ImmutableSet<Optional<String>> 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<Optional<String>> testStreamMaxNaturalOrder() {
Expand Down Expand Up @@ -146,53 +178,63 @@ boolean testStreamAllMatch2() {
ImmutableSet<Integer> testStreamMapToIntSum() {
Function<String, Integer> 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<Double> testStreamMapToDoubleSum() {
Function<String, Double> 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<Long> testStreamMapToLongSum() {
Function<String, Long> 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<Integer> 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<String> testStreamFilterCollect() {
return Stream.of("1").collect(filtering(String::isEmpty, toImmutableSet()));
}

Optional<Integer> testStreamReduceOptional() {
return Stream.of(1).collect(java.util.stream.Collectors.reducing(Integer::sum));
ImmutableSet<Integer> testStreamMapCollect() {
return Stream.of("1").collect(mapping(Integer::parseInt, toImmutableSet()));
}

ImmutableSet<Integer> testStreamMap() {
return Stream.of("1")
.collect(java.util.stream.Collectors.mapping(Integer::parseInt, toImmutableSet()));
ImmutableSet<Integer> testStreamFlatMapCollect() {
return Stream.of(1).collect(flatMapping(n -> Stream.of(n, n), toImmutableSet()));
}
}
Loading

0 comments on commit 78e60cf

Please sign in to comment.