Skip to content

Commit

Permalink
Introduce {Mono,Flux}OfType Refaster rules (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
werli authored Oct 9, 2023
1 parent 9ada078 commit 583e51c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,32 @@ 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, Class<S> clazz) {
return mono.filter(clazz::isInstance).cast(clazz);
}

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

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

@AfterTemplate
Flux<S> after(Flux<T> flux, Class<S> clazz) {
return flux.ofType(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,14 @@ Flux<Number> testFluxCast() {
return Flux.just(1).map(Number.class::cast);
}

Mono<Number> testMonoOfType() {
return Mono.just(1).filter(Number.class::isInstance).cast(Number.class);
}

Flux<Number> testFluxOfType() {
return Flux.just(1).filter(Number.class::isInstance).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);
}

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

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

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

0 comments on commit 583e51c

Please sign in to comment.