Introduce StreamsConcat Refaster rule (#619)

This commit is contained in:
Luke Prananta
2023-05-14 16:48:35 +02:00
committed by GitHub
parent 7cf569cca3
commit 137ec4c573
3 changed files with 29 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package tech.picnic.errorprone.refasterrules;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.filtering;
@@ -27,6 +28,7 @@ import com.google.errorprone.refaster.annotation.Matches;
import com.google.errorprone.refaster.annotation.MayOptionallyUse;
import com.google.errorprone.refaster.annotation.NotMatches;
import com.google.errorprone.refaster.annotation.Placeholder;
import com.google.errorprone.refaster.annotation.Repeated;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Arrays;
import java.util.Collection;
@@ -623,4 +625,16 @@ final class StreamRules {
return stream.flatMap(mapper).collect(collector);
}
}
static final class StreamsConcat<T> {
@BeforeTemplate
Stream<T> before(@Repeated Stream<T> stream) {
return Stream.of(Refaster.asVarargs(stream)).flatMap(Refaster.anyOf(identity(), s -> s));
}
@AfterTemplate
Stream<T> after(@Repeated Stream<T> stream) {
return Streams.concat(Refaster.asVarargs(stream));
}
}
}

View File

@@ -3,6 +3,7 @@ package tech.picnic.errorprone.refasterrules;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Comparator.comparingInt;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.filtering;
@@ -40,6 +41,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
counting(),
filtering(null, null),
flatMapping(null, null),
identity(),
mapping(null, null),
maxBy(null),
minBy(null),
@@ -250,4 +252,10 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
ImmutableSet<Integer> testStreamFlatMapCollect() {
return Stream.of(1).collect(flatMapping(n -> Stream.of(n, n), toImmutableSet()));
}
ImmutableSet<Stream<Integer>> testStreamsConcat() {
return ImmutableSet.of(
Stream.of(Stream.of(1), Stream.of(2)).flatMap(identity()),
Stream.of(Stream.of(3), Stream.of(4)).flatMap(v -> v));
}
}

View File

@@ -4,6 +4,7 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Comparator.comparingInt;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.filtering;
@@ -42,6 +43,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
counting(),
filtering(null, null),
flatMapping(null, null),
identity(),
mapping(null, null),
maxBy(null),
minBy(null),
@@ -251,4 +253,9 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
ImmutableSet<Integer> testStreamFlatMapCollect() {
return Stream.of(1).flatMap(n -> Stream.of(n, n)).collect(toImmutableSet());
}
ImmutableSet<Stream<Integer>> testStreamsConcat() {
return ImmutableSet.of(
Streams.concat(Stream.of(1), Stream.of(2)), Streams.concat(Stream.of(3), Stream.of(4)));
}
}