V - The type of the task's result, or Void if the task does
not return anything (i.e. it only has side-effects).E - The type of the exception thrown by the task if it fails, or
NeverThrowsException if the task cannot fail.public class PromiseImpl<V,E extends Exception> extends Object implements Promise<V,E>, ResultHandler<V>, ExceptionHandler<E>, RuntimeExceptionHandler
Promise which can be used as is, or as the basis
for more complex asynchronous behavior. A PromiseImpl must be
completed by invoking one of:
handleResult(V) - marks the promise as having succeeded with the
provide result
handleException(E) - marks the promise as having failed with the
provided exception
cancel(boolean) - requests cancellation of the asynchronous task
represented by the promise. Cancellation is only supported if the
tryCancel(boolean) is overridden and returns an exception.
| Modifier | Constructor and Description |
|---|---|
protected |
PromiseImpl()
Creates a new pending
Promise implementation. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel the asynchronous task associated with this
Promise. |
static <V,E extends Exception> |
create()
Creates a new pending
Promise implementation. |
V |
get()
Waits if necessary for this
Promise to complete, and then returns
the result if it completed successfully, or throws an
ExecutionException containing the cause of the failure. |
V |
get(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for this
Promise to
complete, and then returns the result if it completed successfully, or
throws an ExecutionException containing the cause of the failure. |
V |
getOrThrow()
Waits if necessary for this
Promise to complete, and then returns
the result if it completed successfully, or throws an exception
representing the cause of the failure. |
V |
getOrThrow(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for this
Promise to
complete, and then returns the result if it completed successfully, or
throws an exception representing the cause of the failure. |
V |
getOrThrowUninterruptibly()
Waits if necessary for this
Promise to complete, and then returns
the result if it completed successfully, or throws an exception
representing the cause of the failure. |
V |
getOrThrowUninterruptibly(long timeout,
TimeUnit unit)
Waits if necessary for at most the given time for this
Promise to
complete, and then returns the result if it completed successfully, or
throws an exception representing the cause of the failure. |
void |
handleException(E exception)
Signals that the asynchronous task represented by this promise has
failed.
|
void |
handleResult(V result)
Signals that the asynchronous task represented by this promise has
succeeded.
|
void |
handleRuntimeException(RuntimeException exception)
Invoked when the asynchronous task has failed with a runtime exception.
|
boolean |
isCancelled()
Returns
true if this Promise was cancelled before it
completed normally. |
boolean |
isDone()
Returns
true if this Promise has completed. |
<VOUT> Promise<VOUT,E> |
then(Function<? super V,VOUT,E> onResult)
Submits the provided function for execution once this
Promise has
completed with a result, and returns a new Promise representing
the outcome of the function. |
<VOUT,EOUT extends Exception> |
then(Function<? super V,VOUT,EOUT> onResult,
Function<? super E,VOUT,EOUT> onException)
Submits the provided functions for execution once this
Promise
has completed (with a result or an exception), and returns a new
Promise representing the outcome of the invoked function. |
<VOUT,EOUT extends Exception> |
then(Function<? super V,VOUT,EOUT> onResult,
Function<? super E,VOUT,EOUT> onException,
Function<? super RuntimeException,VOUT,EOUT> onRuntimeException)
Submits the provided functions for execution once this
Promise
has completed (with a result or an exception or a RuntimeException), and returns a new
Promise representing the outcome of the invoked function. |
Promise<V,E> |
thenAlways(Runnable always)
Submits the provided runnable for execution once this
Promise has
completed, and regardless of whether it has a result or an exception. |
<VOUT> Promise<VOUT,E> |
thenAsync(AsyncFunction<? super V,VOUT,E> onResult)
Submits the provided asynchronous function for execution once this
Promise has completed with a result, and returns a new
Promise representing the outcome of the function. |
<VOUT,EOUT extends Exception> |
thenAsync(AsyncFunction<? super V,VOUT,EOUT> onResult,
AsyncFunction<? super E,VOUT,EOUT> onException)
Submits the provided asynchronous functions for execution once this
Promise has completed, and returns a new Promise
representing the outcome of the invoked function. |
<VOUT,EOUT extends Exception> |
thenAsync(AsyncFunction<? super V,VOUT,EOUT> onResult,
AsyncFunction<? super E,VOUT,EOUT> onException,
AsyncFunction<? super RuntimeException,VOUT,EOUT> onRuntimeException)
Submits the provided asynchronous functions for execution once this
Promise has completed, and returns a new Promise
representing the outcome of the invoked function. |
<EOUT extends Exception> |
thenCatch(Function<? super E,V,EOUT> onException)
Submits the provided function for execution once this
Promise has
not completed with a result (has completed with an exception), and returns
a new Promise representing the outcome of the function. |
<EOUT extends Exception> |
thenCatchAsync(AsyncFunction<? super E,V,EOUT> onException)
Submits the provided asynchronous function for execution once this
Promise has completed with an exception, and returns a new
Promise representing the outcome of the function. |
Promise<V,E> |
thenCatchRuntimeException(Function<? super RuntimeException,V,E> onRuntimeException)
Submits the provided function for execution once this
Promise has
not completed with a result nor with an exception but with a RuntimeException, and returns
a new Promise representing the outcome of the function. |
Promise<V,E> |
thenCatchRuntimeExceptionAsync(AsyncFunction<? super RuntimeException,V,E> onRuntimeException)
Submits the provided asynchronous function for execution once this
Promise has completed with a RuntimeException, and returns a new
Promise representing the outcome of the function. |
Promise<V,E> |
thenFinally(Runnable onFinally)
Submits the provided runnable for execution once this
Promise has
completed, and regardless of whether of its outcome. |
Promise<V,E> |
thenOnException(ExceptionHandler<? super E> onException)
Registers the provided completion handler for notification if this
Promise cannot be completed due to an exception. |
Promise<V,E> |
thenOnResult(ResultHandler<? super V> onResult)
Registers the provided completion handler for notification once this
Promise has completed with a result. |
Promise<V,E> |
thenOnResultOrException(ResultHandler<? super V> onResult,
ExceptionHandler<? super E> onException)
Registers the provided completion handlers for notification once this
Promise has completed (with a result or an exception). |
Promise<V,E> |
thenOnResultOrException(Runnable onResultOrException)
Submits the provided runnable for execution once this
Promise has
completed, and regardless of whether it has a result or an exception. |
Promise<V,E> |
thenOnRuntimeException(RuntimeExceptionHandler onRuntimeException)
Registers the provided completion handler for notification if this
Promise cannot be completed due to an runtime exception. |
protected E |
tryCancel(boolean mayInterruptIfRunning)
Invoked when the client attempts to cancel the asynchronous task
represented by this promise.
|
boolean |
tryHandleException(E exception)
Attempts to signal that the asynchronous task represented by this promise
has failed.
|
boolean |
tryHandleResult(V result)
Attempts to signal that the asynchronous task represented by this promise
has succeeded.
|
protected PromiseImpl()
Promise implementation. This constructor is
protected to allow for sub-classing.public static <V,E extends Exception> PromiseImpl<V,E> create()
Promise implementation.V - The type of the task's result, or Void if the task
does not return anything (i.e. it only has side-effects).E - The type of the exception thrown by the task if it fails, or
NeverThrowsException if the task cannot fail.Promise implementation.public final boolean cancel(boolean mayInterruptIfRunning)
PromisePromise. Cancellation will fail if this Promise has
already completed or has already been cancelled. If successful, then
cancellation will complete this Promise with an appropriate
exception and notify any registered functions and completion handlers.
After this method returns, subsequent calls to Promise.isDone() will
always return true. Subsequent calls to Promise.isCancelled() will
always return true if this method returned true.
cancel in interface Future<V>cancel in interface Promise<V,E extends Exception>mayInterruptIfRunning - true if the thread executing executing the response
handler should be interrupted; otherwise, in-progress response
handlers are allowed to complete.false if Promise could not be cancelled,
typically because it has already completed normally; true
otherwise.public final V get() throws InterruptedException, ExecutionException
PromisePromise to complete, and then returns
the result if it completed successfully, or throws an
ExecutionException containing the cause of the failure.get in interface Future<V>get in interface Promise<V,E extends Exception>Promise completed
successfully.InterruptedException - If the current thread was interrupted while waiting.ExecutionException - If this Promise was cancelled or did not complete
successfully. The ExecutionException will contain the
cause of the failure.public final V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
PromisePromise to
complete, and then returns the result if it completed successfully, or
throws an ExecutionException containing the cause of the failure.get in interface Future<V>get in interface Promise<V,E extends Exception>timeout - The maximum time to wait.unit - The time unit of the timeout argument.Promise completed
successfully.InterruptedException - If the current thread was interrupted while waiting.ExecutionException - If this Promise was cancelled or did not complete
successfully. The ExecutionException will contain the
cause of the failure.TimeoutException - If the wait timed out.public final V getOrThrow() throws InterruptedException, E extends Exception
PromisePromise to complete, and then returns
the result if it completed successfully, or throws an exception
representing the cause of the failure.getOrThrow in interface Promise<V,E extends Exception>Promise completed
successfully.InterruptedException - If the current thread was interrupted while waiting.E - If this Promise was cancelled or did not complete
successfully.E extends Exceptionpublic final V getOrThrow(long timeout, TimeUnit unit) throws InterruptedException, E extends Exception, TimeoutException
PromisePromise to
complete, and then returns the result if it completed successfully, or
throws an exception representing the cause of the failure.getOrThrow in interface Promise<V,E extends Exception>timeout - The maximum time to wait.unit - The time unit of the timeout argument.Promise completed
successfully.InterruptedException - If the current thread was interrupted while waiting.E - If this Promise was cancelled or did not complete
successfully.TimeoutException - If the wait timed out.E extends Exceptionpublic final V getOrThrowUninterruptibly() throws E extends Exception
PromisePromise to complete, and then returns
the result if it completed successfully, or throws an exception
representing the cause of the failure.
This method is similar to Promise.getOrThrow() except that it will
ignore thread interrupts. When this method returns the status of the
current thread will be interrupted if an interrupt was received while
waiting.
public final V getOrThrowUninterruptibly(long timeout, TimeUnit unit) throws E extends Exception, TimeoutException
PromisePromise to
complete, and then returns the result if it completed successfully, or
throws an exception representing the cause of the failure.
This method is similar to Promise.getOrThrow(long, TimeUnit) except that
it will ignore thread interrupts. When this method returns the status of
the current thread will be interrupted if an interrupt was received while
waiting.
getOrThrowUninterruptibly in interface Promise<V,E extends Exception>timeout - The maximum time to wait.unit - The time unit of the timeout argument.Promise completed
successfully.E - If this Promise was cancelled or did not complete
successfully.TimeoutException - If the wait timed out.E extends Exceptionpublic final void handleException(E exception)
isDone() == true)
then calling this method has no effect and the provided result will be
discarded.handleException in interface ExceptionHandler<E extends Exception>exception - The exception indicating why the task failed.tryHandleException(Exception)public void handleRuntimeException(RuntimeException exception)
RuntimeExceptionHandlerhandleRuntimeException in interface RuntimeExceptionHandlerexception - The runtime exception indicating why the asynchronous
task has failed.public final void handleResult(V result)
isDone() == true) then calling this method has no effect and the
provided result will be discarded.handleResult in interface ResultHandler<V>result - The result of the asynchronous task (may be null).tryHandleResult(Object)public final boolean tryHandleException(E exception)
isDone() == true) then calling this method has no effect and
false is returned.
This method should be used in cases where multiple threads may concurrently attempt to complete a promise and need to release resources if the completion attempt fails. For example, an asynchronous TCP connect attempt may complete after a timeout has expired. In this case the connection should be immediately closed because it is never going to be used.
exception - The exception indicating why the task failed.false if this promise has already been completed, either
due to normal termination, an exception, or cancellation (i.e.
isDone() == true).handleException(Exception),
isDone()public final boolean tryHandleResult(V result)
isDone() == true) then calling this method has no effect and
false is returned.
This method should be used in cases where multiple threads may concurrently attempt to complete a promise and need to release resources if the completion attempt fails. For example, an asynchronous TCP connect attempt may complete after a timeout has expired. In this case the connection should be immediately closed because it is never going to be used.
result - The result of the asynchronous task (may be null).false if this promise has already been completed, either
due to normal termination, an exception, or cancellation (i.e.
isDone() == true).handleResult(Object),
isDone()public final boolean isCancelled()
Promisetrue if this Promise was cancelled before it
completed normally.isCancelled in interface Future<V>isCancelled in interface Promise<V,E extends Exception>true if this Promise was cancelled before it
completed normally, otherwise false.public final boolean isDone()
Promisetrue if this Promise has completed.
Completion may be due to normal termination, an exception, or
cancellation. In all of these cases, this method will return true.
public final Promise<V,E> thenOnException(ExceptionHandler<? super E> onException)
PromisePromise cannot be completed due to an exception. If this
Promise completes with a result then the completion handler
will not be notified.
This method can be used for asynchronous completion notification.
thenOnException in interface Promise<V,E extends Exception>onException - The completion handler which will be notified upon failure
completion of this Promise.Promise.public final Promise<V,E> thenOnResult(ResultHandler<? super V> onResult)
PromisePromise has completed with a result. If this Promise
completes with an exception then the completion handler will not be
notified.
This method can be used for asynchronous completion notification and is
equivalent to Promise.then(Function).
thenOnResult in interface Promise<V,E extends Exception>onResult - The completion handler which will be notified upon successful
completion of this Promise.Promise.public final Promise<V,E> thenOnResultOrException(ResultHandler<? super V> onResult, ExceptionHandler<? super E> onException)
PromisePromise has completed (with a result or an exception). If this
Promise completes with a result then onResult will be
notified with the result, otherwise onException will be notified
with the exception that occurred.
This method can be used for asynchronous completion notification.
thenOnResultOrException in interface Promise<V,E extends Exception>onResult - The completion handler which will be notified upon completion
with a result of this Promise.onException - The completion handler which will be notified upon failure of
this Promise.Promise.public final Promise<V,E> thenOnResultOrException(Runnable onResultOrException)
PromisePromise has
completed, and regardless of whether it has a result or an exception.
This method can be used for resource cleanup after a series of
asynchronous tasks have completed. More specifically, this method should
be used in a similar manner to finally statements in
try...catch expressions.
This method is equivalent to Promise.thenAlways(Runnable).
thenOnResultOrException in interface Promise<V,E extends Exception>onResultOrException - The runnable which will be notified regardless of the final
outcome of this Promise.Promise.public final <VOUT> Promise<VOUT,E> then(Function<? super V,VOUT,E> onResult)
PromisePromise has
completed with a result, and returns a new Promise representing
the outcome of the function. If this Promise does not
complete with a result then the function will not be invoked and the exception
will be forwarded to the returned Promise.
This method can be used for transforming the result of an asynchronous task.
then in interface Promise<V,E extends Exception>VOUT - The type of the function's result, or Void if the
function does not return anything (i.e. it only has
side-effects). Note that the type may be different to the type
of this Promise.onResult - The function which will be executed upon successful completion
of this Promise.Promise representing the outcome of the
function.public <EOUT extends Exception> Promise<V,EOUT> thenCatch(Function<? super E,V,EOUT> onException)
PromisePromise has
not completed with a result (has completed with an exception), and returns
a new Promise representing the outcome of the function.
If this Promise completes with a result then the function will not
be invoked and the result notification will be forwarded to the returned
Promise.
This method can be used for transforming the result of an asynchronous task.
thenCatch in interface Promise<V,E extends Exception>EOUT - The type of the exception thrown by the function if it
fails, or NeverThrowsException if it cannot fails.
Note that the type may be different to the type of this
Promise.onException - The function which will be executed upon failure completion
of this Promise.Promise representing the outcome of the
function.public Promise<V,E> thenCatchRuntimeException(Function<? super RuntimeException,V,E> onRuntimeException)
PromisePromise has
not completed with a result nor with an exception but with a RuntimeException, and returns
a new Promise representing the outcome of the function.
If this Promise completes with a result or an exception then the function will not
be invoked and the result notification will be forwarded to the returned
Promise.
This method can be used for transforming the result of an asynchronous task.
thenCatchRuntimeException in interface Promise<V,E extends Exception>onRuntimeException - The function which will be executed upon failure completion
of this Promise.Promise representing the outcome of the
function.public final <VOUT,EOUT extends Exception> Promise<VOUT,EOUT> then(Function<? super V,VOUT,EOUT> onResult, Function<? super E,VOUT,EOUT> onException)
PromisePromise
has completed (with a result or an exception), and returns a new
Promise representing the outcome of the invoked function. If
this Promise completes with a result then onResult
will be invoked with the result, otherwise onException will
be invoked with the exception that occurred.
This method can be used for transforming the outcome of an asynchronous task.
then in interface Promise<V,E extends Exception>VOUT - The type of the functions' result, or Void if the
functions do not return anything (i.e. they only have
side-effects). Note that the type may be different to the type
of this Promise.EOUT - The type of the exception thrown by the functions if they
fail, or NeverThrowsException if they cannot fail.
Note that the type may be different to the type of this
Promise.onResult - The function which will be executed upon successful completion
of this Promise.onException - The function which will be executed upon failure of this
Promise.Promise representing the outcome of the
invoked function.public final <VOUT,EOUT extends Exception> Promise<VOUT,EOUT> then(Function<? super V,VOUT,EOUT> onResult, Function<? super E,VOUT,EOUT> onException, Function<? super RuntimeException,VOUT,EOUT> onRuntimeException)
PromisePromise
has completed (with a result or an exception or a RuntimeException), and returns a new
Promise representing the outcome of the invoked function. If
this Promise completes with a result then onResult
will be invoked with the result, with a RuntimeException then onRuntimeException
will be invoked with the runtime exception that occurred, otherwise onException will
be invoked with the exception that occurred.
This method can be used for transforming the outcome of an asynchronous task.
then in interface Promise<V,E extends Exception>VOUT - The type of the functions' result, or Void if the
functions do not return anything (i.e. they only have
side-effects). Note that the type may be different to the type
of this Promise.EOUT - The type of the exception thrown by the functions if they
fail, or NeverThrowsException if they cannot fail.
Note that the type may be different to the type of this
Promise.onResult - The function which will be executed upon successful completion
of this Promise.onException - The function which will be executed upon failure of this
Promise.onRuntimeException - The function which will be executed upon failure with
RuntimeException of this Promise.Promise representing the outcome of the
invoked function.public final Promise<V,E> thenAlways(Runnable always)
PromisePromise has
completed, and regardless of whether it has a result or an exception.
This method can be used for resource cleanup after a series of
asynchronous tasks have completed. More specifically, this method should
be used in a similar manner to finally statements in
try...catch expressions.
This method is equivalent to Promise.thenOnResultOrException(Runnable).
thenAlways in interface Promise<V,E extends Exception>always - The runnable which will be notified regardless of the final
outcome of this Promise.Promise.public final Promise<V,E> thenFinally(Runnable onFinally)
PromisePromise has
completed, and regardless of whether of its outcome.
This method can be used for resource cleanup after a series of
asynchronous tasks have completed. More specifically, this method should
be used in a similar manner to finally statements in
try...catch expressions.
This method is equivalent to Promise.thenAlways(Runnable).
thenFinally in interface Promise<V,E extends Exception>onFinally - The runnable which will be notified regardless of the final
outcome of this Promise.Promise.public final <VOUT> Promise<VOUT,E> thenAsync(AsyncFunction<? super V,VOUT,E> onResult)
PromisePromise has completed with a result, and returns a new
Promise representing the outcome of the function. If
this Promise complete with an exception then the function
will not be invoked and the error will be forwarded to the returned
Promise.
This method may be used for chaining together a series of asynchronous tasks.
thenAsync in interface Promise<V,E extends Exception>VOUT - The type of the function's result, or Void if the
function does not return anything (i.e. it only has
side-effects). Note that the type may be different to the type
of this Promise.onResult - The asynchronous function which will be executed upon
successful completion of this Promise.Promise representing the outcome of the
function.public final <EOUT extends Exception> Promise<V,EOUT> thenCatchAsync(AsyncFunction<? super E,V,EOUT> onException)
PromisePromise has completed with an exception, and returns a new
Promise representing the outcome of the function. If
this Promise completes with a result then the function
will not be invoked and the exception will be forwarded to the returned
Promise.
This method may be used for chaining together a series of asynchronous tasks.
thenCatchAsync in interface Promise<V,E extends Exception>EOUT - The type of the exception thrown by the function if it
fails, or NeverThrowsException if it cannot fails.
Note that the type may be different to the type of this
Promise.onException - The asynchronous function which will be executed upon failure completion
of this Promise.Promise representing the outcome of the
function.public final Promise<V,E> thenCatchRuntimeExceptionAsync(AsyncFunction<? super RuntimeException,V,E> onRuntimeException)
PromisePromise has completed with a RuntimeException, and returns a new
Promise representing the outcome of the function. If this
Promise completes with a result or the typed exception then the
completion asynchronous function will not be called.
This method may be used for chaining together a series of asynchronous tasks.
thenCatchRuntimeExceptionAsync in interface Promise<V,E extends Exception>onRuntimeException - The asynchronous function which will be executed upon failure completion
with a RuntimeException of this Promise.Promise representing the outcome of the
function.public final <VOUT,EOUT extends Exception> Promise<VOUT,EOUT> thenAsync(AsyncFunction<? super V,VOUT,EOUT> onResult, AsyncFunction<? super E,VOUT,EOUT> onException)
PromisePromise has completed, and returns a new Promise
representing the outcome of the invoked function. If this
Promise completes with a result then onResult will be
invoked with the result, otherwise onException will be invoked with
the exception that occurred.
This method may be used for chaining together a series of asynchronous tasks.
thenAsync in interface Promise<V,E extends Exception>VOUT - The type of the functions' result, or Void if the
functions do not return anything (i.e. they only have
side-effects). Note that the type may be different to the type
of this Promise.EOUT - The type of the exception thrown by the functions if they
fail, or NeverThrowsException if they cannot fail.
Note that the type may be different to the type of this
Promise.onResult - The asynchronous function which will be executed upon
successful completion of this Promise.onException - The asynchronous function which will be executed upon failure
of this Promise.Promise representing the outcome of the
invoked function.public final <VOUT,EOUT extends Exception> Promise<VOUT,EOUT> thenAsync(AsyncFunction<? super V,VOUT,EOUT> onResult, AsyncFunction<? super E,VOUT,EOUT> onException, AsyncFunction<? super RuntimeException,VOUT,EOUT> onRuntimeException)
PromisePromise has completed, and returns a new Promise
representing the outcome of the invoked function. If this
Promise completes with a result then onResult will be
invoked with the result, otherwise onException will be invoked with
the exception that occurred, or onRuntimeException will be invoked with
the runtime exception that occurred.
This method may be used for chaining together a series of asynchronous tasks.
thenAsync in interface Promise<V,E extends Exception>VOUT - The type of the functions' result, or Void if the
functions do not return anything (i.e. they only have
side-effects). Note that the type may be different to the type
of this Promise.EOUT - The type of the exception thrown by the functions if they
fail, or NeverThrowsException if they cannot fail.
Note that the type may be different to the type of this
Promise.onResult - The asynchronous function which will be executed upon
successful completion of this Promise.onException - The asynchronous function which will be executed upon failure
of this Promise.onRuntimeException - The asynchronous function which will be executed upon failure
with RuntimeException of this Promise.Promise representing the outcome of the
invoked function.public final Promise<V,E> thenOnRuntimeException(RuntimeExceptionHandler onRuntimeException)
PromisePromise cannot be completed due to an runtime exception. If this
Promise completes with a result or the typed exception then the
completion handler will not be notified.
This method can be used for asynchronous completion notification.
thenOnRuntimeException in interface Promise<V,E extends Exception>onRuntimeException - The completion handler which will be notified upon an
uncaught runtime exception completion of this
Promise.Promise.protected E tryCancel(boolean mayInterruptIfRunning)
By default cancellation is not supported and this method returns
null.
mayInterruptIfRunning - true if the thread executing this task should be
interrupted; otherwise, in-progress tasks are allowed to
complete.null if cancellation was not supported or not possible,
otherwise an appropriate exception.Copyright © 2025 Open Identity Platform Community. All rights reserved.