From 9ada0783dd36d154c98df8188e28cfe0da225e35 Mon Sep 17 00:00:00 2001 From: Vincent Koeman Date: Mon, 9 Oct 2023 11:30:50 +0200 Subject: [PATCH] Introduce `Flux{Empty,Just}` Refaster rules (#815) --- .../refasterrules/ReactorRules.java | 68 +++++++++++++++++++ .../refasterrules/ReactorRulesTestInput.java | 31 +++++++++ .../refasterrules/ReactorRulesTestOutput.java | 31 +++++++++ 3 files changed, 130 insertions(+) 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 3a72d4e818..68650e5b45 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 @@ -426,6 +426,74 @@ Flux after(Flux flux, T object) { } } + /** Prefer {@link Flux#empty()} over more contrived alternatives. */ + // XXX: In combination with the `IsEmpty` matcher introduced by + // https://github.com/PicnicSupermarket/error-prone-support/pull/744, the non-varargs overloads of + // most methods referenced here can be rewritten as well. Additionally, some invocations of + // methods such as `Flux#fromIterable`, `Flux#fromArray` and `Flux#justOrEmpty` can also be + // rewritten. + static final class FluxEmpty> { + @BeforeTemplate + Flux before( + int prefetch, + Function combinator, + Comparator comparator) { + return Refaster.anyOf( + Flux.concat(), + Flux.concatDelayError(), + Flux.firstWithSignal(), + Flux.just(), + Flux.merge(), + Flux.merge(prefetch), + Flux.mergeComparing(comparator), + Flux.mergeComparing(prefetch, comparator), + Flux.mergeComparingDelayError(prefetch, comparator), + Flux.mergeDelayError(prefetch), + Flux.mergePriority(comparator), + Flux.mergePriority(prefetch, comparator), + Flux.mergePriorityDelayError(prefetch, comparator), + Flux.mergeSequential(), + Flux.mergeSequential(prefetch), + Flux.mergeSequentialDelayError(prefetch), + Flux.zip(combinator), + Flux.zip(combinator, prefetch)); + } + + @BeforeTemplate + Flux before(int prefetch, Function combinator) { + return Refaster.anyOf( + Flux.combineLatest(combinator), Flux.combineLatest(combinator, prefetch)); + } + + @BeforeTemplate + Flux before() { + return Refaster.anyOf(Flux.mergeComparing(), Flux.mergePriority()); + } + + @BeforeTemplate + Flux before(int start) { + return Flux.range(start, 0); + } + + @AfterTemplate + Flux after() { + return Flux.empty(); + } + } + + /** Prefer {@link Flux#just(Object)} over more contrived alternatives. */ + static final class FluxJust { + @BeforeTemplate + Flux before(int start) { + return Flux.range(start, 1); + } + + @AfterTemplate + Flux after(int start) { + return Flux.just(start); + } + } + /** Don't unnecessarily transform a {@link Mono} to an equivalent instance. */ static final class MonoIdentity { @BeforeTemplate 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 f077307dcd..b10b47f57b 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 @@ -148,6 +148,37 @@ ImmutableSet> testFluxDefaultIfEmpty() { Flux.just("baz").switchIfEmpty(Flux.just("qux"))); } + ImmutableSet> testFluxEmpty() { + return ImmutableSet.of( + Flux.concat(), + Flux.concatDelayError(), + Flux.firstWithSignal(), + Flux.just(), + Flux.merge(), + Flux.merge(1), + Flux.mergeComparing((a, b) -> 0), + Flux.mergeComparing(1, (a, b) -> 0), + Flux.mergeComparingDelayError(1, (a, b) -> 0), + Flux.mergeDelayError(1), + Flux.mergePriority((a, b) -> 0), + Flux.mergePriority(1, (a, b) -> 0), + Flux.mergePriorityDelayError(1, (a, b) -> 0), + Flux.mergeSequential(), + Flux.mergeSequential(1), + Flux.mergeSequentialDelayError(1), + Flux.zip(v -> v), + Flux.zip(v -> v, 1), + Flux.combineLatest(v -> v), + Flux.combineLatest(v -> v, 1), + Flux.mergeComparing(), + Flux.mergePriority(), + Flux.range(0, 0)); + } + + Flux testFluxJust() { + return Flux.range(0, 1); + } + ImmutableSet> testMonoIdentity() { return ImmutableSet.of( Mono.just(1).switchIfEmpty(Mono.empty()), 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 e762c242ad..98279c9060 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 @@ -152,6 +152,37 @@ ImmutableSet> testFluxDefaultIfEmpty() { Flux.just("foo").defaultIfEmpty("bar"), Flux.just("baz").defaultIfEmpty("qux")); } + ImmutableSet> testFluxEmpty() { + return ImmutableSet.of( + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty(), + Flux.empty()); + } + + Flux testFluxJust() { + return Flux.just(0); + } + ImmutableSet> testMonoIdentity() { return ImmutableSet.of( Mono.just(1),