public class GroovyPromise<T>
extends Object
implements Awaitable
Default 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.
T - the result type| Constructor and description |
|---|
GroovyPromise(CompletableFuture<T> future)Creates a new GroovyPromise wrapping the given CompletableFuture. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public boolean |
cancel()Attempts to cancel this computation. |
|
public Awaitable<T> |
exceptionally(Function<Throwable, ? extends T> fn)Returns a new Awaitable that, 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.
|
|
public T |
get()Blocks until the computation completes and returns the result.
|
|
public T |
get(long timeout, TimeUnit unit)Blocks until the computation completes or the timeout expires.
|
|
public boolean |
isCancelled()Returns true if the computation was cancelled before completing normally.
|
|
public boolean |
isCompletedExceptionally()Returns true if this computation completed exceptionally
(including cancellation).
|
|
public boolean |
isDone()Returns true if the computation has completed (normally,
exceptionally, or via cancellation).
|
<T> |
public static GroovyPromise<T> |
of(CompletableFuture<T> future)Creates a GroovyPromise wrapping the given CompletableFuture. |
<U> |
public Awaitable<U> |
then(Function<? super T, ? extends U> fn)Returns a new Awaitable whose result is obtained by applying the
given function to this awaitable's result when it completes.
|
<U> |
public Awaitable<U> |
thenCompose(Function<? super T, ? extends Awaitable<U>> fn)Returns a new Awaitable produced by applying the given async
function to this awaitable's result, flattening the nested Awaitable.
This is the monadic flatMap operation for awaitables.
|
|
public CompletableFuture<T> |
toCompletableFuture()Converts this Awaitable to a JDK CompletableFuture
for interoperability with APIs that require it.
|
|
public String |
toString()Returns a human-readable representation showing the promise state: GroovyPromise{pending}, GroovyPromise{completed}, or
GroovyPromise{failed}. |
Creates a new GroovyPromise wrapping the given CompletableFuture.
future is nullfuture - the backing future; must not be nullAttempts to cancel this computation. Delegates to CompletableFuture#cancel(boolean) CompletableFuture.cancel(true).
Note: CompletableFuture cancellation 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, check Thread.isInterrupted in
long-running async bodies.
true if the future was successfully cancelled Returns a new Awaitable that, 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.
fn - the recovery function
Returns a new GroovyPromise that 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 the AsyncContext snapshot that was active
when the recovery continuation was registered.
Blocks until the computation completes and returns the result.
Includes a synchronous completion fast-path: if the underlying CompletableFuture is already done, the result is extracted via CompletableFuture.join which avoids the full park/unpark machinery of CompletableFuture.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 CancellationException is unwrapped from the JDK 23+ wrapper for cross-version consistency.
Blocks until the computation completes or the timeout expires.
timeout - the maximum time to waitunit - the time unit of the timeout argumentIncludes a synchronous completion fast-path for already-done futures, consistent with the zero-argument get() overload. Unwraps JDK 23+ CancellationException wrappers for consistency.
Returns true if the computation was cancelled before completing normally.
true if cancelled Returns true if this computation completed exceptionally
(including cancellation).
true if completed with an error or cancellation Returns true if the computation has completed (normally,
exceptionally, or via cancellation).
true if complete Creates a GroovyPromise wrapping the given CompletableFuture.
This is a convenience factory that delegates to GroovyPromise(CompletableFuture).
future is nullfuture - the backing future; must not be nullT - the result typeGroovyPromise wrapping future Returns a new Awaitable whose result is obtained by applying the
given function to this awaitable's result when it completes.
fn - the mapping functionU - the type of the mapped result
Returns a new GroovyPromise whose result is obtained by applying
the given function to this promise's result. The current
AsyncContext snapshot is captured when the continuation is
registered and restored when it executes.
Returns a new Awaitable produced by applying the given async
function to this awaitable's result, flattening the nested Awaitable.
This is the monadic flatMap operation for awaitables.
fn - the async mapping function that returns an AwaitableU - the type of the inner awaitable's result
Returns a new GroovyPromise that is the result of composing this
promise with the async function, enabling flat-mapping of awaitables.
The current AsyncContext snapshot is captured when the
continuation is registered and restored when it executes.
Converts this Awaitable to a JDK CompletableFuture
for interoperability with APIs that require it.
CompletableFuture representing this computationReturns the underlying CompletableFuture for interop with JDK APIs.
Returns a human-readable representation showing the promise state:
GroovyPromise{pending}, GroovyPromise{completed}, or
GroovyPromise{failed}.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.