Skip to content

Commit

Permalink
Introduce {Mono,Flux}Map{,NotNull} Refaster rules (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
cernat-catalin authored Oct 26, 2022
1 parent dc0f90e commit afb2a28
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,195 @@ Flux<S> after(
*/
abstract static class MonoFlatMapToFlux<T, S> {
@Placeholder(allowsIdentity = true)
abstract Mono<S> valueTransformation(@MayOptionallyUse T value);
abstract Mono<S> transformation(@MayOptionallyUse T value);

@BeforeTemplate
Flux<S> before(Mono<T> mono) {
return mono.flatMapMany(v -> valueTransformation(v));
return mono.flatMapMany(v -> transformation(v));
}

@AfterTemplate
Flux<S> after(Mono<T> mono) {
return mono.flatMap(v -> valueTransformation(v)).flux();
return mono.flatMap(v -> transformation(v)).flux();
}
}

/**
* Prefer {@link Mono#map(Function)} over alternatives that unnecessarily require an inner
* subscription.
*/
abstract static class MonoMap<T, S> {
@Placeholder(allowsIdentity = true)
abstract S transformation(@MayOptionallyUse T value);

@BeforeTemplate
Mono<S> before(Mono<T> mono) {
return mono.flatMap(x -> Mono.just(transformation(x)));
}

@AfterTemplate
Mono<S> after(Mono<T> mono) {
return mono.map(x -> transformation(x));
}
}

/**
* Prefer {@link Flux#map(Function)} over alternatives that unnecessarily require an inner
* subscription.
*/
abstract static class FluxMap<T, S> {
@Placeholder(allowsIdentity = true)
abstract S transformation(@MayOptionallyUse T value);

@BeforeTemplate
Flux<S> before(Flux<T> flux, boolean delayUntilEnd, int maxConcurrency, int prefetch) {
return Refaster.anyOf(
flux.concatMap(x -> Mono.just(transformation(x))),
flux.concatMap(x -> Flux.just(transformation(x))),
flux.concatMap(x -> Mono.just(transformation(x)), prefetch),
flux.concatMap(x -> Flux.just(transformation(x)), prefetch),
flux.concatMapDelayError(x -> Mono.just(transformation(x))),
flux.concatMapDelayError(x -> Flux.just(transformation(x))),
flux.concatMapDelayError(x -> Mono.just(transformation(x)), prefetch),
flux.concatMapDelayError(x -> Flux.just(transformation(x)), prefetch),
flux.concatMapDelayError(x -> Mono.just(transformation(x)), delayUntilEnd, prefetch),
flux.concatMapDelayError(x -> Flux.just(transformation(x)), delayUntilEnd, prefetch),
flux.flatMap(x -> Mono.just(transformation(x)), maxConcurrency),
flux.flatMap(x -> Flux.just(transformation(x)), maxConcurrency),
flux.flatMap(x -> Mono.just(transformation(x)), maxConcurrency, prefetch),
flux.flatMap(x -> Flux.just(transformation(x)), maxConcurrency, prefetch),
flux.flatMapDelayError(x -> Mono.just(transformation(x)), maxConcurrency, prefetch),
flux.flatMapDelayError(x -> Flux.just(transformation(x)), maxConcurrency, prefetch),
flux.flatMapSequential(x -> Mono.just(transformation(x)), maxConcurrency),
flux.flatMapSequential(x -> Flux.just(transformation(x)), maxConcurrency),
flux.flatMapSequential(x -> Mono.just(transformation(x)), maxConcurrency, prefetch),
flux.flatMapSequential(x -> Flux.just(transformation(x)), maxConcurrency, prefetch),
flux.flatMapSequentialDelayError(
x -> Mono.just(transformation(x)), maxConcurrency, prefetch),
flux.flatMapSequentialDelayError(
x -> Flux.just(transformation(x)), maxConcurrency, prefetch),
flux.switchMap(x -> Mono.just(transformation(x))),
flux.switchMap(x -> Flux.just(transformation(x))));
}

@AfterTemplate
Flux<S> after(Flux<T> flux) {
return flux.map(x -> transformation(x));
}
}

/**
* Prefer {@link Mono#mapNotNull(Function)} over alternatives that unnecessarily require an inner
* subscription.
*/
abstract static class MonoMapNotNull<T, S> {
@Placeholder(allowsIdentity = true)
abstract S transformation(@MayOptionallyUse T value);

@BeforeTemplate
Mono<S> before(Mono<T> mono) {
return mono.flatMap(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)), Mono.fromSupplier(() -> transformation(x))));
}

