Class GroovyPromise<T>
- Type Parameters:
T- the result type
- All Implemented Interfaces:
Awaitable<T>
Awaitable implementation backed by a CompletableFuture.
This is the concrete type returned by async methods. It delegates
all operations to an underlying CompletableFuture while keeping the
public API limited to the Awaitable contract, thereby decoupling
user code from JDK-specific async APIs.
This class is an internal implementation detail and should not be referenced
directly by user code. Use the Awaitable interface instead.
- Since:
- 6.0.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionGroovyPromise(CompletableFuture<T> future) Creates a newGroovyPromisewrapping the givenCompletableFuture. -
Method Summary
Modifier and TypeMethodDescriptionbooleancancel()Attempts to cancel this computation.exceptionally(Function<Throwable, ? extends T> fn) Returns a newAwaitablethat, if this one completes exceptionally, applies the given function to the exception to produce a recovery value.get()Blocks until the computation completes and returns the result.Blocks until the computation completes or the timeout expires.booleanReturnstrueif the computation was cancelled before completing normally.booleanReturnstrueif this computation completed exceptionally (including cancellation).booleanisDone()Returnstrueif the computation has completed (normally, exceptionally, or via cancellation).static <T> GroovyPromise<T>of(CompletableFuture<T> future) Creates aGroovyPromisewrapping the givenCompletableFuture.<U> Awaitable<U>Returns a newAwaitablewhose result is obtained by applying the given function to this awaitable's result when it completes.<U> Awaitable<U>thenCompose(Function<? super T, ? extends Awaitable<U>> fn) Returns a newAwaitableproduced by applying the given async function to this awaitable's result, flattening the nestedAwaitable.Converts thisAwaitableto a JDKCompletableFuturefor interoperability with APIs that require it.toString()Returns a human-readable representation showing the promise state:GroovyPromise{pending},GroovyPromise{completed}, orGroovyPromise{failed}.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface groovy.concurrent.Awaitable
completeOnTimeout, completeOnTimeoutMillis, handle, orTimeout, orTimeoutMillis, thenAccept, whenComplete
-
Constructor Details
-
GroovyPromise
Creates a newGroovyPromisewrapping the givenCompletableFuture.- Parameters:
future- the backing future; must not benull- Throws:
NullPointerException- iffutureisnull
-
-
Method Details
-
of
Creates aGroovyPromisewrapping the givenCompletableFuture.This is a convenience factory that delegates to
GroovyPromise(CompletableFuture).- Type Parameters:
T- the result type- Parameters:
future- the backing future; must not benull- Returns:
- a new
GroovyPromisewrappingfuture - Throws:
NullPointerException- iffutureisnull
-
get
Blocks until the computation completes and returns the result.Includes a synchronous completion fast-path: if the underlying
CompletableFutureis already done, the result is extracted viaCompletableFuture.join()which avoids the full park/unpark machinery ofCompletableFuture.get(). This optimisation provides a synchronous completion fast-path and eliminates unnecessary thread state transitions on the hot path where async operations complete before being awaited.If the future was cancelled, the original
CancellationExceptionis unwrapped from the JDK 23+ wrapper for cross-version consistency.- Specified by:
getin interfaceAwaitable<T>- Returns:
- the computed result
- Throws:
InterruptedException- if the calling thread is interrupted while waitingExecutionException- if the computation completed exceptionally
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Blocks until the computation completes or the timeout expires.Includes a synchronous completion fast-path for already-done futures, consistent with the zero-argument
get()overload. Unwraps JDK 23+CancellationExceptionwrappers for consistency.- Specified by:
getin interfaceAwaitable<T>- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the computed result
- Throws:
InterruptedException- if the calling thread is interrupted while waitingExecutionException- if the computation completed exceptionallyTimeoutException- if the wait timed out
-
isDone
public boolean isDone()Returnstrueif the computation has completed (normally, exceptionally, or via cancellation). -
cancel
public boolean cancel()Attempts to cancel this computation. Delegates toCompletableFuture.cancel(true).Note:
CompletableFuturecancellation sets the future's state to cancelled but does not reliably interrupt the underlying thread. Async work already in progress may continue running in the background. For cooperative cancellation, checkThread.isInterrupted()in long-running async bodies. -
isCancelled
public boolean isCancelled()Returnstrueif the computation was cancelled before completing normally.- Specified by:
isCancelledin interfaceAwaitable<T>- Returns:
trueif cancelled
-
isCompletedExceptionally
public boolean isCompletedExceptionally()Returnstrueif this computation completed exceptionally (including cancellation).- Specified by:
isCompletedExceptionallyin interfaceAwaitable<T>- Returns:
trueif completed with an error or cancellation
-
then
Returns a newAwaitablewhose result is obtained by applying the given function to this awaitable's result when it completes.Returns a new
GroovyPromisewhose result is obtained by applying the given function to this promise's result. The currentAsyncContextsnapshot is captured when the continuation is registered and restored when it executes. -
thenCompose
Returns a newAwaitableproduced by applying the given async function to this awaitable's result, flattening the nestedAwaitable. This is the monadicflatMapoperation for awaitables.Returns a new
GroovyPromisethat is the result of composing this promise with the async function, enabling flat-mapping of awaitables. The currentAsyncContextsnapshot is captured when the continuation is registered and restored when it executes.- Specified by:
thenComposein interfaceAwaitable<T>- Type Parameters:
U- the type of the inner awaitable's result- Parameters:
fn- the async mapping function that returns anAwaitable- Returns:
- a new awaitable holding the inner result
-
exceptionally
Returns a newAwaitablethat, if this one completes exceptionally, applies the given function to the exception to produce a recovery value. The throwable passed to the function is deeply unwrapped to strip JDK wrapper layers.Returns a new
GroovyPromisethat handles exceptions thrown by this promise. The throwable passed to the handler is deeply unwrapped to strip JDK wrapper layers (CompletionException,ExecutionException). The handler runs with theAsyncContextsnapshot that was active when the recovery continuation was registered.- Specified by:
exceptionallyin interfaceAwaitable<T>- Parameters:
fn- the recovery function- Returns:
- a new awaitable that recovers from failures
-
toCompletableFuture
Converts thisAwaitableto a JDKCompletableFuturefor interoperability with APIs that require it.Returns the underlying
CompletableFuturefor interop with JDK APIs.- Specified by:
toCompletableFuturein interfaceAwaitable<T>- Returns:
- a
CompletableFuturerepresenting this computation
-
toString
Returns a human-readable representation showing the promise state:GroovyPromise{pending},GroovyPromise{completed}, orGroovyPromise{failed}.
-