Skip to content

Commit

Permalink
Introduce MonoJustOrEmptyOptional Refaster rule (#563)
Browse files Browse the repository at this point in the history
While there, rename two other rules.
  • Loading branch information
mohamedsamehsalah authored Apr 5, 2023
1 parent 64f9d6b commit 0fefb69
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Mono<T> after(T value) {
}

/** Prefer {@link Mono#justOrEmpty(Object)} over more contrived alternatives. */
static final class MonoJustOrEmpty<@Nullable T> {
static final class MonoJustOrEmptyObject<@Nullable T> {
@BeforeTemplate
Mono<T> before(T value) {
return Mono.justOrEmpty(Optional.ofNullable(value));
Expand All @@ -107,9 +107,25 @@ Mono<T> after(T value) {
}

/** Prefer {@link Mono#justOrEmpty(Optional)} over more verbose alternatives. */
static final class MonoJustOrEmptyOptional<T> {
@BeforeTemplate
Mono<T> before(Optional<T> optional) {
return Mono.just(optional).filter(Optional::isPresent).map(Optional::orElseThrow);
}

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

/**
* Prefer {@link Mono#defer(Supplier) deferring} {@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.
static final class MonoFromOptional<T> {
static final class MonoDeferMonoJustOrEmpty<T> {
@BeforeTemplate
@SuppressWarnings(
"MonoFromSupplier" /* `optional` may match a checked exception-throwing expression. */)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ Mono<Integer> testMonoJust() {
return Mono.justOrEmpty(Optional.of(1));
}

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

ImmutableSet<Mono<Integer>> testMonoFromOptional() {
Mono<Integer> testMonoJustOrEmptyOptional() {
return Mono.just(Optional.of(1)).filter(Optional::isPresent).map(Optional::orElseThrow);
}

ImmutableSet<Mono<Integer>> testMonoDeferMonoJustOrEmpty() {
return ImmutableSet.of(
Mono.fromCallable(() -> Optional.of(1).orElse(null)),
Mono.fromSupplier(() -> Optional.of(2).orElse(null)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ Mono<Integer> testMonoJust() {
return Mono.just(1);
}

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

ImmutableSet<Mono<Integer>> testMonoFromOptional() {
Mono<Integer> testMonoJustOrEmptyOptional() {
return Mono.justOrEmpty(Optional.of(1));
}

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

0 comments on commit 0fefb69

Please sign in to comment.