Package io.activej.promise
Class SettablePromise<T>
- java.lang.Object
-
- io.activej.promise.SettablePromise<T>
-
- Type Parameters:
T- result type
- All Implemented Interfaces:
AsyncComputation<T>,Callback<T>,Promisable<T>,Promise<T>
public final class SettablePromise<T> extends Object implements Callback<T>
Represents aPromisewhich can be completed or completedExceptionally manually at once or later in the future.Can be used as root
Promiseto start execution of chain ofPromisesor when you want wrap your actions inPromise.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.activej.promise.Promise
Promise.BlockingCallable<V>, Promise.BlockingRunnable
-
-
Constructor Summary
Constructors Constructor Description SettablePromise()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccept(T result, @Nullable Throwable e)Accepts the provided values and performs this operation on them.@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.protected voidcomplete(T value)protected voidcomplete(T value, @Nullable Throwable e)protected voidcompleteExceptionally(@Nullable Throwable e)Stringdescribe()@NotNull Promise<T>either(@NotNull Promise<? extends T> other)Returns thePromisewhich was completed first.ThrowablegetException()TgetResult()io.activej.common.collection.Try<T>getTry()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,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.voidpost(T result)voidpost(T result, @Nullable Throwable e)voidpostException(@NotNull Throwable e)protected static @Nullable VoidrecycleToVoid(Object item)voidreset()voidresetCallbacks()voidset(T result)Sets the result of thisSettablePromiseand completes it.voidsetException(@NotNull Throwable e)Sets exception and completes thisSettablePromiseexceptionally.protected voidsubscribe(@NotNull Callback<? super T> callback)<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,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.StringtoString()@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.protected booleantryComplete(T value)protected booleantryComplete(T value, @Nullable Throwable e)protected booleantryCompleteExceptionally(@NotNull Throwable e)voidtryPost(T result)voidtryPost(T result, @Nullable Throwable e)voidtryPostException(@NotNull Throwable e)booleantrySet(T result)Tries to set providedresultfor thisSettablePromiseif it is not completed yet.booleantrySet(T result, @Nullable Throwable e)Tries to set result or exception for thisSettablePromiseif it not completed yet.booleantrySetException(@NotNull Throwable e)Tries to set providedeexception for thisSettablePromiseif it is not completed yet.@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
-
accept
public void accept(T result, @Nullable @Nullable Throwable e)
Accepts the provided values and performs this operation on them. If theThrowable eisnull, providedresultwill be set to thisSettablePromise.Otherwise,
Throwable ewill be set.
-
set
public void set(T result)
Sets the result of thisSettablePromiseand completes it.AssertionErroris thrown when you try to set result for an already completedPromise.
-
setException
public void setException(@NotNull @NotNull Throwable e)Sets exception and completes thisSettablePromiseexceptionally.AssertionErroris thrown when you try to set exception for an already completedPromise.- Parameters:
e- exception
-
trySet
public boolean trySet(T result)
Tries to set providedresultfor thisSettablePromiseif it is not completed yet. Otherwise does nothing.
-
trySetException
public boolean trySetException(@NotNull @NotNull Throwable e)Tries to set providedeexception for thisSettablePromiseif it is not completed yet. Otherwise does nothing.
-
trySet
public boolean trySet(T result, @Nullable @Nullable Throwable e)
Tries to set result or exception for thisSettablePromiseif it not completed yet. Otherwise does nothing.
-
post
public void post(T result)
-
postException
public void postException(@NotNull @NotNull Throwable e)
-
tryPost
public void tryPost(T result)
-
tryPostException
public void tryPostException(@NotNull @NotNull Throwable e)
-
describe
public String describe()
-
reset
public void reset()
-
resetCallbacks
public void resetCallbacks()
-
isComplete
public final boolean isComplete()
- Specified by:
isCompletein interfacePromise<T>
-
isException
public final boolean isException()
- Specified by:
isExceptionin interfacePromise<T>
-
getException
public Throwable getException()
- Specified by:
getExceptionin interfacePromise<T>
-
getTry
public io.activej.common.collection.Try<T> getTry()
-
complete
protected void complete(@Nullable T value, @Nullable @Nullable Throwable e)
-
complete
protected void complete(@Nullable T value)
-
completeExceptionally
protected void completeExceptionally(@Nullable @Nullable Throwable e)
-
tryComplete
protected boolean tryComplete(@Nullable T value, @Nullable @Nullable Throwable e)
-
tryComplete
protected boolean tryComplete(@Nullable T value)
-
tryCompleteExceptionally
protected boolean tryCompleteExceptionally(@NotNull @NotNull Throwable e)
-
next
@NotNull public <U,P extends Callback<? super T> & Promise<U>> @NotNull Promise<U> next(@NotNull P promise)
Description copied from interface:PromiseExecutes givenpromiseafter execution of thisPromisecompletes.
-
subscribe
protected void subscribe(@NotNull @NotNull Callback<? super T> callback)
-
map
@NotNull public <U> @NotNull Promise<U> map(@NotNull @NotNull Function<? super T,? extends U> fn)
Description copied from interface:PromiseReturns a newPromisewhich is executed with thisPromise's result as the argument to the provided function when thisPromisecompletes successfully.- Specified by:
mapin interfacePromise<T>- 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
@NotNull public <U> @NotNull Promise<U> mapEx(@NotNull @NotNull BiFunction<? super T,Throwable,? extends U> fn)
Description copied from interface:PromiseReturns a newPromisewhich is executed with thisPromise's result as the argument to the provided function when thisPromisecompletes either successfully (whenexceptionisnull) or with an exception.
-
then
@NotNull public <U> @NotNull Promise<U> then(@NotNull @NotNull Function<? super T,? extends Promise<? extends U>> fn)
Description copied from interface:PromiseReturns a newPromisewhich, when thisPromisecompletes successfully, is executed with thisPromise'sresult as the argument to the supplied function.
-
then
@NotNull public <U> @NotNull Promise<U> then(@NotNull @NotNull Supplier<? extends Promise<? extends U>> fn)
-
thenEx
@NotNull public <U> @NotNull Promise<U> thenEx(@NotNull @NotNull BiFunction<? super T,Throwable,? extends Promise<? extends U>> fn)
Description copied from interface:PromiseReturns 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.
-
whenComplete
@NotNull public @NotNull Promise<T> whenComplete(@NotNull @NotNull Callback<? super T> action)
Description copied from interface:PromiseSubscribes given action to be executed after thisPromisecompletes and returns a newPromise.- Specified by:
whenCompletein interfacePromise<T>- Parameters:
action- to be executed
-
whenComplete
@NotNull public @NotNull Promise<T> whenComplete(@NotNull @NotNull Runnable action)
Description copied from interface:PromiseSubscribes given action to be executed after thisPromisecompletes and returns a newPromise.- Specified by:
whenCompletein interfacePromise<T>- Parameters:
action- to be executed
-
whenResult
@NotNull public @NotNull Promise<T> whenResult(Consumer<? super T> action)
Description copied from interface:PromiseSubscribes given action to be executed after thisPromisecompletes successfully and returns a newPromise.- Specified by:
whenResultin interfacePromise<T>- Parameters:
action- to be executed
-
whenResult
public Promise<T> whenResult(@NotNull @NotNull Runnable action)
- Specified by:
whenResultin interfacePromise<T>
-
whenException
public Promise<T> whenException(@NotNull @NotNull Consumer<Throwable> action)
Description copied from interface:PromiseSubscribes given action to be executed after thisPromisecompletes exceptionally and returns a newPromise.- Specified by:
whenExceptionin interfacePromise<T>- Parameters:
action- to be executed
-
whenException
public Promise<T> whenException(@NotNull @NotNull Runnable action)
- Specified by:
whenExceptionin interfacePromise<T>
-
async
@NotNull public @NotNull Promise<T> async()
Description copied from interface:PromiseEnsures thatPromisecompletes asynchronously: if thisPromiseis already completed, its completion will be posted to next eventloop tick. Otherwise, does nothing.
-
combine
@NotNull public <U,V> @NotNull Promise<V> combine(@NotNull @NotNull Promise<? extends U> other, @NotNull @NotNull BiFunction<? super T,? super U,? extends V> fn)
Description copied from interface:PromiseReturns a newPromisethat, when this and the other givenPromiseboth complete, is executed with the two results as arguments to the supplied function.
-
both
@NotNull public @NotNull Promise<Void> both(@NotNull @NotNull Promise<?> other)
Description copied from interface:PromiseReturns a newPromisewhen both this and providedotherPromisescomplete.
-
either
@NotNull public @NotNull Promise<T> either(@NotNull @NotNull Promise<? extends T> other)
Description copied from interface:PromiseReturns thePromisewhich was completed first.
-
toTry
@NotNull public @NotNull Promise<io.activej.common.collection.Try<T>> toTry()
Description copied from interface:PromiseReturnsPromisethat always completes successfully with result or exception wrapped inTry.
-
toVoid
@NotNull public @NotNull Promise<Void> toVoid()
Description copied from interface:PromiseWaits for result and discards it.
-
toCompletableFuture
@NotNull public @NotNull CompletableFuture<T> toCompletableFuture()
Description copied from interface:PromiseWrapsPromiseintoCompletableFuture.- Specified by:
toCompletableFuturein interfacePromise<T>
-
-