Skip to content

Commit

Permalink
Introduce template to rewrite flatMapIterable to concatMapIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie authored and Stephan202 committed Jan 15, 2022
1 parent ba13b73 commit 5d362c2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import reactor.test.publisher.PublisherProbe;
import tech.picnic.errorprone.bugpatterns.FluxFlatMapUsageCheck;

/** Refaster templates related to Reactor expressions and statements. */
final class ReactorTemplates {
Expand Down Expand Up @@ -155,6 +156,25 @@ Flux<S> after(Flux<T> flux, Function<? super T, ? extends Publisher<? extends S>
}
}

/**
* Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#concatMapIterable(Function)}
* to be consistent with {@link FluxFlatMapUsageCheck}.
*
* <p>NB: Both implementations emit values in a deterministic order and there is no difference
* with eager or lazy inner subscriptions. This means that both implementations are *equivalent*.
*/
static final class FluxConcatMapIterable<T, S> {
@BeforeTemplate
Flux<S> before(Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function) {
return flux.flatMapIterable(function);
}

@AfterTemplate
Flux<S> after(Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function) {
return flux.concatMapIterable(function);
}
}

/**
* Don't use {@link Mono#flatMapMany(Function)} to implicitly convert a {@link Mono} to a {@link
* Flux}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.bugpatterns;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.time.Duration;
import java.util.Optional;
Expand Down Expand Up @@ -50,6 +51,10 @@ ImmutableSet<Flux<Integer>> testFluxConcatMap() {
Flux.just(1).flatMap(Mono::just, 1), Flux.just(2).flatMapSequential(Mono::just, 1));
}

Flux<Integer> testFluxConcatMapIterable() {
return Flux.just(1, 2).flatMapIterable(ImmutableList::of);
}

Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMapMany(s -> Mono.just(s + s));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.google.common.collect.MoreCollectors.toOptional;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.time.Duration;
import java.util.Optional;
Expand Down Expand Up @@ -50,6 +51,10 @@ ImmutableSet<Flux<Integer>> testFluxConcatMap() {
return ImmutableSet.of(Flux.just(1).concatMap(Mono::just), Flux.just(2).concatMap(Mono::just));
}

Flux<Integer> testFluxConcatMapIterable() {
return Flux.just(1, 2).concatMapIterable(ImmutableList::of);
}

Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux();
}
Expand Down

0 comments on commit 5d362c2

Please sign in to comment.