@AfterTemplate
Mono<S> after(Mono<T> mono) {
return mono.mapNotNull(x -> transformation(x));
}
}

/**
* Prefer {@link Flux#mapNotNull(Function)} over alternatives that unnecessarily require an inner
* subscription.
*/
abstract static class FluxMapNotNull<T, S> {
@Placeholder(allowsIdentity = true)
abstract S transformation(@MayOptionallyUse T value);

@BeforeTemplate
Publisher<S> before(Flux<T> flux, boolean delayUntilEnd, int maxConcurrency, int prefetch) {
return Refaster.anyOf(
flux.concatMap(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x)))),
flux.concatMap(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
prefetch),
flux.concatMapDelayError(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x)))),
flux.concatMapDelayError(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
prefetch),
flux.concatMapDelayError(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
delayUntilEnd,
prefetch),
flux.flatMap(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
maxConcurrency),
flux.flatMap(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
maxConcurrency,
prefetch),
flux.flatMapDelayError(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
maxConcurrency,
prefetch),
flux.flatMapSequential(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
maxConcurrency),
flux.flatMapSequential(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
maxConcurrency,
prefetch),
flux.flatMapSequentialDelayError(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x))),
maxConcurrency,
prefetch),
flux.switchMap(
x ->
Refaster.anyOf(
Mono.justOrEmpty(transformation(x)),
Mono.fromSupplier(() -> transformation(x)))));
}

@AfterTemplate
Flux<S> after(Flux<T> flux) {
return flux.mapNotNull(x -> transformation(x));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,71 @@ Flux<Integer> testFluxConcatMapIterableWithPrefetch() {
}

Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMapMany(s -> Mono.just(s + s));
return Mono.just("foo").flatMapMany(s -> Mono.fromSupplier(() -> s + s));
}

ImmutableSet<Mono<String>> testMonoMap() {
return ImmutableSet.of(
Mono.just("foo").flatMap(s -> Mono.just(s)),
Mono.just("bar").flatMap(s -> Mono.just(s.substring(1))));
}

ImmutableSet<Flux<Integer>> testFluxMap() {
return ImmutableSet.of(
Flux.just(1).concatMap(n -> Mono.just(n)),
Flux.just(1).concatMap(n -> Flux.just(n * 2)),
Flux.just(1).concatMap(n -> Mono.just(n), 3),
Flux.just(1).concatMap(n -> Flux.just(n * 2), 3),
Flux.just(1).concatMapDelayError(n -> Mono.just(n)),
Flux.just(1).concatMapDelayError(n -> Flux.just(n * 2)),
Flux.just(1).concatMapDelayError(n -> Mono.just(n), 3),
Flux.just(1).concatMapDelayError(n -> Flux.just(n * 2), 3),
Flux.just(1).flatMap(n -> Mono.just(n), 3),
Flux.just(1).flatMap(n -> Flux.just(n * 2), 3),
Flux.just(1).flatMap(n -> Mono.just(n), 3, 4),
Flux.just(1).flatMap(n -> Flux.just(n * 2), 3, 4),
Flux.just(1).flatMapDelayError(n -> Mono.just(n), 3, 4),
Flux.just(1).flatMapDelayError(n -> Flux.just(n * 2), 3, 4),
Flux.just(1).flatMapSequential(n -> Mono.just(n), 3),
Flux.just(1).flatMapSequential(n -> Flux.just(n * 2), 3),
Flux.just(1).flatMapSequential(n -> Mono.just(n), 3, 4),
Flux.just(1).flatMapSequential(n -> Flux.just(n * 2), 3, 4),
Flux.just(1).flatMapSequentialDelayError(n -> Mono.just(n), 3, 4),
Flux.just(1).flatMapSequentialDelayError(n -> Flux.just(n * 2), 3, 4),
Flux.just(1).switchMap(n -> Mono.just(n)),
Flux.just(1).switchMap(n -> Flux.just(n * 2)));
}

