From a74fed0f6ac8192d7b6a4baf8a10ccb48635607b Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 6 Oct 2022 07:42:16 +0200 Subject: [PATCH] Suggestions --- .../refastertemplates/ReactorTemplates.java | 60 +++++++++++++++---- .../ReactorTemplatesTestInput.java | 21 +++++-- .../ReactorTemplatesTestOutput.java | 21 +++++-- 3 files changed, 79 insertions(+), 23 deletions(-) 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 981537fbb69..8a6cd695a90 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 @@ -178,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. @@ -194,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}. @@ -273,10 +311,10 @@ Flux after(Flux flux) { } /** - * Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#concatMap(Function)} with - * {@link Flux#fromIterable(Iterable)}. + * Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional + * subscription. */ - static final class FluxConcatMapFromIterable { + static final class ConcatMapIterableIdentity { @BeforeTemplate Flux before(Flux> flux) { return Refaster.anyOf( @@ -291,21 +329,21 @@ Flux after(Flux> flux) { } /** - * Prefer {@link Flux#flatMapIterable(Function, int)} over {@link Flux#flatMap(Function, int)} - * with {@link Flux#fromIterable(Iterable)}. + * Prefer {@link Flux#concatMapIterable(Function, int)} over alternatives that require an + * additional subscription. */ - static final class FluxFlatMapFromIterable { + static final class ConcatMapIterableIdentityWithPrefetch { @BeforeTemplate - Flux before(Flux> flux, int concurrency) { + Flux before(Flux> flux, int prefetch) { return Refaster.anyOf( - flux.flatMap(list -> Flux.fromIterable(list), concurrency), - flux.flatMap(Flux::fromIterable, concurrency)); + flux.concatMap(list -> Flux.fromIterable(list), prefetch), + flux.concatMap(Flux::fromIterable, prefetch)); } @AfterTemplate @UseImportPolicy(STATIC_IMPORT_ALWAYS) - Flux after(Flux> flux, int concurrency) { - return flux.flatMapIterable(identity(), concurrency); + Flux after(Flux> flux, int prefetch) { + return flux.concatMapIterable(identity(), prefetch); } } 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 570e667981e..3747a3b066c 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,16 +104,16 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } - ImmutableSet> testFluxConcatMapFromIterable() { + ImmutableSet> testConcatMapIterableIdentity() { return ImmutableSet.of( - Flux.just(ImmutableList.of("1")).concatMap(list -> Flux.fromIterable(list)), - Flux.just(ImmutableList.of("1")).concatMap(Flux::fromIterable)); + Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list)), + Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable)); } - ImmutableSet> testFluxFlatMapFromIterable() { + ImmutableSet> testConcatMapIterableIdentityWithPrefetch() { return ImmutableSet.of( - Flux.just(ImmutableList.of("1")).flatMap(list -> Flux.fromIterable(list), 1), - Flux.just(ImmutableList.of("1")).flatMap(Flux::fromIterable, 2)); + Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list), 1), + Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable, 2)); } ImmutableSet> testPublisherProbeEmpty() { 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 7bda8462528..39a5f3147ef 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 @@ -69,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(); } @@ -95,16 +104,16 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } - ImmutableSet> testFluxConcatMapFromIterable() { + ImmutableSet> testConcatMapIterableIdentity() { return ImmutableSet.of( - Flux.just(ImmutableList.of("1")).concatMapIterable(identity()), - Flux.just(ImmutableList.of("1")).concatMapIterable(identity())); + Flux.just(ImmutableList.of("foo")).concatMapIterable(identity()), + Flux.just(ImmutableList.of("bar")).concatMapIterable(identity())); } - ImmutableSet> testFluxFlatMapFromIterable() { + ImmutableSet> testConcatMapIterableIdentityWithPrefetch() { return ImmutableSet.of( - Flux.just(ImmutableList.of("1")).flatMapIterable(identity(), 1), - Flux.just(ImmutableList.of("1")).flatMapIterable(identity(), 2)); + Flux.just(ImmutableList.of("foo")).concatMapIterable(identity(), 1), + Flux.just(ImmutableList.of("bar")).concatMapIterable(identity(), 2)); } ImmutableSet> testPublisherProbeEmpty() {