Skip to content

Commit

Permalink
Introduce Reactor identity Refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
werli authored and rickie committed Nov 28, 2022
1 parent 6d15cfe commit 5dacf5f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,58 @@ Flux<S> after(Flux<T> flux) {
}
}

/** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */
static final class MonoFlatMapIdentity<S, T> {
@BeforeTemplate
Mono<T> before(Mono<S> mono, Function<S, Mono<T>> function) {
return mono.map(function).flatMap(identity());
}

@AfterTemplate
Mono<T> after(Mono<S> mono, Function<S, Mono<T>> function) {
return mono.flatMap(function);
}
}

/** Prefer {@link Mono#flatMapMany(Function)} over more contrived alternatives. */
static final class FlatMapManyIdentity<S, T> {
@BeforeTemplate
Flux<T> before(Mono<S> mono, Function<S, ? extends Publisher<T>> function) {
return mono.map(function).flatMapMany(identity());
}

@AfterTemplate
Flux<T> after(Mono<S> mono, Function<S, ? extends Publisher<T>> function) {
return mono.flatMapMany(function);
}
}

/** Prefer {@link Flux#concatMap(Function)} over more contrived alternatives. */
static final class ConcatMapIdentity<S, T> {
@BeforeTemplate
Flux<T> before(Flux<S> flux, Function<S, ? extends Publisher<T>> function) {
return flux.map(function).concatMap(identity());
}

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

/** Prefer {@link Flux#concatMap(Function, int)} over more contrived alternatives. */
static final class ConcatMapIdentityWithPrefetch<S, T> {
@BeforeTemplate
Flux<T> before(Flux<S> flux, Function<S, ? extends Publisher<T>> function, int prefetch) {
return flux.map(function).concatMap(identity(), prefetch);
}

@AfterTemplate
Flux<T> after(Flux<S> flux, Function<S, ? extends Publisher<T>> function, int prefetch) {
return flux.concatMap(function, prefetch);
}
}

/**
* 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 @@ -207,6 +208,28 @@ Flux<Number> testFluxCast() {
return Flux.just(1).map(Number.class::cast);
}

Mono<String> testMonoFlatMapIdentity() {
return Mono.just("foo").map(Mono::just).flatMap(identity());
}

ImmutableSet<Flux<String>> testFlatMapManyIdentity() {
return ImmutableSet.of(
Mono.just("foo").map(Mono::just).flatMapMany(identity()),
Mono.just("foo").map(Flux::just).flatMapMany(identity()));
}

ImmutableSet<Flux<String>> testConcatMapIdentity() {
return ImmutableSet.of(
Flux.just("foo", "bar").map(Mono::just).concatMap(identity()),
Flux.just("foo", "bar").map(Flux::just).concatMap(identity()));
}

ImmutableSet<Flux<String>> testConcatMapIdentityWithPrefetch() {
return ImmutableSet.of(
Flux.just("foo", "bar").map(Mono::just).concatMap(identity(), 1),
Flux.just("foo", "bar").map(Flux::just).concatMap(identity(), 1));
}

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 @@ -206,6 +206,27 @@ Flux<Number> testFluxCast() {
return Flux.just(1).cast(Number.class);
}

Mono<String> testMonoFlatMapIdentity() {
return Mono.just("foo").flatMap(Mono::just);
}

ImmutableSet<Flux<String>> testFlatMapManyIdentity() {
return ImmutableSet.of(
Mono.just("foo").flatMapMany(Mono::just), Mono.just("foo").flatMapMany(Flux::just));
}

ImmutableSet<Flux<String>> testConcatMapIdentity() {
return ImmutableSet.of(
Flux.just("foo", "bar").concatMap(Mono::just),
Flux.just("foo", "bar").concatMap(Flux::just));
}

ImmutableSet<Flux<String>> testConcatMapIdentityWithPrefetch() {
return ImmutableSet.of(
Flux.just("foo", "bar").concatMap(Mono::just, 1),
Flux.just("foo", "bar").concatMap(Flux::just, 1));
}

ImmutableSet<Flux<String>> testConcatMapIterableIdentity() {
return ImmutableSet.of(
Flux.just(ImmutableList.of("foo")).concatMapIterable(identity()),
Expand Down

0 comments on commit 5dacf5f

Please sign in to comment.