Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer flatMapIterable(identity()) over flatMap(i -> FluxfromIterable(i)) #279

Merged
merged 4 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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;
Expand Down Expand Up @@ -177,6 +178,26 @@ Flux<S> after(Flux<T> flux, Function<? super T, ? extends Publisher<? extends S>
}
}

/** 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.
Expand All @@ -193,6 +214,24 @@ Flux<S> after(Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>>
}
}

/**
* 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}.
Expand Down Expand Up @@ -271,6 +310,43 @@ Flux<S> after(Flux<T> flux) {
}
}

/**
* Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional
* subscription.
*/
static final class ConcatMapIterableIdentity<S> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use S when we need two generics, in this case we can simply use <T>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes :). (Also something to auto-unify on day 🙈.)

@BeforeTemplate
Flux<S> before(Flux<? extends Iterable<S>> flux) {
return Refaster.anyOf(
flux.concatMap(list -> Flux.fromIterable(list)), flux.concatMap(Flux::fromIterable));
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Flux<S> after(Flux<? extends Iterable<S>> flux) {
return flux.concatMapIterable(identity());
}
}

/**
* Prefer {@link Flux#concatMapIterable(Function, int)} over alternatives that require an
* additional subscription.
*/
static final class ConcatMapIterableIdentityWithPrefetch<S> {
@BeforeTemplate
Flux<S> before(Flux<? extends Iterable<S>> flux, int prefetch) {
return Refaster.anyOf(
flux.concatMap(list -> Flux.fromIterable(list), prefetch),
flux.concatMap(Flux::fromIterable, prefetch));
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Flux<S> after(Flux<? extends Iterable<S>> flux, int prefetch) {
return flux.concatMapIterable(identity(), prefetch);
}
}

/** Prefer {@link Mono#onErrorComplete()} over more contrived alternatives. */
static final class MonoOnErrorComplete<T> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,19 @@ ImmutableSet<Flux<Integer>> testFluxConcatMap() {
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));
}
Expand All @@ -95,6 +104,18 @@ Flux<Number> testFluxCast() {
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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -68,10 +69,19 @@ ImmutableSet<Flux<Integer>> testFluxConcatMap() {
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();
}
Expand All @@ -94,6 +104,18 @@ Flux<Number> testFluxCast() {
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();
}
Expand Down