From 193979fb34b0ea3b82f98d58b2784e4e259acfc4 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 27 Nov 2022 15:40:15 +0100 Subject: [PATCH] Suggestions --- .../refasterrules/ReactorRules.java | 47 +++++-------------- .../refasterrules/ReactorRulesTestInput.java | 28 ++++------- .../refasterrules/ReactorRulesTestOutput.java | 28 ++++------- 3 files changed, 32 insertions(+), 71 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java index 34fcc11e03..d3695bec7f 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java @@ -320,7 +320,10 @@ Flux after(Flux flux) { static final class FluxConcatMap { @BeforeTemplate Flux before(Flux flux, Function> function) { - return Refaster.anyOf(flux.flatMap(function, 1), flux.flatMapSequential(function, 1)); + return Refaster.anyOf( + flux.flatMap(function, 1), + flux.flatMapSequential(function, 1), + flux.map(function).concatMap(identity())); } @AfterTemplate @@ -337,7 +340,9 @@ Flux before( Function> function, int prefetch) { return Refaster.anyOf( - flux.flatMap(function, 1, prefetch), flux.flatMapSequential(function, 1, prefetch)); + flux.flatMap(function, 1, prefetch), + flux.flatMapSequential(function, 1, prefetch), + flux.map(function).concatMap(identity(), prefetch)); } @AfterTemplate @@ -640,57 +645,31 @@ Flux after(Flux flux) { } /** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */ - static final class MonoFlatMapIdentity { + static final class MonoFlatMap { @BeforeTemplate - Mono before(Mono mono, Function> function) { + Mono before(Mono mono, Function> function) { return mono.map(function).flatMap(identity()); } @AfterTemplate - Mono after(Mono mono, Function> function) { + Mono after(Mono mono, Function> function) { return mono.flatMap(function); } } /** Prefer {@link Mono#flatMapMany(Function)} over more contrived alternatives. */ - static final class FlatMapManyIdentity { + static final class MonoFlatMapMany { @BeforeTemplate - Flux before(Mono mono, Function> function) { + Flux before(Mono mono, Function> function) { return mono.map(function).flatMapMany(identity()); } @AfterTemplate - Flux after(Mono mono, Function> function) { + Flux after(Mono mono, Function> function) { return mono.flatMapMany(function); } } - /** Prefer {@link Flux#concatMap(Function)} over more contrived alternatives. */ - static final class ConcatMapIdentity { - @BeforeTemplate - Flux before(Flux flux, Function> function) { - return flux.map(function).concatMap(identity()); - } - - @AfterTemplate - Flux after(Flux flux, Function> function) { - return flux.concatMap(function); - } - } - - /** Prefer {@link Flux#concatMap(Function, int)} over more contrived alternatives. */ - static final class ConcatMapIdentityWithPrefetch { - @BeforeTemplate - Flux before(Flux flux, Function> function, int prefetch) { - return flux.map(function).concatMap(identity(), prefetch); - } - - @AfterTemplate - Flux after(Flux flux, Function> function, int prefetch) { - return flux.concatMap(function, prefetch); - } - } - /** * Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional * subscription. diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java index f7c5543000..daf40d369f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java @@ -105,12 +105,16 @@ ImmutableSet> testFluxSwitchIfEmptyOfEmptyPublisher() { ImmutableSet> testFluxConcatMap() { return ImmutableSet.of( - Flux.just(1).flatMap(Mono::just, 1), Flux.just(2).flatMapSequential(Mono::just, 1)); + Flux.just(1).flatMap(Mono::just, 1), + Flux.just(2).flatMapSequential(Mono::just, 1), + Flux.just(3).map(Mono::just).concatMap(identity())); } ImmutableSet> testFluxConcatMapWithPrefetch() { return ImmutableSet.of( - Flux.just(1).flatMap(Mono::just, 1, 3), Flux.just(2).flatMapSequential(Mono::just, 1, 4)); + Flux.just(1).flatMap(Mono::just, 1, 3), + Flux.just(2).flatMapSequential(Mono::just, 1, 4), + Flux.just(3).map(Mono::just).concatMap(identity(), 5)); } Flux testFluxConcatMapIterable() { @@ -208,26 +212,12 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } - Mono testMonoFlatMapIdentity() { + Mono testMonoFlatMap() { return Mono.just("foo").map(Mono::just).flatMap(identity()); } - ImmutableSet> testFlatMapManyIdentity() { - return ImmutableSet.of( - Mono.just("foo").map(Mono::just).flatMapMany(identity()), - Mono.just("foo").map(Flux::just).flatMapMany(identity())); - } - - ImmutableSet> testConcatMapIdentity() { - return ImmutableSet.of( - Flux.just("foo", "bar").map(Mono::just).concatMap(identity()), - Flux.just("foo", "bar").map(Flux::just).concatMap(identity())); - } - - ImmutableSet> testConcatMapIdentityWithPrefetch() { - return ImmutableSet.of( - Flux.just("foo", "bar").map(Mono::just).concatMap(identity(), 1), - Flux.just("foo", "bar").map(Flux::just).concatMap(identity(), 1)); + Flux testMonoFlatMapMany() { + return Mono.just("foo").map(Mono::just).flatMapMany(identity()); } ImmutableSet> testConcatMapIterableIdentity() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java index ddf1bc5fb6..df6fb70af4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java @@ -108,12 +108,17 @@ ImmutableSet> testFluxSwitchIfEmptyOfEmptyPublisher() { } ImmutableSet> testFluxConcatMap() { - return ImmutableSet.of(Flux.just(1).concatMap(Mono::just), Flux.just(2).concatMap(Mono::just)); + return ImmutableSet.of( + Flux.just(1).concatMap(Mono::just), + Flux.just(2).concatMap(Mono::just), + Flux.just(3).concatMap(Mono::just)); } ImmutableSet> testFluxConcatMapWithPrefetch() { return ImmutableSet.of( - Flux.just(1).concatMap(Mono::just, 3), Flux.just(2).concatMap(Mono::just, 4)); + Flux.just(1).concatMap(Mono::just, 3), + Flux.just(2).concatMap(Mono::just, 4), + Flux.just(3).concatMap(Mono::just, 5)); } Flux testFluxConcatMapIterable() { @@ -206,25 +211,12 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } - Mono testMonoFlatMapIdentity() { + Mono testMonoFlatMap() { return Mono.just("foo").flatMap(Mono::just); } - ImmutableSet> testFlatMapManyIdentity() { - return ImmutableSet.of( - Mono.just("foo").flatMapMany(Mono::just), Mono.just("foo").flatMapMany(Flux::just)); - } - - ImmutableSet> testConcatMapIdentity() { - return ImmutableSet.of( - Flux.just("foo", "bar").concatMap(Mono::just), - Flux.just("foo", "bar").concatMap(Flux::just)); - } - - ImmutableSet> testConcatMapIdentityWithPrefetch() { - return ImmutableSet.of( - Flux.just("foo", "bar").concatMap(Mono::just, 1), - Flux.just("foo", "bar").concatMap(Flux::just, 1)); + Flux testMonoFlatMapMany() { + return Mono.just("foo").flatMapMany(Mono::just); } ImmutableSet> testConcatMapIterableIdentity() {