diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java index ef56d3a32b..68391d463e 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java @@ -2,6 +2,7 @@ import static com.google.common.collect.MoreCollectors.toOptional; import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static java.util.function.Function.identity; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.MoreCollectors; @@ -177,6 +178,26 @@ Flux after(Flux flux, Function } } + /** Prefer {@link Flux#concatMap(Function, int)} over more contrived alternatives. */ + static final class FluxConcatMapWithPrefetch { + @BeforeTemplate + Flux before( + Flux flux, + Function> function, + int prefetch) { + return Refaster.anyOf( + flux.flatMap(function, 1, prefetch), flux.flatMapSequential(function, 1, prefetch)); + } + + @AfterTemplate + Flux after( + Flux flux, + Function> function, + int prefetch) { + return flux.concatMap(function, prefetch); + } + } + /** * Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#flatMapIterable(Function)}, as * the former has equivalent semantics but a clearer name. @@ -193,6 +214,24 @@ Flux after(Flux flux, Function> } } + /** + * Prefer {@link Flux#concatMapIterable(Function, int)} over {@link Flux#flatMapIterable(Function, + * int)}, as the former has equivalent semantics but a clearer name. + */ + static final class FluxConcatMapIterableWithPrefetch { + @BeforeTemplate + Flux before( + Flux flux, Function> function, int prefetch) { + return flux.flatMapIterable(function, prefetch); + } + + @AfterTemplate + Flux after( + Flux flux, Function> function, int prefetch) { + return flux.concatMapIterable(function, prefetch); + } + } + /** * Don't use {@link Mono#flatMapMany(Function)} to implicitly convert a {@link Mono} to a {@link * Flux}. @@ -271,6 +310,43 @@ Flux after(Flux flux) { } } + /** + * Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional + * subscription. + */ + static final class ConcatMapIterableIdentity { + @BeforeTemplate + Flux before(Flux> flux) { + return Refaster.anyOf( + flux.concatMap(list -> Flux.fromIterable(list)), flux.concatMap(Flux::fromIterable)); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + Flux after(Flux> flux) { + return flux.concatMapIterable(identity()); + } + } + + /** + * Prefer {@link Flux#concatMapIterable(Function, int)} over alternatives that require an + * additional subscription. + */ + static final class ConcatMapIterableIdentityWithPrefetch { + @BeforeTemplate + Flux before(Flux> flux, int prefetch) { + return Refaster.anyOf( + flux.concatMap(list -> Flux.fromIterable(list), prefetch), + flux.concatMap(Flux::fromIterable, prefetch)); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + Flux after(Flux> flux, int prefetch) { + return flux.concatMapIterable(identity(), prefetch); + } + } + /** Prefer {@link Mono#onErrorComplete()} over more contrived alternatives. */ static final class MonoOnErrorComplete { @BeforeTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java index 4844a8dcab..aef41daaca 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestInput.java @@ -69,10 +69,19 @@ ImmutableSet> testFluxConcatMap() { Flux.just(1).flatMap(Mono::just, 1), Flux.just(2).flatMapSequential(Mono::just, 1)); } + ImmutableSet> testFluxConcatMapWithPrefetch() { + return ImmutableSet.of( + Flux.just(1).flatMap(Mono::just, 1, 3), Flux.just(2).flatMapSequential(Mono::just, 1, 4)); + } + Flux testFluxConcatMapIterable() { return Flux.just(1, 2).flatMapIterable(ImmutableList::of); } + Flux testFluxConcatMapIterableWithPrefetch() { + return Flux.just(1, 2).flatMapIterable(ImmutableList::of, 3); + } + Flux testMonoFlatMapToFlux() { return Mono.just("foo").flatMapMany(s -> Mono.just(s + s)); } @@ -95,6 +104,18 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } + ImmutableSet> testConcatMapIterableIdentity() { + return ImmutableSet.of( + Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list)), + Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable)); + } + + ImmutableSet> testConcatMapIterableIdentityWithPrefetch() { + return ImmutableSet.of( + Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list), 1), + Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable, 2)); + } + Mono testMonoOnErrorComplete() { return Mono.just(1).onErrorResume(e -> Mono.empty()); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java index 34a39c6259..a57a3c561a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refastertemplates/ReactorTemplatesTestOutput.java @@ -1,6 +1,7 @@ package tech.picnic.errorprone.refastertemplates; import static com.google.common.collect.MoreCollectors.toOptional; +import static java.util.function.Function.identity; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableList; @@ -68,10 +69,19 @@ ImmutableSet> testFluxConcatMap() { return ImmutableSet.of(Flux.just(1).concatMap(Mono::just), Flux.just(2).concatMap(Mono::just)); } + ImmutableSet> testFluxConcatMapWithPrefetch() { + return ImmutableSet.of( + Flux.just(1).concatMap(Mono::just, 3), Flux.just(2).concatMap(Mono::just, 4)); + } + Flux testFluxConcatMapIterable() { return Flux.just(1, 2).concatMapIterable(ImmutableList::of); } + Flux testFluxConcatMapIterableWithPrefetch() { + return Flux.just(1, 2).concatMapIterable(ImmutableList::of, 3); + } + Flux testMonoFlatMapToFlux() { return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux(); } @@ -94,6 +104,18 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } + ImmutableSet> testConcatMapIterableIdentity() { + return ImmutableSet.of( + Flux.just(ImmutableList.of("foo")).concatMapIterable(identity()), + Flux.just(ImmutableList.of("bar")).concatMapIterable(identity())); + } + + ImmutableSet> testConcatMapIterableIdentityWithPrefetch() { + return ImmutableSet.of( + Flux.just(ImmutableList.of("foo")).concatMapIterable(identity(), 1), + Flux.just(ImmutableList.of("bar")).concatMapIterable(identity(), 2)); + } + Mono testMonoOnErrorComplete() { return Mono.just(1).onErrorComplete(); }