mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Introduce some Refaster rules that avoid nested Publishers (#374)
This commit is contained in:
@@ -320,7 +320,10 @@ final class ReactorRules {
|
||||
static final class FluxConcatMap<T, S> {
|
||||
@BeforeTemplate
|
||||
Flux<S> before(Flux<T> flux, Function<? super T, ? extends Publisher<? extends S>> 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 @@ final class ReactorRules {
|
||||
Function<? super T, ? extends Publisher<? extends S>> 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
|
||||
@@ -639,6 +644,32 @@ final class ReactorRules {
|
||||
}
|
||||
}
|
||||
|
||||
/** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */
|
||||
static final class MonoFlatMap<S, T> {
|
||||
@BeforeTemplate
|
||||
Mono<T> before(Mono<S> mono, Function<? super S, ? extends Mono<? extends T>> function) {
|
||||
return mono.map(function).flatMap(identity());
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
Mono<T> after(Mono<S> mono, Function<? super S, ? extends Mono<? extends T>> function) {
|
||||
return mono.flatMap(function);
|
||||
}
|
||||
}
|
||||
|
||||
/** Prefer {@link Mono#flatMapMany(Function)} over more contrived alternatives. */
|
||||
static final class MonoFlatMapMany<S, T> {
|
||||
@BeforeTemplate
|
||||
Flux<T> before(Mono<S> mono, Function<? super S, ? extends Publisher<? extends T>> function) {
|
||||
return mono.map(function).flatMapMany(identity());
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
Flux<T> after(Mono<S> mono, Function<? super S, ? extends Publisher<? extends T>> function) {
|
||||
return mono.flatMapMany(function);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional
|
||||
* subscription.
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package tech.picnic.errorprone.refasterrules;
|
||||
|
||||
import static java.util.function.Function.identity;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
@@ -104,12 +105,16 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
|
||||
|
||||
ImmutableSet<Flux<Integer>> 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<Flux<Integer>> 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<Integer> testFluxConcatMapIterable() {
|
||||
@@ -207,6 +212,14 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
|
||||
return Flux.just(1).map(Number.class::cast);
|
||||
}
|
||||
|
||||
Mono<String> testMonoFlatMap() {
|
||||
return Mono.just("foo").map(Mono::just).flatMap(identity());
|
||||
}
|
||||
|
||||
Flux<String> testMonoFlatMapMany() {
|
||||
return Mono.just("foo").map(Mono::just).flatMapMany(identity());
|
||||
}
|
||||
|
||||
ImmutableSet<Flux<String>> testConcatMapIterableIdentity() {
|
||||
return ImmutableSet.of(
|
||||
Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list)),
|
||||
|
||||
@@ -108,12 +108,17 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
|
||||
}
|
||||
|
||||
ImmutableSet<Flux<Integer>> 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<Flux<Integer>> 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<Integer> testFluxConcatMapIterable() {
|
||||
@@ -206,6 +211,14 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
|
||||
return Flux.just(1).cast(Number.class);
|
||||
}
|
||||
|
||||
Mono<String> testMonoFlatMap() {
|
||||
return Mono.just("foo").flatMap(Mono::just);
|
||||
}
|
||||
|
||||
Flux<String> testMonoFlatMapMany() {
|
||||
return Mono.just("foo").flatMapMany(Mono::just);
|
||||
}
|
||||
|
||||
ImmutableSet<Flux<String>> testConcatMapIterableIdentity() {
|
||||
return ImmutableSet.of(
|
||||
Flux.just(ImmutableList.of("foo")).concatMapIterable(identity()),
|
||||
|
||||
Reference in New Issue
Block a user