Skip to content

Commit

Permalink
Introduce MonoFromFutureSupplier{,Boolean} Refaster rules (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
Venorcis authored Jul 20, 2024
1 parent 2b6b8de commit 4b1c892
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
Expand Down Expand Up @@ -1904,4 +1905,41 @@ Duration after(StepVerifier.LastStep step, Duration duration) {
return step.verifyTimeout(duration);
}
}

/**
* Prefer {@link Mono#fromFuture(Supplier)} over {@link Mono#fromFuture(CompletableFuture)}, as
* the former may defer initiation of the asynchornous computation until subscription.
*/
static final class MonoFromFutureSupplier<T> {
// XXX: Constrain the `future` parameter using `@NotMatches(IsIdentityOperation.class)` once
// `IsIdentityOperation` no longer matches nullary method invocations.
@BeforeTemplate
Mono<T> before(CompletableFuture<T> future) {
return Mono.fromFuture(future);
}

@AfterTemplate
Mono<T> after(CompletableFuture<T> future) {
return Mono.fromFuture(() -> future);
}
}

/**
* Prefer {@link Mono#fromFuture(Supplier, boolean)} over {@link
* Mono#fromFuture(CompletableFuture, boolean)}, as the former may defer initiation of the
* asynchornous computation until subscription.
*/
static final class MonoFromFutureSupplierBoolean<T> {
// XXX: Constrain the `future` parameter using `@NotMatches(IsIdentityOperation.class)` once
// `IsIdentityOperation` no longer matches nullary method invocations.
@BeforeTemplate
Mono<T> before(CompletableFuture<T> future, boolean suppressCancel) {
return Mono.fromFuture(future, suppressCancel);
}

@AfterTemplate
Mono<T> after(CompletableFuture<T> future, boolean suppressCancel) {
return Mono.fromFuture(() -> future, suppressCancel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -651,4 +652,12 @@ Duration testStepVerifierLastStepVerifyErrorMessage() {
Duration testStepVerifierLastStepVerifyTimeout() {
return Mono.empty().as(StepVerifier::create).expectTimeout(Duration.ZERO).verify();
}

Mono<Void> testMonoFromFutureSupplier() {
return Mono.fromFuture(CompletableFuture.completedFuture(null));
}

Mono<Void> testMonoFromFutureSupplierBoolean() {
return Mono.fromFuture(CompletableFuture.completedFuture(null), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -632,4 +633,12 @@ Duration testStepVerifierLastStepVerifyErrorMessage() {
Duration testStepVerifierLastStepVerifyTimeout() {
return Mono.empty().as(StepVerifier::create).verifyTimeout(Duration.ZERO);
}

Mono<Void> testMonoFromFutureSupplier() {
return Mono.fromFuture(() -> CompletableFuture.completedFuture(null));
}

Mono<Void> testMonoFromFutureSupplierBoolean() {
return Mono.fromFuture(() -> CompletableFuture.completedFuture(null), true);
}
}

0 comments on commit 4b1c892

Please sign in to comment.