Skip to content

Commit

Permalink
Introduce {Mono,Flux}OfType Refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
werli authored and rickie committed Oct 9, 2023
1 parent 9ada078 commit aeb79f9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,40 @@ Flux<S> after(Flux<T> flux) {
}
}

/** Prefer {@link Mono#ofType(Class)} over more contrived alternatives. */
static final class MonoOfType<T, S> {
@BeforeTemplate
Mono<S> before(Mono<T> mono) {
return mono.filter(
Refaster.anyOf(
Refaster.<S>clazz()::isInstance,
x -> Refaster.<S>clazz().isAssignableFrom(x.getClass())))
.cast(Refaster.<S>clazz());
}

@AfterTemplate
Mono<S> after(Mono<T> mono) {
return mono.ofType(Refaster.<S>clazz());
}
}

/** Prefer {@link Flux#ofType(Class)} over more contrived alternatives. */
static final class FluxOfType<T, S> {
@BeforeTemplate
Flux<S> before(Flux<T> flux) {
return flux.filter(
Refaster.anyOf(
Refaster.<S>clazz()::isInstance,
x -> Refaster.<S>clazz().isAssignableFrom(x.getClass())))
.cast(Refaster.<S>clazz());
}

@AfterTemplate
Flux<S> after(Flux<T> flux) {
return flux.ofType(Refaster.<S>clazz());
}
}

/** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */
static final class MonoFlatMap<S, T> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,18 @@ Flux<Number> testFluxCast() {
return Flux.just(1).map(Number.class::cast);
}

ImmutableSet<Mono<Number>> testMonoOfType() {
return ImmutableSet.of(
Mono.just(1).filter(Number.class::isInstance).cast(Number.class),
Mono.just(2).filter(n -> Number.class.isAssignableFrom(n.getClass())).cast(Number.class));
}

ImmutableSet<Flux<Number>> testFluxOfType() {
return ImmutableSet.of(
Flux.just(1).filter(Number.class::isInstance).cast(Number.class),
Flux.just(2).filter(n -> Number.class.isAssignableFrom(n.getClass())).cast(Number.class));
}

Mono<String> testMonoFlatMap() {
return Mono.just("foo").map(Mono::just).flatMap(identity());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ Flux<Number> testFluxCast() {
return Flux.just(1).cast(Number.class);
}

ImmutableSet<Mono<Number>> testMonoOfType() {
return ImmutableSet.of(Mono.just(1).ofType(Number.class), Mono.just(2).ofType(Number.class));
}

ImmutableSet<Flux<Number>> testFluxOfType() {
return ImmutableSet.of(Flux.just(1).ofType(Number.class), Flux.just(2).ofType(Number.class));
}

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

0 comments on commit aeb79f9

Please sign in to comment.