Package io.activej.promise
Interface Promise<T>
-
- All Superinterfaces:
AsyncComputation<T>,Promisable<T>
- All Known Implementing Classes:
CompleteExceptionallyPromise,CompleteNullPromise,CompletePromise,CompleteResultPromise,NextPromise,SettablePromise
public interface Promise<T> extends Promisable<T>, AsyncComputation<T>
Replacement of default JavaCompletionStageinterface with optimized design, which allows to handle different scenarios more efficiently.Each promise represents some sort of operations executed after the previous
Promisecompletes.Promisecan complete either successfully with a result which will be wrapped inside thePromiseor exceptionally, returning a newCompletePromiseorCompleteExceptionallyPromiserespectively.SettablePromiseallows to create a root for chain ofPromises.- See Also:
CompletionStage
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfacePromise.BlockingCallable<V>static interfacePromise.BlockingRunnable
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description @NotNull Promise<T>async()Ensures thatPromisecompletes asynchronously: if thisPromiseis already completed, its completion will be posted to next eventloop tick.@NotNull Promise<Void>both(@NotNull Promise<?> other)Returns a newPromisewhen both this and providedotherPromisescomplete.<U,V>
@NotNull Promise<V>combine(@NotNull Promise<? extends U> other, @NotNull BiFunction<? 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.static @NotNull CompleteNullPromise<Void>complete()Creates successfully completedPromise@NotNull Promise<T>either(@NotNull Promise<? extends T> other)Returns thePromisewhich was completed first.ThrowablegetException()TgetResult()io.activej.common.collection.Try<T>getTry()default booleanisComplete()booleanisException()booleanisResult()<U> @NotNull Promise<U>map(@NotNull Function<? super T,? extends U> fn)Returns a newPromisewhich is executed with thisPromise's result as the argument to the provided function when thisPromisecompletes successfully.<U> @NotNull Promise<U>mapEx(@NotNull BiFunction<? super T,@Nullable Throwable,? extends U> fn)Returns a newPromisewhich is executed with thisPromise's result as the argument to the provided function when thisPromisecompletes either successfully (whenexceptionisnull) or with an exception.<U,P extends Callback<? super T> & Promise<U>>
@NotNull Promise<U>next(P promise)Executes givenpromiseafter execution of thisPromisecompletes.static <T> @NotNull CompletePromise<T>of(T value)Creates successfully completedPromise.static <T> @NotNull Promise<T>of(T value, @Nullable Throwable e)Creates a completedPromisefromT valueandThrowable eparameters, any of them can benull.static <T> Promise<T>ofBlockingCallable(@NotNull Executor executor, @NotNull Promise.BlockingCallable<? extends T> callable)Runs some task in another thread (executed by a givenExecutor) and returns aPromisefor it.static @NotNull Promise<Void>ofBlockingRunnable(@NotNull Executor executor, @NotNull Promise.BlockingRunnable runnable)Same asofBlockingCallable(Executor, BlockingCallable), but without a result (returnedPromiseis only a marker of completion).static <T> @NotNull Promise<T>ofCallback(@NotNull Consumer<@NotNull SettablePromise<T>> callbackConsumer)Creates and returns a newSettablePromisethat is accepted by the providedConsumerofSettablePromisestatic <T> @NotNull Promise<T>ofCompletionStage(CompletionStage<? extends T> completionStage)Wraps JavaCompletionStagein aPromise, running it in current eventloop.static <T> @NotNull CompleteExceptionallyPromise<T>ofException(@NotNull Throwable e)Creates an exceptionally completedPromise.static <T> @NotNull Promise<T>ofFuture(@NotNull CompletableFuture<? extends T> future)Creates aPromisewrapper around default JavaCompletableFutureand runs it immediately.static <T> @NotNull Promise<T>ofFuture(@NotNull Executor executor, @NotNull Future<? extends T> future)static <T> @NotNull Promise<T>ofOptional(@NotNull Optional<T> optional)static <T> @NotNull Promise<T>ofOptional(@NotNull Optional<T> optional, @NotNull Supplier<? extends Throwable> errorSupplier)Creates a newPromiseof the given value.static <T> @NotNull Promise<T>ofTry(@NotNull io.activej.common.collection.Try<T> t)default @NotNull Promise<T>post()default Promise<T>promise()default voidrun(@NotNull Callback<? super T> action)<U> @NotNull Promise<U>then(@NotNull Function<? super T,? extends Promise<? extends U>> fn)Returns a newPromisewhich, when thisPromisecompletes successfully, is executed with thisPromise'sresult as the argument to the supplied function.<U> @NotNull Promise<U>then(@NotNull Supplier<? extends Promise<? extends U>> fn)<U> @NotNull Promise<U>thenEx(@NotNull BiFunction<? super T,@Nullable Throwable,? extends Promise<? extends U>> fn)Returns a newPromisewhich, when thisPromisecompletes either successfully (if exception isnull) or exceptionally (if exception is notnull), is executed with thisPromise'sresult as the argument to the supplied function.@NotNull CompletableFuture<T>toCompletableFuture()WrapsPromiseintoCompletableFuture.@NotNull Promise<io.activej.common.collection.Try<T>>toTry()ReturnsPromisethat always completes successfully with result or exception wrapped inTry.@NotNull Promise<Void>toVoid()Waits for result and discards it.@NotNull Promise<T>whenComplete(@NotNull Callback<? super T> action)Subscribes given action to be executed after thisPromisecompletes and returns a newPromise.@NotNull Promise<T>whenComplete(@NotNull Runnable action)Subscribes given action to be executed after thisPromisecompletes and returns a newPromise.Promise<T>whenException(@NotNull Runnable action)Promise<T>whenException(@NotNull Consumer<Throwable> action)Subscribes given action to be executed after thisPromisecompletes exceptionally and returns a newPromise.Promise<T>whenResult(@NotNull Runnable action)@NotNull Promise<T>whenResult(Consumer<? super T> action)Subscribes given action to be executed after thisPromisecompletes successfully and returns a newPromise.
-
-
-
Method Detail
-
complete
@NotNull static @NotNull CompleteNullPromise<Void> complete()
Creates successfully completedPromise
-
of
@NotNull static <T> @NotNull CompletePromise<T> of(@Nullable T value)
Creates successfully completedPromise.- Parameters:
value- result of Promise. If value isnull, returnsCompleteNullPromise, otherwiseCompleteResultPromise
-
ofException
@NotNull static <T> @NotNull CompleteExceptionallyPromise<T> ofException(@NotNull @NotNull Throwable e)
Creates an exceptionally completedPromise.- Parameters:
e- Throwable
-
ofCallback
@NotNull static <T> @NotNull Promise<T> ofCallback(@NotNull @NotNull Consumer<@NotNull SettablePromise<T>> callbackConsumer)
Creates and returns a newSettablePromisethat is accepted by the providedConsumerofSettablePromise
-
ofOptional
@NotNull static <T> @NotNull Promise<T> ofOptional(@NotNull @NotNull Optional<T> optional)
- See Also:
ofOptional(Optional, Supplier)
-
ofOptional
@NotNull static <T> @NotNull Promise<T> ofOptional(@NotNull @NotNull Optional<T> optional, @NotNull @NotNull Supplier<? extends Throwable> errorSupplier)
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
@NotNull static <T> @NotNull Promise<T> of(@Nullable T value, @Nullable @Nullable Throwable e)
Creates a completedPromisefromT valueandThrowable eparameters, any of them can benull. Useful forthenEx(BiFunction)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
@NotNull static <T> @NotNull Promise<T> ofTry(@NotNull @NotNull io.activej.common.collection.Try<T> t)
-
ofFuture
@NotNull static <T> @NotNull Promise<T> ofFuture(@NotNull @NotNull CompletableFuture<? extends T> future)
Creates aPromisewrapper around default JavaCompletableFutureand runs it immediately.- Returns:
- a new
Promisewith a result of the given future
-
ofCompletionStage
@NotNull static <T> @NotNull Promise<T> ofCompletionStage(CompletionStage<? extends T> completionStage)
Wraps JavaCompletionStagein aPromise, running it in current eventloop.- Parameters:
completionStage- completion stage itself- Returns:
- result of the given completionStage wrapped in a
Promise
-
ofFuture
@NotNull static <T> @NotNull Promise<T> ofFuture(@NotNull @NotNull Executor executor, @NotNull @NotNull Future<? extends T> future)
- Parameters:
executor- executor to execute the future concurrentlyfuture- the future itself- Returns:
- a new
Promiseof the future result
-
ofBlockingCallable
static <T> Promise<T> ofBlockingCallable(@NotNull @NotNull Executor executor, @NotNull @NotNull Promise.BlockingCallable<? extends T> callable)
Runs some task in another thread (executed by a givenExecutor) and returns aPromisefor it. Also manages external task count for current eventloop, so it won't shut down until the task is complete.- Parameters:
executor- executor to execute the task concurrentlycallable- the task itself- Returns:
Promisefor the given task
-
ofBlockingRunnable
@NotNull static @NotNull Promise<Void> ofBlockingRunnable(@NotNull @NotNull Executor executor, @NotNull @NotNull Promise.BlockingRunnable runnable)
Same asofBlockingCallable(Executor, BlockingCallable), but without a result (returnedPromiseis only a marker of completion).
-
promise
default Promise<T> promise()
- Specified by:
promisein interfacePromisable<T>
-
isComplete
@Contract(pure=true) default boolean isComplete()
-
isResult
@Contract(pure=true) boolean isResult()
-
isException
@Contract(pure=true) boolean isException()
-
getResult
@Contract(pure=true) T getResult()
-
getException
@Contract(pure=true) Throwable getException()
-
getTry
@Contract(pure=true) io.activej.common.collection.Try<T> getTry()
-
async
@Contract(pure=true) @NotNull @NotNull Promise<T> async()
Ensures thatPromisecompletes asynchronously: if thisPromiseis already completed, its completion will be posted to next eventloop tick. Otherwise, does nothing.
-
next
@Contract("_ -> param1") @NotNull <U,P extends Callback<? super T> & Promise<U>> @NotNull Promise<U> next(@NotNull P promise)Executes givenpromiseafter execution of thisPromisecompletes.- Type Parameters:
U- type of result- Parameters:
promise- given promise- Returns:
- subscribed
Promise
-
map
@Contract(pure=true) @NotNull <U> @NotNull Promise<U> map(@NotNull @NotNull Function<? super T,? extends U> fn)
Returns a newPromisewhich is executed with thisPromise's result as the argument to the provided function when thisPromisecompletes successfully.- Parameters:
fn- function to be applied to thisPromisewhen it completes successfully- Returns:
- new
Promisewhich is the result of function applied to the result of thisPromise - See Also:
CompletionStage.thenApply(Function)
-
mapEx
@Contract(pure=true) @NotNull <U> @NotNull Promise<U> mapEx(@NotNull @NotNull BiFunction<? super T,@Nullable Throwable,? extends U> fn)
Returns a newPromisewhich is executed with thisPromise's result as the argument to the provided function when thisPromisecompletes either successfully (whenexceptionisnull) or with an exception.- Parameters:
fn- function to be applied to thisPromisewhen it completes either successfully or with an exception- Returns:
- new
Promisewhich is the result of function applied to the result of thisPromise
-
then
@Contract(pure=true) @NotNull <U> @NotNull Promise<U> then(@NotNull @NotNull Function<? super T,? extends Promise<? extends U>> fn)
Returns a newPromisewhich, when thisPromisecompletes successfully, is executed with thisPromise'sresult as the argument to the supplied function.- Parameters:
fn- to be applied
-
then
@NotNull <U> @NotNull Promise<U> then(@NotNull @NotNull Supplier<? extends Promise<? extends U>> fn)
-
thenEx
@Contract(pure=true) @NotNull <U> @NotNull Promise<U> thenEx(@NotNull @NotNull BiFunction<? super T,@Nullable Throwable,? extends Promise<? extends U>> fn)
Returns a newPromisewhich, when thisPromisecompletes either successfully (if exception isnull) or exceptionally (if exception is notnull), is executed with thisPromise'sresult as the argument to the supplied function.- Parameters:
fn- to be applied to the result of thisPromise- Returns:
- new
Promise
-
whenComplete
@Contract(" _ -> this") @NotNull @NotNull Promise<T> whenComplete(@NotNull @NotNull Callback<? super T> action)Subscribes given action to be executed after thisPromisecompletes and returns a newPromise.- Parameters:
action- to be executed
-
whenComplete
@Contract(" _ -> this") @NotNull @NotNull Promise<T> whenComplete(@NotNull @NotNull Runnable action)Subscribes given action to be executed after thisPromisecompletes and returns a newPromise.- Parameters:
action- to be executed
-
whenResult
@Contract(" _ -> this") @NotNull @NotNull Promise<T> whenResult(Consumer<? super T> action)Subscribes given action to be executed after thisPromisecompletes successfully and returns a newPromise.- Parameters:
action- to be executed
-
whenException
@Contract("_ -> this") Promise<T> whenException(@NotNull @NotNull Consumer<Throwable> action)Subscribes given action to be executed after thisPromisecompletes exceptionally and returns a newPromise.- Parameters:
action- to be executed
-
combine
@Contract(pure=true) @NotNull <U,V> @NotNull Promise<V> combine(@NotNull @NotNull Promise<? extends U> other, @NotNull @NotNull BiFunction<? 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
@Contract(pure=true) @NotNull @NotNull Promise<Void> both(@NotNull @NotNull Promise<?> other)
Returns a newPromisewhen both this and providedotherPromisescomplete.- Parameters:
other- the otherPromise- Returns:
Promiseofnullwhen both this and otherPromisecomplete
-
either
@Contract(pure=true) @NotNull @NotNull Promise<T> either(@NotNull @NotNull Promise<? extends T> other)
Returns thePromisewhich was completed first.- Parameters:
other- the otherPromise- Returns:
- the first completed
Promise
-
toTry
@Contract(pure=true) @NotNull @NotNull Promise<io.activej.common.collection.Try<T>> toTry()
ReturnsPromisethat always completes successfully with result or exception wrapped inTry.
-
toVoid
@Contract(pure=true) @NotNull @NotNull Promise<Void> toVoid()
Waits for result and discards it.
-
run
default void run(@NotNull @NotNull Callback<? super T> action)- Specified by:
runin interfaceAsyncComputation<T>
-
toCompletableFuture
@Contract(pure=true) @NotNull @NotNull CompletableFuture<T> toCompletableFuture()
WrapsPromiseintoCompletableFuture.
-
-