Class HandlerFutures
java.lang.Object
dev.tachyonmcp.api.server.features.HandlerFutures
Static utilities for common CompletionStage patterns.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidstatic <R> CompletionStage<R> completedOrFailed(Callable<? extends R> handler) Adapts a synchronous handler body into aCompletionStage: a completed stage on success, a failed one carrying the thrown exception otherwise.static <T,R> CompletionStage <R> completeOn(CompletionStage<T> stage, Executor executor, BiFunction<? super T, Throwable, ? extends R> fn) Appliesfnto the outcome ofstage.static <R> CompletionStage<Object> invokeAndMap(String nullStageMessage, Callable<? extends @Nullable CompletionStage<? extends R>> invocation, Executor executor, BiFunction<? super R, @Nullable Throwable, @Nullable Object> resultMapper) Invokes a handler, guards against a synchronous throw or anullreturned stage, and maps the outcome viacompleteOn(java.util.concurrent.CompletionStage<T>, java.util.concurrent.Executor, java.util.function.BiFunction<? super T, java.lang.Throwable, ? extends R>).static <T> TjoinInterruptibly(CompletionStage<T> stage) Joins a CompletionStage via blocking.get(), restoring interrupt and unwrapping ExecutionException.static ThrowableUnwraps a single level ofCompletionException, returning the original cause when present.
-
Method Details
-
assumeVirtualThread
public static void assumeVirtualThread() -
joinInterruptibly
Joins a CompletionStage via blocking.get(), restoring interrupt and unwrapping ExecutionException. For use on virtual threads where blocking is expected.- Type Parameters:
T- the result type- Parameters:
stage- the stage to join- Returns:
- the result of the stage
- Throws:
Exception- if the stage completed exceptionally
-
unwrap
Unwraps a single level ofCompletionException, returning the original cause when present. Mirrors the unwrap aCompletionStagecallback receives after an async hop.- Parameters:
throwable- the throwable to unwrap- Returns:
- the original cause, or
throwableitself if not aCompletionException
-
completeOn
public static <T,R> CompletionStage<R> completeOn(CompletionStage<T> stage, Executor executor, BiFunction<? super T, Throwable, ? extends R> fn) Appliesfnto the outcome ofstage. Runs inline on the calling thread when the stage is already complete (the common case: sync handlers, already-resolved async ones) to avoid an unconditional executor hop; otherwise schedulesfnonexecutorso a handler completing from a foreign thread doesn't leak that thread into response mapping.- Type Parameters:
T- the input result typeR- the output result type- Parameters:
stage- the stage whose outcome to mapexecutor- the executor for async completionfn- the mapping function- Returns:
- a new stage with the mapped result
-
completedOrFailed
Adapts a synchronous handler body into aCompletionStage: a completed stage on success, a failed one carrying the thrown exception otherwise. Shared by the sync-handler internal adapters from synchronous feature functions.- Type Parameters:
R- the result type- Parameters:
handler- the synchronous handler body- Returns:
- a completed or failed stage
-
invokeAndMap
public static <R> CompletionStage<Object> invokeAndMap(String nullStageMessage, Callable<? extends @Nullable CompletionStage<? extends R>> invocation, Executor executor, BiFunction<? super R, @Nullable Throwable, @Nullable Object> resultMapper) Invokes a handler, guards against a synchronous throw or anullreturned stage, and maps the outcome viacompleteOn(java.util.concurrent.CompletionStage<T>, java.util.concurrent.Executor, java.util.function.BiFunction<? super T, java.lang.Throwable, ? extends R>). Shared registry-dispatch skeleton: obtain the handler's stage safely, then compose the result without blocking. The mapper receives the failure cause alreadyunwrapped(ornullon success).- Type Parameters:
R- the handler result type- Parameters:
nullStageMessage- message for the NPE if invocation returns nullinvocation- the handler invocationexecutor- the executor for async completionresultMapper- maps result or failure to the dispatch outcome- Returns:
- a stage with the mapped result
-