Skip to content

Commit

Permalink
Introduce some Refaster rules that avoid nested Publishers (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
werli authored Nov 28, 2022
1 parent 6d15cfe commit f46859a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ Flux<T> after(Flux<T> flux) {
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
Expand All @@ -337,7 +340,9 @@ Flux<S> before(
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
Expand Down Expand Up @@ -639,6 +644,32 @@ Flux<S> after(Flux<T> flux) {
}
}

/** 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -104,12 +105,16 @@ ImmutableSet<Flux<Integer>> testFluxSwitchIfEmptyOfEmptyPublisher() {

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() {
Expand Down Expand Up @@ -207,6 +212,14 @@ Flux<Number> testFluxCast() {
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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ ImmutableSet<Flux<Integer>> testFluxSwitchIfEmptyOfEmptyPublisher() {
}

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() {
Expand Down Expand Up @@ -206,6 +211,14 @@ Flux<Number> testFluxCast() {
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()),
Expand Down

0 comments on commit f46859a

Please sign in to comment.