ImmutableSet<Mono<String>> testMonoMapNotNull() {
return ImmutableSet.of(
Mono.just("foo").flatMap(s -> Mono.justOrEmpty(s)),
Mono.just("bar").flatMap(s -> Mono.fromSupplier(() -> s.substring(1))));
}

ImmutableSet<Flux<Integer>> testFluxMapNotNull() {
return ImmutableSet.of(
Flux.just(1).concatMap(n -> Mono.justOrEmpty(n)),
Flux.just(1).concatMap(n -> Mono.fromSupplier(() -> n * 2)),
Flux.just(1).concatMap(n -> Mono.justOrEmpty(n), 3),
Flux.just(1).concatMap(n -> Mono.fromSupplier(() -> n * 2), 3),
Flux.just(1).concatMapDelayError(n -> Mono.justOrEmpty(n)),
Flux.just(1).concatMapDelayError(n -> Mono.fromSupplier(() -> n * 2)),
Flux.just(1).concatMapDelayError(n -> Mono.justOrEmpty(n), 3),
Flux.just(1).concatMapDelayError(n -> Mono.fromSupplier(() -> n * 2), 3),
Flux.just(1).flatMap(n -> Mono.justOrEmpty(n), 3),
Flux.just(1).flatMap(n -> Mono.fromSupplier(() -> n * 2), 3),
Flux.just(1).flatMap(n -> Mono.justOrEmpty(n), 3, 4),
Flux.just(1).flatMap(n -> Mono.fromSupplier(() -> n * 2), 3, 4),
Flux.just(1).flatMapDelayError(n -> Mono.justOrEmpty(n), 3, 4),
Flux.just(1).flatMapDelayError(n -> Mono.fromSupplier(() -> n * 2), 3, 4),
Flux.just(1).flatMapSequential(n -> Mono.justOrEmpty(n), 3),
Flux.just(1).flatMapSequential(n -> Mono.fromSupplier(() -> n * 2), 3),
Flux.just(1).flatMapSequential(n -> Mono.justOrEmpty(n), 3, 4),
Flux.just(1).flatMapSequential(n -> Mono.fromSupplier(() -> n * 2), 3, 4),
Flux.just(1).flatMapSequentialDelayError(n -> Mono.justOrEmpty(n), 3, 4),
Flux.just(1).flatMapSequentialDelayError(n -> Mono.fromSupplier(() -> n * 2), 3, 4),
Flux.just(1).switchMap(n -> Mono.justOrEmpty(n)),
Flux.just(1).switchMap(n -> Mono.fromSupplier(() -> n * 2)));
}

Flux<String> testMonoFlux() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,68 @@ Flux<Integer> testFluxConcatMapIterableWithPrefetch() {
}

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

ImmutableSet<Mono<String>> testMonoMap() {
return ImmutableSet.of(Mono.just("foo").map(s -> s), Mono.just("bar").map(s -> s.substring(1)));
}

ImmutableSet<Flux<Integer>> testFluxMap() {
return ImmutableSet.of(
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2),
Flux.just(1).map(n -> n),
Flux.just(1).map(n -> n * 2));
}

ImmutableSet<Mono<String>> testMonoMapNotNull() {
return ImmutableSet.of(
Mono.just("foo").mapNotNull(s -> s), Mono.just("bar").mapNotNull(s -> s.substring(1)));
}

ImmutableSet<Flux<Integer>> testFluxMapNotNull() {
return ImmutableSet.of(
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2),
Flux.just(1).mapNotNull(n -> n),
Flux.just(1).mapNotNull(n -> n * 2));
}

Flux<String> testMonoFlux() {
Expand Down

0 comments on commit afb2a28

Please sign in to comment.