From ded0a48258264f93d25e5c1b036ce76befb90ba9 Mon Sep 17 00:00:00 2001 From: Dirk van Bokkem Date: Thu, 22 Feb 2024 09:17:14 +0100 Subject: [PATCH] Introduce `FluxFromIterable` Refaster rule (#1047) --- .../errorprone/refasterrules/ReactorRules.java | 13 +++++++++++++ .../refasterrules/ReactorRulesTestInput.java | 4 ++++ .../refasterrules/ReactorRulesTestOutput.java | 4 ++++ 3 files changed, 21 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 467c526f63..57cb55a636 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 @@ -1205,6 +1205,19 @@ Flux after(Flux> flux, int prefetch) { } } + /** Prefer {@link Flux#fromIterable(Iterable)} over less efficient alternatives. */ + static final class FluxFromIterable { + @BeforeTemplate + Flux before(Collection collection) { + return Flux.fromStream(collection.stream()); + } + + @AfterTemplate + Flux after(Collection collection) { + return Flux.fromIterable(collection); + } + } + /** * Prefer {@link Flux#count()} followed by a conversion from {@code long} to {@code int} over * collecting into a list and counting its elements. 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 25e0f5144e..f2f04dc3ed 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 @@ -432,6 +432,10 @@ ImmutableSet> testConcatMapIterableIdentityWithPrefetch() { Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable, 2)); } + Flux testFluxFromIterable() { + return Flux.fromStream(ImmutableList.of("foo").stream()); + } + ImmutableSet> testFluxCountMapMathToIntExact() { return ImmutableSet.of( Flux.just(1).collect(toImmutableList()).map(Collection::size), 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 dd25437fb6..6bfd98f972 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 @@ -427,6 +427,10 @@ ImmutableSet> testConcatMapIterableIdentityWithPrefetch() { Flux.just(ImmutableList.of("bar")).concatMapIterable(identity(), 2)); } + Flux testFluxFromIterable() { + return Flux.fromIterable(ImmutableList.of("foo")); + } + ImmutableSet> testFluxCountMapMathToIntExact() { return ImmutableSet.of( Flux.just(1).count().map(Math::toIntExact),