Interface Promise<T>
- All Superinterfaces:
AsyncComputation<T>
- All Known Implementing Classes:
AbstractPromise,AbstractPromise.EitherPromise,AbstractPromise.PromiseBoth,AbstractPromise.PromiseCombine,CompleteExceptionallyPromise,CompleteNullPromise,CompletePromise,CompleteResultPromise,NextPromise,SettablePromise
CompletionStage interface with
optimized design, which allows handling different scenarios more
efficiently.
Each promise represents some sort of operations executed
after the previous Promise completes.
Promise can complete either successfully with a result
which will be wrapped inside the Promise or exceptionally,
returning a new CompletePromise or CompleteExceptionallyPromise
respectively.
SettablePromise allows to create a root for chain of Promises.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionasync()Ensures thatPromisecompletes asynchronously: if thisPromiseis already completed, its completion will be posted to the next reactor tick.Returns a newPromisewhen both this and providedotherPromisescomplete.default voiddefault <U> Promise<U>cast()Caststhispromise to a promise of some other typePromisedefault <U> Promise<U>Caststhispromise to a promise of some other typePromise<U,V> Promise<V> combine(Promise<? extends U> other, io.activej.common.function.BiFunctionEx<? super T, ? super U, ? extends V> fn) Returns a newPromisethat, when this and the other givenPromiseboth complete, is executed with the two results as arguments to the supplied function.complete()Creates successfully completedPromiseReturns thePromisewhich was completed first.io.activej.common.collection.Try<T>getTry()default booleanbooleanbooleanisResult()default <U> Promise<U>Returns a newPromisewhich is obtained by mapping a result and an exception ofthispromise to some other value.default <U> Promise<U>Returns a newPromisewhich is obtained by mapping a result ofthispromise to some other value.default <U> Promise<U>map(io.activej.common.function.FunctionEx<? super T, ? extends U> fn, io.activej.common.function.FunctionEx<Exception, ? extends U> exceptionFn) Returns a newPromisewhich is obtained by mapping either a result or an exception ofthispromise to some other values.mapException(io.activej.common.function.FunctionEx<Exception, Exception> exceptionFn) Returns a newPromisewhich is obtained by mapping an exception ofthispromise to some other exception.mapException(Class<E> clazz, io.activej.common.function.FunctionEx<? super E, ? extends Exception> exceptionFn) Returns a newPromisewhich is obtained by mapping an exception ofthispromise to some other exception.voidnext(NextPromise<? super T, ?> cb) Calls acallbackafterthispromise is completed, passing both a result and an exception of a promise to the callback.static <T> Promise<T>of(T value) Creates successfully completedPromise.static <T> Promise<T>Creates a completedPromisefromT valueandException eparameters, any of them can benull.ofBlocking(Executor executor, io.activej.common.function.RunnableEx runnable) Same asofBlocking(Executor, SupplierEx), but without a result (returnedPromiseis only a marker of completion).static <T> Promise<T>ofBlocking(Executor executor, io.activej.common.function.SupplierEx<? extends T> supplier) Runs some task in another thread (executed by a givenExecutor) and returns aPromisefor it.static <T> Promise<T>ofCallback(CallbackSupplierEx<T> fn) Creates and returns a newSettablePromisethat is accepted by the providedConsumerExofSettablePromisestatic <T,R> Promise<R> ofCallback(T value, @Nullable Exception exception, CallbackBiFunctionEx<? super T, @Nullable Exception, R> fn) static <T,R> Promise<R> ofCallback(T value, @Nullable Exception exception, CallbackFunctionEx<? super T, R> fn, CallbackFunctionEx<Exception, R> fnException) static <T,R> Promise<R> ofCallback(T value, CallbackFunctionEx<? super T, R> fn) static <T> Promise<T>ofCompletionStage(CompletionStage<? extends T> completionStage) Wraps JavaCompletionStagein aPromise, running it in current reactor.static <T> Promise<T>Creates an exceptionally completedPromise.static <T> Promise<T>ofFuture(CompletableFuture<? extends T> future) Creates aPromisewrapper around default JavaCompletableFutureand runs it immediately.static <T> Promise<T>static <T> Promise<T>ofOptional(Optional<T> optional) Creates a newPromiseof the given valuestatic <T> Promise<T>ofOptional(Optional<T> optional, Supplier<? extends Exception> errorSupplier) Creates a newPromiseof the given value.static <T> Promise<T>ofTry(io.activej.common.collection.Try<T> t) default <U> Promise<U>then(AsyncBiFunctionEx<? super T, @Nullable Exception, U> fn) Returns a newPromisewhich is obtained by mapping a result and an exception ofthispromise to some other promise.default <U> Promise<U>then(AsyncFunctionEx<? super T, U> fn) Returns a newPromisewhich is obtained by mapping a result ofthispromise to some other promise.default <U> Promise<U>then(AsyncFunctionEx<? super T, U> fn, AsyncFunctionEx<Exception, U> exceptionFn) Returns a newPromisewhich is obtained by mapping either a result or an exception ofthispromise to some other promises.default <U> Promise<U>then(AsyncSupplierEx<U> fn) Returns a newPromisewhich is obtained by calling a provided supplier of a new promise.<U> Promise<U>thenCallback(CallbackBiFunctionEx<? super T, @Nullable Exception, U> fn) default <U> Promise<U>thenCallback(CallbackFunctionEx<? super T, U> fn) default <U> Promise<U>thenCallback(CallbackFunctionEx<? super T, U> fn, CallbackFunctionEx<Exception, U> exceptionFn) default <U> Promise<U>thenCallback(CallbackSupplierEx<U> fn) WrapsPromiseintoCompletableFuture.toTry()ReturnsPromisethat always completes successfully with result or exception wrapped inTry.toVoid()Waits for result and discards it.whenComplete(io.activej.common.function.BiConsumerEx<? super T, Exception> fn) Subscribes given consumer to be executed after thisPromisecompletes (either successfully or exceptionally).whenComplete(io.activej.common.function.ConsumerEx<? super T> fn, io.activej.common.function.ConsumerEx<Exception> exceptionFn) Subscribes given consumers to be executed afterthisPromise completes (either successfully or exceptionally).whenComplete(io.activej.common.function.RunnableEx action) Subscribes given runnable to be executed after thisPromisecompletes (either successfully or exceptionally).whenException(io.activej.common.function.ConsumerEx<Exception> fn) Subscribes given consumer to be executed after thisPromisecompletes exceptionally.whenException(io.activej.common.function.RunnableEx action) Subscribes given runnable to be executed after thisPromisecompletes exceptionally.whenException(Class<? extends Exception> clazz, io.activej.common.function.RunnableEx action) Subscribes given runnable to be executed after thisPromisecompletes exceptionally and an exception of thisPromiseis an instance of a given exceptionClass.whenException(Class<E> clazz, io.activej.common.function.ConsumerEx<? super E> fn) Subscribes given consumer to be executed after thisPromisecompletes exceptionally and an exception of thisPromiseis an instance of a given exceptionClass.whenResult(io.activej.common.function.ConsumerEx<? super T> fn) Subscribes given consumer to be executed after thisPromisecompletes successfully.whenResult(io.activej.common.function.RunnableEx action) Subscribes given runnable to be executed after thisPromisecompletes successfully and a result of thisPromisesatisfy a givenPredicate.
-
Method Details
-
complete
Creates successfully completedPromise -
of
Creates successfully completedPromise.- Parameters:
value- result of Promise. If value isnull, returnsCompleteNullPromise, otherwiseCompleteResultPromise
-
ofException
Creates an exceptionally completedPromise.- Parameters:
e- Exception
-
ofCallback
Creates and returns a newSettablePromisethat is accepted by the providedConsumerExofSettablePromise -
ofCallback
-
ofCallback
static <T,R> Promise<R> ofCallback(T value, @Nullable @Nullable Exception exception, CallbackBiFunctionEx<? super T, @Nullable Exception, R> fn) -
ofCallback
static <T,R> Promise<R> ofCallback(T value, @Nullable @Nullable Exception exception, CallbackFunctionEx<? super T, R> fn, CallbackFunctionEx<Exception, R> fnException) -
ofOptional
Creates a newPromiseof the given value- See Also:
-
ofOptional
Creates a newPromiseof the given value. IfOptionaldoesn't equalnull, aPromiseofoptionalcontained value will be created. Otherwise, aPromisewitherrorSupplierexception will be created.- Returns:
CompletePromiseif the optional value doesn't equalnull, otherwiseCompleteExceptionallyPromisewitherrorSupplierexception.
-
of
Creates a completedPromisefromT valueandException eparameters, any of them can benull. Useful forthen(io.activej.async.function.AsyncSupplierEx<U>)passthroughs (for example, when mapping specific exceptions).- Parameters:
value- value to wrap when exception is nulle- possibly-null exception, determines type of promise completion
-
ofTry
-
ofFuture
Creates aPromisewrapper around default JavaCompletableFutureand runs it immediately.- Returns:
- a new
Promisewith a result of the given future
-
ofCompletionStage
Wraps JavaCompletionStagein aPromise, running it in current reactor.- Parameters:
completionStage- completion stage itself- Returns:
- result of the given completionStage wrapped in a
Promise
-
ofFuture
- Parameters:
executor- executor to execute the future concurrentlyfuture- the future itself- Returns:
- a new
Promiseof the future result
-
ofBlocking
static <T> Promise<T> ofBlocking(Executor executor, io.activej.common.function.SupplierEx<? extends T> supplier) Runs some task in another thread (executed by a givenExecutor) and returns aPromisefor it. Also manages external task count for current reactor, so it won't shut down until the task is complete.- Parameters:
executor- executor to execute the task concurrentlysupplier- the task itself- Returns:
Promisefor the given task
-
ofBlocking
Same asofBlocking(Executor, SupplierEx), but without a result (returnedPromiseis only a marker of completion). -
isComplete
@Contract(pure=true) default boolean isComplete() -
isResult
@Contract(pure=true) boolean isResult() -
isException
@Contract(pure=true) boolean isException() -
getResult
-
getException
-
getTry
-
async
Ensures thatPromisecompletes asynchronously: if thisPromiseis already completed, its completion will be posted to the next reactor tick. Otherwise, does nothing. -
subscribe
-
map
Returns a newPromisewhich is obtained by mapping a result ofthispromise to some other value. Ifthispromise is completed exceptionally, a mapping function will not be applied.A mapping function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- a function to map the result of thisPromiseto a new value- Returns:
- new
Promisewhose result is the result of function applied to the result ofthispromise - See Also:
-
map
default <U> Promise<U> map(io.activej.common.function.BiFunctionEx<? super T, @Nullable Exception, ? extends U> fn) Returns a newPromisewhich is obtained by mapping a result and an exception ofthispromise to some other value. Ifthispromise is completed exceptionally, an exception passed to a mapping function is guaranteed to be not null.A {bi function} may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- a function to map a result and an exception ofthispromise to some other value- Returns:
- new
Promisewhose result is the result of function applied to the result and exception ofthispromise
-
map
default <U> Promise<U> map(io.activej.common.function.FunctionEx<? super T, ? extends U> fn, io.activej.common.function.FunctionEx<Exception, ? extends U> exceptionFn) Returns a newPromisewhich is obtained by mapping either a result or an exception ofthispromise to some other values. Ifthispromise is completed successfully, the first function will be called mapping the result to some other value. Otherwise, second function will be called mapping the exception to some other value.Each function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- a function to map a result ofthispromise to some other valueexceptionFn- a function to map an exception ofthispromise to some other value- Returns:
- new
Promisewhose result is the result of either first or second function applied either to a result or an exception ofthispromise.
-
mapException
default Promise<T> mapException(io.activej.common.function.FunctionEx<Exception, Exception> exceptionFn) Returns a newPromisewhich is obtained by mapping an exception ofthispromise to some other exception. The mapping function will be called only ifthispromise completes exceptionallyA mapping function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
exceptionFn- a function to map an exception ofthispromise to some other value- Returns:
- new
Promisewhose result is the result of a mapping function applied either to an exception ofthispromise.
-
mapException
default <E extends Exception> Promise<T> mapException(Class<E> clazz, io.activej.common.function.FunctionEx<? super E, ? extends Exception> exceptionFn) Returns a newPromisewhich is obtained by mapping an exception ofthispromise to some other exception. The mapping function will be called only ifthispromise completes with an exception that is an instance of a givenClassA mapping function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
clazz- an exception class that exception is tested against. A mapping function is applied only if an exception ofthispromise is an instance of the specified classexceptionFn- a function to map an exception ofthispromise to some other value- Returns:
- new
Promisewhose result is the result of a mapping function applied either to an exception ofthispromise.
-
then
Returns a newPromisewhich is obtained by calling a provided supplier of a new promise. Ifthispromise is completed exceptionally, a supplier will not be called.A supplier may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- a supplier of a new promise which will be called ifthispromise completes successfully
-
thenCallback
-
then
Returns a newPromisewhich is obtained by mapping a result ofthispromise to some other promise. Ifthispromise is completed exceptionally, a mapping function will not be applied.A mapping function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- a function to map the result of thisPromiseto a new promise- Returns:
- new
Promisewhich is the result of function applied to the result ofthispromise - See Also:
-
thenCallback
-
then
Returns a newPromisewhich is obtained by mapping a result and an exception ofthispromise to some other promise. Ifthispromise is completed exceptionally, an exception passed to a mapping function is guaranteed to be not null.A function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- a function to map a result and an exception ofthispromise to some other promise- Returns:
- new
Promisewhich is the result of function applied to the result and exception ofthispromise
-
thenCallback
-
then
default <U> Promise<U> then(AsyncFunctionEx<? super T, U> fn, AsyncFunctionEx<Exception, U> exceptionFn) Returns a newPromisewhich is obtained by mapping either a result or an exception ofthispromise to some other promises. Ifthispromise is completed successfully, the first function will be called mapping the result to a promise of some other value. Otherwise, second function will be called mapping the exception to a promise of some other value.Each function may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- a function to map a result ofthispromise to a dofferent promiseexceptionFn- a function to map an exception ofthispromise a different promise- Returns:
- new
Promisewhich is a result of either first or second function applied either to a result or an exception ofthispromise.
-
thenCallback
default <U> Promise<U> thenCallback(CallbackFunctionEx<? super T, U> fn, CallbackFunctionEx<Exception, U> exceptionFn) -
whenComplete
Subscribes given consumer to be executed after thisPromisecompletes (either successfully or exceptionally). Returns a newPromise.A consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- a consumer that consumes a result and an exception ofthispromise
-
whenComplete
default Promise<T> whenComplete(io.activej.common.function.ConsumerEx<? super T> fn, io.activej.common.function.ConsumerEx<Exception> exceptionFn) Subscribes given consumers to be executed afterthisPromise completes (either successfully or exceptionally). The first consumer will be executed ifthisPromise completes successfully, otherwise the second promise will be executed. Returns a newPromise.Each consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- consumer that consumes a result ofthispromiseexceptionFn- consumer that consumes an exception ofthispromise
-
whenComplete
Subscribes given runnable to be executed after thisPromisecompletes (either successfully or exceptionally). Returns a newPromise.A runnable may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
action- runnable to be executed afterthispromise completes
-
whenResult
Subscribes given consumer to be executed after thisPromisecompletes successfully. Returns a newPromise.A consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- consumer that consumes a result ofthispromise
-
whenResult
Subscribes given runnable to be executed after thisPromisecompletes successfully and a result of thisPromisesatisfy a givenPredicate. Returns a newPromise.A runnable may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
action- runnable to be executed afterthispromise completes successfully
-
whenException
Subscribes given consumer to be executed after thisPromisecompletes exceptionally. Returns a newPromise.A consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
fn- consumer that consumes an exception ofthispromise
-
whenException
default <E extends Exception> Promise<T> whenException(Class<E> clazz, io.activej.common.function.ConsumerEx<? super E> fn) Subscribes given consumer to be executed after thisPromisecompletes exceptionally and an exception of thisPromiseis an instance of a given exceptionClass. Returns a newPromise.A consumer may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
clazz- an exception class that exception is tested against. A consumer is called only if an exception ofthispromise is an instance of the specified classfn- consumer that consumes an exception ofthispromise
-
whenException
Subscribes given runnable to be executed after thisPromisecompletes exceptionally. Returns a newPromise.A runnable may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
action- runnable to be executed afterthispromise completes exceptionally
-
whenException
default Promise<T> whenException(Class<? extends Exception> clazz, io.activej.common.function.RunnableEx action) Subscribes given runnable to be executed after thisPromisecompletes exceptionally and an exception of thisPromiseis an instance of a given exceptionClass. Returns a newPromise.A runnable may throw a checked exception. In this case the resulting promise is completed exceptionally with a thrown exception.
- Parameters:
clazz- an exception class that exception is tested against. A consumer is called only if an exception ofthispromise is an instance of the specified classaction- runnable to be executed afterthispromise completes exceptionally and an exception of thisPromiseis an instance of a given exceptionClass.
-
cast
Caststhispromise to a promise of some other typePromiseUsage example:
Promise<Object> promise = Promise.of(255); promise.<Integer>cast() .map(Integer::toHexString) .whenResult(hex -> System.out.println(hex));- Type Parameters:
U- a type to which a promise result will be cast- Returns:
- a promise of type
Promise - Throws:
ClassCastException- if a promise result is notnulland a cast could not be made
-
cast
Caststhispromise to a promise of some other typePromiseUsage example:
Promise<Object> promise = Promise.of(255); promise.cast(Integer.class) .map(Integer::toHexString) .whenResult(hex -> System.out.println(hex));- Type Parameters:
U- a type to which a promise result will be cast- Parameters:
cls- a class to which a result ofthispromise will be cast- Returns:
- a promise of type
Promise - Throws:
ClassCastException- if a promise result is notnulland a cast could not be made
-
combine
@Contract(pure=true) <U,V> Promise<V> combine(Promise<? extends U> other, io.activej.common.function.BiFunctionEx<? super T, ? super U, ? extends V> fn) Returns a newPromisethat, when this and the other givenPromiseboth complete, is executed with the two results as arguments to the supplied function.- Parameters:
other- the otherPromisefn- the function to use to compute the value of the returnedPromise- Returns:
- new
Promise
-
both
Returns a newPromisewhen both this and providedotherPromisescomplete.- Parameters:
other- the otherPromise- Returns:
Promiseofnullwhen both this and otherPromisecomplete
-
either
Returns thePromisewhich was completed first.- Parameters:
other- the otherPromise- Returns:
- the first completed
Promise
-
toTry
ReturnsPromisethat always completes successfully with result or exception wrapped inTry. -
toVoid
Waits for result and discards it. -
next
Calls acallbackafterthispromise is completed, passing both a result and an exception of a promise to the callback. This method is similar towhenComplete(BiConsumerEx)but does not create or return a new promise. ACallbackinterface also prohibits throwing checked exceptions.In most cases
whenComplete(BiConsumerEx)is preferred. Fall back to using this method only if you expect a performance hit from constantly callingwhenComplete(BiConsumerEx)and creating new promises as a result.- Parameters:
cb- a callback to be called once a promise completes
-
call
- Specified by:
callin interfaceAsyncComputation<T>
-
toCompletableFuture
WrapsPromiseintoCompletableFuture.
-