Skip to content

Commit

Permalink
Introduce Mono{Empty,Just,JustOrEmpty} Refaster rules (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
werli authored Dec 6, 2022
1 parent f585306 commit 1afce12
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.jspecify.nullness.Nullable;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -59,6 +60,45 @@ Mono<T> after(Supplier<? extends T> supplier) {
}
}

/** Prefer {@link Mono#empty()} over more contrived alternatives. */
static final class MonoEmpty<T> {
@BeforeTemplate
Mono<T> before() {
return Refaster.anyOf(Mono.justOrEmpty(null), Mono.justOrEmpty(Optional.empty()));
}

@AfterTemplate
Mono<T> after() {
return Mono.empty();
}
}

/** Prefer {@link Mono#just(Object)} over more contrived alternatives. */
static final class MonoJust<T> {
@BeforeTemplate
Mono<T> before(T value) {
return Mono.justOrEmpty(Optional.of(value));
}

@AfterTemplate
Mono<T> after(T value) {
return Mono.just(value);
}
}

/** Prefer {@link Mono#justOrEmpty(Object)} over more contrived alternatives. */
static final class MonoJustOrEmpty<@Nullable T> {
@BeforeTemplate
Mono<T> before(T value) {
return Mono.justOrEmpty(Optional.ofNullable(value));
}

@AfterTemplate
Mono<T> after(T value) {
return Mono.justOrEmpty(value);
}
}

/** Prefer {@link Mono#justOrEmpty(Optional)} over more verbose alternatives. */
// XXX: If `optional` is a constant and effectively-final expression then the `Mono.defer` can be
// dropped. Should look into Refaster support for identifying this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ ImmutableSet<Mono<?>> testMonoFromSupplier() {
Mono.fromCallable(this::toString));
}

ImmutableSet<Mono<String>> testMonoEmpty() {
return ImmutableSet.of(Mono.justOrEmpty(null), Mono.justOrEmpty(Optional.empty()));
}

Mono<Integer> testMonoJust() {
return Mono.justOrEmpty(Optional.of(1));
}

Mono<Integer> testMonoJustOrEmpty() {
return Mono.justOrEmpty(Optional.ofNullable(1));
}

ImmutableSet<Mono<Integer>> testMonoFromOptional() {
return ImmutableSet.of(
Mono.fromCallable(() -> Optional.of(1).orElse(null)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ ImmutableSet<Mono<?>> testMonoFromSupplier() {
Mono.fromSupplier(this::toString));
}

ImmutableSet<Mono<String>> testMonoEmpty() {
return ImmutableSet.of(Mono.empty(), Mono.empty());
}

Mono<Integer> testMonoJust() {
return Mono.just(1);
}

Mono<Integer> testMonoJustOrEmpty() {
return Mono.justOrEmpty(1);
}

ImmutableSet<Mono<Integer>> testMonoFromOptional() {
return ImmutableSet.of(
Mono.defer(() -> Mono.justOrEmpty(Optional.of(1))),
Expand Down

0 comments on commit 1afce12

Please sign in to comment.