Suggest Flux#concatMap{,Iterable} usage in more contexts (#279)

This commit is contained in:
Bastien Diederichs
2022-10-08 11:12:41 +02:00
committed by GitHub
parent 4ec349582c
commit 902d4f7736
3 changed files with 119 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.MoreCollectors.toOptional;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.function.Function.identity;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.collect.MoreCollectors;
@@ -177,6 +178,26 @@ final class ReactorTemplates {
}
}
/** Prefer {@link Flux#concatMap(Function, int)} over more contrived alternatives. */
static final class FluxConcatMapWithPrefetch<T, S> {
@BeforeTemplate
Flux<S> before(
Flux<T> flux,
Function<? super T, ? extends Publisher<? extends S>> function,
int prefetch) {
return Refaster.anyOf(
flux.flatMap(function, 1, prefetch), flux.flatMapSequential(function, 1, prefetch));
}
@AfterTemplate
Flux<S> after(
Flux<T> flux,
Function<? super T, ? extends Publisher<? extends S>> function,
int prefetch) {
return flux.concatMap(function, prefetch);
}
}
/**
* Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#flatMapIterable(Function)}, as
* the former has equivalent semantics but a clearer name.
@@ -193,6 +214,24 @@ final class ReactorTemplates {
}
}
/**
* Prefer {@link Flux#concatMapIterable(Function, int)} over {@link Flux#flatMapIterable(Function,
* int)}, as the former has equivalent semantics but a clearer name.
*/
static final class FluxConcatMapIterableWithPrefetch<T, S> {
@BeforeTemplate
Flux<S> before(
Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function, int prefetch) {
return flux.flatMapIterable(function, prefetch);
}
@AfterTemplate
Flux<S> after(
Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function, int prefetch) {
return flux.concatMapIterable(function, prefetch);
}
}
/**
* Don't use {@link Mono#flatMapMany(Function)} to implicitly convert a {@link Mono} to a {@link
* Flux}.
@@ -271,6 +310,43 @@ final class ReactorTemplates {
}
}
/**
* Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional
* subscription.
*/
static final class ConcatMapIterableIdentity<T> {
@BeforeTemplate
Flux<T> before(Flux<? extends Iterable<T>> flux) {
return Refaster.anyOf(
flux.concatMap(list -> Flux.fromIterable(list)), flux.concatMap(Flux::fromIterable));
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Flux<T> after(Flux<? extends Iterable<T>> flux) {
return flux.concatMapIterable(identity());
}
}
/**
* Prefer {@link Flux#concatMapIterable(Function, int)} over alternatives that require an
* additional subscription.
*/
static final class ConcatMapIterableIdentityWithPrefetch<T> {
@BeforeTemplate
Flux<T> before(Flux<? extends Iterable<T>> flux, int prefetch) {
return Refaster.anyOf(
flux.concatMap(list -> Flux.fromIterable(list), prefetch),
flux.concatMap(Flux::fromIterable, prefetch));
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Flux<T> after(Flux<? extends Iterable<T>> flux, int prefetch) {
return flux.concatMapIterable(identity(), prefetch);
}
}
/** Prefer {@link Mono#onErrorComplete()} over more contrived alternatives. */
static final class MonoOnErrorComplete<T> {
@BeforeTemplate

View File

@@ -69,10 +69,19 @@ final class ReactorTemplatesTest implements RefasterTemplateTestCase {
Flux.just(1).flatMap(Mono::just, 1), Flux.just(2).flatMapSequential(Mono::just, 1));
}
ImmutableSet<Flux<Integer>> testFluxConcatMapWithPrefetch() {
return ImmutableSet.of(
Flux.just(1).flatMap(Mono::just, 1, 3), Flux.just(2).flatMapSequential(Mono::just, 1, 4));
}
Flux<Integer> testFluxConcatMapIterable() {
return Flux.just(1, 2).flatMapIterable(ImmutableList::of);
}
Flux<Integer> testFluxConcatMapIterableWithPrefetch() {
return Flux.just(1, 2).flatMapIterable(ImmutableList::of, 3);
}
Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMapMany(s -> Mono.just(s + s));
}
@@ -95,6 +104,18 @@ final class ReactorTemplatesTest implements RefasterTemplateTestCase {
return Flux.just(1).map(Number.class::cast);
}
ImmutableSet<Flux<String>> testConcatMapIterableIdentity() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list)),
Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable));
}
ImmutableSet<Flux<String>> testConcatMapIterableIdentityWithPrefetch() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list), 1),
Flux.just(ImmutableList.of("bar")).concatMap(Flux::fromIterable, 2));
}
Mono<Integer> testMonoOnErrorComplete() {
return Mono.just(1).onErrorResume(e -> Mono.empty());
}

View File

@@ -1,6 +1,7 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.MoreCollectors.toOptional;
import static java.util.function.Function.identity;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.collect.ImmutableList;
@@ -68,10 +69,19 @@ final class ReactorTemplatesTest implements RefasterTemplateTestCase {
return ImmutableSet.of(Flux.just(1).concatMap(Mono::just), Flux.just(2).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<Integer> testFluxConcatMapIterable() {
return Flux.just(1, 2).concatMapIterable(ImmutableList::of);
}
Flux<Integer> testFluxConcatMapIterableWithPrefetch() {
return Flux.just(1, 2).concatMapIterable(ImmutableList::of, 3);
}
Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux();
}
@@ -94,6 +104,18 @@ final class ReactorTemplatesTest implements RefasterTemplateTestCase {
return Flux.just(1).cast(Number.class);
}
ImmutableSet<Flux<String>> testConcatMapIterableIdentity() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("foo")).concatMapIterable(identity()),
Flux.just(ImmutableList.of("bar")).concatMapIterable(identity()));
}
ImmutableSet<Flux<String>> testConcatMapIterableIdentityWithPrefetch() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("foo")).concatMapIterable(identity(), 1),
Flux.just(ImmutableList.of("bar")).concatMapIterable(identity(), 2));
}
Mono<Integer> testMonoOnErrorComplete() {
return Mono.just(1).onErrorComplete();
}