Interface Promise<T>
-
- Type Parameters:
T- The result type of the underlyingFuture.
- All Known Implementing Classes:
PromiseImpl
public interface Promise<T>A Promise is a write-once wrapper around a read-only Future which can complete the underlying Future with a value or an exception.The underlying
Executoris used to execute asynchronous handlers, e.g. viapromise.future().onComplete(...).Creation
Promise offers static factory methods to create new promises which hasn't been fulfilled yet:
- create new promises:
make()
Executoras argument. This gives us more control over thread creation and thread pool sizes.One-shot API
The main purpose of a
Promiseis to complete its underlyingFuture. When only a singleThreadwill eventually complete thePromise, we use one of these methods. Calls will throw if thePromiseis already completed.API for competing threads
When multiple
Threads may complete ourPromise, we typically use one of these methods. Calls will gracefully returnfalseif thePromiseis already completed.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default Promise<T>complete(Try<? extends T> value)Completes thisPromisewith the givenvalue.default Promise<T>completeWith(Future<? extends T> other)Completes thisPromisewith the givenFuture, once thatFutureis completed.default java.util.concurrent.Executorexecutor()java.util.concurrent.ExecutorServiceexecutorService()Deprecated.Removed starting with Vavr 0.10.0, useexecutor()instead.static <T> Promise<T>failed(java.lang.Throwable exception)Creates a failedPromise, backed by theFuture.DEFAULT_EXECUTOR.static <T> Promise<T>failed(java.util.concurrent.Executor executor, java.lang.Throwable exception)Creates a failedPromise, backed by the givenExecutor.default Promise<T>failure(java.lang.Throwable exception)Completes thisPromisewith the givenexception.static <T> Promise<T>fromTry(Try<? extends T> result)static <T> Promise<T>fromTry(java.util.concurrent.Executor executor, Try<? extends T> result)Future<T>future()Returns the underlyingFutureof thisPromise.default booleanisCompleted()Checks if thisPromiseis completed, i.e.static <T> Promise<T>make()Makes aPromisethat isn't fulfilled yet, backed by theFuture.DEFAULT_EXECUTOR.static <T> Promise<T>make(java.util.concurrent.Executor executor)Makes aPromisethat isn't fulfilled yet, backed by the givenExecutor.static <T> Promise<T>narrow(Promise<? extends T> promise)Narrows a widenedPromise<? extends T>toPromise<T>by performing a type-safe cast.default Promise<T>success(T value)Completes thisPromisewith the givenvalue.static <T> Promise<T>successful(java.util.concurrent.Executor executor, T result)Creates a succeededPromise, backed by the givenExecutor.static <T> Promise<T>successful(T result)Creates a succeededPromise, backed by theFuture.DEFAULT_EXECUTOR.booleantryComplete(Try<? extends T> value)Attempts to completes thisPromisewith the givenvalue.default Promise<T>tryCompleteWith(Future<? extends T> other)Attempts to complete thisPromisewith the specifiedFuture, once thatFutureis completed.default booleantryFailure(java.lang.Throwable exception)Completes thisPromisewith the givenexception.default booleantrySuccess(T value)Completes thisPromisewith the givenvalue.
-
-
-
Method Detail
-
failed
static <T> Promise<T> failed(java.lang.Throwable exception)
Creates a failedPromise, backed by theFuture.DEFAULT_EXECUTOR.- Type Parameters:
T- The value type of a successful result.- Parameters:
exception- The reason why it failed.- Returns:
- A failed
Promise. - Throws:
java.lang.NullPointerException- if exception is null
-
failed
static <T> Promise<T> failed(java.util.concurrent.Executor executor, java.lang.Throwable exception)
Creates a failedPromise, backed by the givenExecutor.- Type Parameters:
T- The value type of a successful result.- Parameters:
executor- AnExecutorpassed to the underlyingFuture.exception- The reason why it failed.- Returns:
- A failed
Promise. - Throws:
java.lang.NullPointerException- if executor or exception is null
-
fromTry
static <T> Promise<T> fromTry(Try<? extends T> result)
- Type Parameters:
T- The value type of a successful result.- Parameters:
result- The result.- Returns:
- A completed
Promisewhich contains either aSuccessor aFailure. - Throws:
java.lang.NullPointerException- if result is null
-
fromTry
static <T> Promise<T> fromTry(java.util.concurrent.Executor executor, Try<? extends T> result)
- Type Parameters:
T- The value type of a successful result.- Parameters:
executor- AnExecutorpassed to the underlyingFuture.result- The result.- Returns:
- A completed
Promisewhich contains either aSuccessor aFailure. - Throws:
java.lang.NullPointerException- if executor or result is null
-
make
static <T> Promise<T> make()
Makes aPromisethat isn't fulfilled yet, backed by theFuture.DEFAULT_EXECUTOR.ForkJoinPool.commonPool().- Type Parameters:
T- Result type of thePromise.- Returns:
- A new
Promise.
-
make
static <T> Promise<T> make(java.util.concurrent.Executor executor)
Makes aPromisethat isn't fulfilled yet, backed by the givenExecutor.- Type Parameters:
T- Result type of thePromise.- Parameters:
executor- AnExecutorpassed to the underlyingFuture.- Returns:
- A new
Promise. - Throws:
java.lang.NullPointerException- if executor is null
-
narrow
static <T> Promise<T> narrow(Promise<? extends T> promise)
Narrows a widenedPromise<? extends T>toPromise<T>by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
T- Component type of thePromise.- Parameters:
promise- APromise.- Returns:
- the given
promiseinstance as narrowed typePromise<T>.
-
successful
static <T> Promise<T> successful(T result)
Creates a succeededPromise, backed by theFuture.DEFAULT_EXECUTOR.- Type Parameters:
T- The value type of a successful result.- Parameters:
result- The result.- Returns:
- A succeeded
Promise.
-
successful
static <T> Promise<T> successful(java.util.concurrent.Executor executor, T result)
Creates a succeededPromise, backed by the givenExecutor.- Type Parameters:
T- The value type of a successful result.- Parameters:
executor- AnExecutorpassed to the underlyingFuture.result- The result.- Returns:
- A succeeded
Promise. - Throws:
java.lang.NullPointerException- if executor is null
-
executor
default java.util.concurrent.Executor executor()
- Returns:
- The underlying
Executor.
-
executorService
@Deprecated java.util.concurrent.ExecutorService executorService()
Deprecated.Removed starting with Vavr 0.10.0, useexecutor()instead.This method is deprecated.THE DEFAULT IMPLEMENTATION (obtained by one of the
Promisefactory methods) MIGHT THROW ANUnsupportedOperationExceptionAT RUNTIME, DEPENDING ON WHATFuture.executorService()returns.- Returns:
- (never)
- Throws:
java.lang.UnsupportedOperationException- if the underlyingExecutorisn't anExecutorService.
-
isCompleted
default boolean isCompleted()
Checks if thisPromiseis completed, i.e. has a value.- Returns:
- true, if the computation successfully finished or failed, false otherwise.
-
complete
default Promise<T> complete(Try<? extends T> value)
Completes thisPromisewith the givenvalue.- Parameters:
value- Either aTry.Successcontaining the result or aTry.Failurecontaining an exception.- Returns:
- This
Promise. - Throws:
java.lang.IllegalStateException- if thisPromisehas already been completed.
-
tryComplete
boolean tryComplete(Try<? extends T> value)
Attempts to completes thisPromisewith the givenvalue.- Parameters:
value- Either aTry.Successcontaining the result or aTry.Failurecontaining an exception.- Returns:
falseif thisPromisehas already been completed,trueotherwise.- Throws:
java.lang.IllegalStateException- if thisPromisehas already been completed.
-
completeWith
default Promise<T> completeWith(Future<? extends T> other)
Completes thisPromisewith the givenFuture, once thatFutureis completed.- Parameters:
other- AnotherFutureto react on.- Returns:
- This
Promise.
-
tryCompleteWith
default Promise<T> tryCompleteWith(Future<? extends T> other)
Attempts to complete thisPromisewith the specifiedFuture, once thatFutureis completed.- Parameters:
other- AnotherFutureto react on.- Returns:
- This
Promise.
-
success
default Promise<T> success(T value)
Completes thisPromisewith the givenvalue.- Parameters:
value- A value.- Returns:
- This
Promise. - Throws:
java.lang.IllegalStateException- if thisPromisehas already been completed.
-
trySuccess
default boolean trySuccess(T value)
Completes thisPromisewith the givenvalue.- Parameters:
value- A value.- Returns:
falseif thisPromisehas already been completed,trueotherwise.
-
failure
default Promise<T> failure(java.lang.Throwable exception)
Completes thisPromisewith the givenexception.- Parameters:
exception- An exception.- Returns:
- This
Promise. - Throws:
java.lang.IllegalStateException- if thisPromisehas already been completed.
-
tryFailure
default boolean tryFailure(java.lang.Throwable exception)
Completes thisPromisewith the givenexception.- Parameters:
exception- An exception.- Returns:
falseif thisPromisehas already been completed,trueotherwise.
-
-