Skip to content

Commit

Permalink
ANS-1289 Prefer flatMapIterable(identity()) over `flatMap(i -> Flux…
Browse files Browse the repository at this point in the history
….fromIterable(i))`
  • Loading branch information
Bastien Diederichs committed Oct 5, 2022
1 parent 9ad8c27 commit 6cf3c93
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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 @@ -271,6 +272,23 @@ Flux<S> after(Flux<T> flux) {
}
}

/**
* Prefer {@link Flux#flatMapIterable(Function)} (Class)} over {@link Flux#flatMap(Function)} with
* {@link Flux#fromIterable(Iterable)}.
*/
static final class FluxFlatMapIterable<S> {
@BeforeTemplate
Flux<S> before(Flux<? extends Iterable<S>> flux) {
return flux.flatMap(list -> Flux.fromIterable(list));
}

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

/** Prefer {@link PublisherProbe#empty()}} over more verbose alternatives. */
static final class PublisherProbeEmpty<T> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ Flux<Number> testFluxCast() {
return Flux.just(1).map(Number.class::cast);
}

Flux<String> testFluxFlatMapIterable() {
return Flux.just(ImmutableList.of("1")).flatMap(list -> Flux.fromIterable(list));
}

ImmutableSet<PublisherProbe<Void>> testPublisherProbeEmpty() {
return ImmutableSet.of(PublisherProbe.of(Mono.empty()), PublisherProbe.of(Flux.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 @@ -94,6 +95,10 @@ Flux<Number> testFluxCast() {
return Flux.just(1).cast(Number.class);
}

Flux<String> testFluxFlatMapIterable() {
return Flux.just(ImmutableList.of("1")).flatMapIterable(identity());
}

ImmutableSet<PublisherProbe<Void>> testPublisherProbeEmpty() {
return ImmutableSet.of(PublisherProbe.empty(), PublisherProbe.empty());
}
Expand Down

0 comments on commit 6cf3c93

Please sign in to comment.