Package io.vavr.concurrent
Interface Promise<T>
-
- Type Parameters:
T- the type of the value that completes the underlyingFuture
- All Known Implementing Classes:
PromiseImpl
public interface Promise<T>APromiseis a write-once container for a read-onlyFuture, allowing the underlyingFutureto be completed with a value or an exception.The associated
Executoris used to run asynchronous handlers, for example, viapromise.future().onComplete(...).Creation
Promiseprovides static factory methods to create new promises:- Unfulfilled promises:
make() - Already completed promises:
Executor, allowing finer control over thread usage and thread pool configuration.One-shot API
When a single
Threadis responsible for completing thePromise, use one of the following methods. Calls will throw an exception if thePromisehas already been completed:API for concurrent completion
When multiple
Threads may attempt to complete thePromise, use one of the following "try" methods. Calls will 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(@NonNull Try<? extends T> value)Completes thisPromisewith the givenvalue.default Promise<T>completeWith(@NonNull Future<? extends T> other)Completes thisPromisewith the result of the givenFutureonce it is 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(@NonNull java.lang.Throwable exception)Creates aPromisethat is already completed with a failure, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.static <T> Promise<T>failed(@NonNull java.util.concurrent.Executor executor, @NonNull java.lang.Throwable exception)Creates aPromisethat is already completed with a failure, using the specifiedExecutorfor asynchronous operations.default Promise<T>failure(@NonNull java.lang.Throwable exception)Completes thisPromisewith the given exception.static <T> Promise<T>fromTry(@NonNull Try<? extends T> result)Creates aPromisefrom the givenTry, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.static <T> Promise<T>fromTry(@NonNull java.util.concurrent.Executor executor, @NonNull Try<? extends T> result)Future<T>future()Returns the underlyingFutureassociated with thisPromise.default booleanisCompleted()Checks whether thisPromisehas been completed, either successfully or with a failure.static <T> Promise<T>make()Creates a newPromisethat is not yet completed, using theFuture.DEFAULT_EXECUTOR(typicallyForkJoinPool.commonPool()) for asynchronous operations.static <T> Promise<T>make(@NonNull java.util.concurrent.Executor executor)Creates a newPromisethat is not yet completed, using the specifiedExecutorfor asynchronous operations.static <T> Promise<T>narrow(Promise<? extends T> promise)Narrows aPromise<? extends T>toPromise<T>through a type-safe cast.default Promise<T>success(T value)Completes thisPromisewith the given value.static <T> Promise<T>successful(@NonNull java.util.concurrent.Executor executor, T result)Creates aPromisethat is already completed successfully, using the specifiedExecutorfor asynchronous operations.static <T> Promise<T>successful(T result)Creates aPromisethat is already completed successfully, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.booleantryComplete(@NonNull Try<? extends T> value)Attempts to complete thisPromisewith the givenvalue.default Promise<T>tryCompleteWith(@NonNull Future<? extends T> other)Attempts to complete thisPromisewith the result of the givenFutureonce it is completed.default booleantryFailure(@NonNull java.lang.Throwable exception)Attempts to complete thisPromisewith the given exception.default booleantrySuccess(T value)Attempts to complete thisPromisewith the given value.
-
-
-
Method Detail
-
failed
static <T> Promise<T> failed(@NonNull java.lang.Throwable exception)
Creates aPromisethat is already completed with a failure, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.- Type Parameters:
T- the type of the value that would have been returned on success- Parameters:
exception- the cause of the failure- Returns:
- a
Promisecompleted with the given failure - Throws:
java.lang.NullPointerException- ifexceptionis null
-
failed
static <T> Promise<T> failed(@NonNull java.util.concurrent.Executor executor, @NonNull java.lang.Throwable exception)
Creates aPromisethat is already completed with a failure, using the specifiedExecutorfor asynchronous operations.- Type Parameters:
T- the type of the value that would have been returned on success- Parameters:
executor- theExecutorused by the underlyingFutureexception- the cause of the failure- Returns:
- a
Promisecompleted with the given failure - Throws:
java.lang.NullPointerException- ifexecutororexceptionis null
-
fromTry
static <T> Promise<T> fromTry(@NonNull Try<? extends T> result)
Creates aPromisefrom the givenTry, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.- Type Parameters:
T- the type of the value in case of success- Parameters:
result- theTryrepresenting a success or failure- Returns:
- a
Promisealready completed with the givenTryresult - Throws:
java.lang.NullPointerException- ifresultis null
-
fromTry
static <T> Promise<T> fromTry(@NonNull java.util.concurrent.Executor executor, @NonNull Try<? extends T> result)
- Type Parameters:
T- the type of the value in case of success- Parameters:
executor- theExecutorused by the underlyingFutureresult- theTryrepresenting a success or failure- Returns:
- a
Promisealready completed with the givenTryresult - Throws:
java.lang.NullPointerException- ifexecutororresultis null
-
make
static <T> Promise<T> make()
Creates a newPromisethat is not yet completed, using theFuture.DEFAULT_EXECUTOR(typicallyForkJoinPool.commonPool()) for asynchronous operations.- Type Parameters:
T- the type of the value that will complete thePromise- Returns:
- a new, uncompleted
Promise
-
make
static <T> Promise<T> make(@NonNull java.util.concurrent.Executor executor)
Creates a newPromisethat is not yet completed, using the specifiedExecutorfor asynchronous operations.- Type Parameters:
T- the type of the value that will complete thePromise- Parameters:
executor- theExecutorused by the underlyingFuture- Returns:
- a new, uncompleted
Promise - Throws:
java.lang.NullPointerException- ifexecutoris null
-
narrow
static <T> Promise<T> narrow(Promise<? extends T> promise)
Narrows aPromise<? extends T>toPromise<T>through a type-safe cast. This is safe because immutable or read-only collections are covariant.- Type Parameters:
T- the component type of thePromise- Parameters:
promise- thePromiseto narrow- Returns:
- the same
promiseinstance, cast toPromise<T>
-
successful
static <T> Promise<T> successful(T result)
Creates aPromisethat is already completed successfully, using theFuture.DEFAULT_EXECUTORfor asynchronous operations.- Type Parameters:
T- the type of the value- Parameters:
result- the value of the successful result- Returns:
- a
Promisealready completed with the given result
-
successful
static <T> Promise<T> successful(@NonNull java.util.concurrent.Executor executor, T result)
Creates aPromisethat is already completed successfully, using the specifiedExecutorfor asynchronous operations.- Type Parameters:
T- the type of the value- Parameters:
executor- theExecutorused by the underlyingFutureresult- the value of the successful result- Returns:
- a
Promisealready completed with the given result - Throws:
java.lang.NullPointerException- ifexecutoris null
-
executor
default java.util.concurrent.Executor executor()
- Returns:
- the
Executorassociated with thisPromise
-
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.
-
future
Future<T> future()
Returns the underlyingFutureassociated with thisPromise.- Returns:
- the underlying
Future
-
isCompleted
default boolean isCompleted()
Checks whether thisPromisehas been completed, either successfully or with a failure.- Returns:
trueif thePromiseis completed,falseotherwise
-
complete
default Promise<T> complete(@NonNull Try<? extends T> value)
Completes thisPromisewith the givenvalue.- Parameters:
value- aTry.Successcontaining the result or aTry.Failurecontaining an exception- Returns:
- this
Promise - Throws:
java.lang.IllegalStateException- if thisPromisehas already been completed
-
tryComplete
boolean tryComplete(@NonNull Try<? extends T> value)
Attempts to complete thisPromisewith the givenvalue.- Parameters:
value- aTry.Successcontaining the result or aTry.Failurecontaining an exception- Returns:
trueif thePromisewas completed successfully,falseif it was already completed
-
completeWith
default Promise<T> completeWith(@NonNull Future<? extends T> other)
Completes thisPromisewith the result of the givenFutureonce it is completed.- Parameters:
other- theFuturewhose result or failure will complete thisPromise- Returns:
- this
Promise
-
tryCompleteWith
default Promise<T> tryCompleteWith(@NonNull Future<? extends T> other)
Attempts to complete thisPromisewith the result of the givenFutureonce it is completed.- Parameters:
other- theFuturewhose result or failure may complete thisPromise- Returns:
trueif thisPromisewas completed byother,falseif it was already completed
-
success
default Promise<T> success(T value)
Completes thisPromisewith the given value.- Parameters:
value- the value to complete thisPromisewith- Returns:
- this
Promise - Throws:
java.lang.IllegalStateException- if thisPromisehas already been completed
-
trySuccess
default boolean trySuccess(T value)
Attempts to complete thisPromisewith the given value.- Parameters:
value- the value to complete thisPromisewith- Returns:
trueif thePromisewas completed successfully,falseif it was already completed
-
failure
default Promise<T> failure(@NonNull java.lang.Throwable exception)
Completes thisPromisewith the given exception.- Parameters:
exception- the exception to complete thisPromisewith- Returns:
- this
Promise - Throws:
java.lang.IllegalStateException- if thisPromisehas already been completed
-
tryFailure
default boolean tryFailure(@NonNull java.lang.Throwable exception)
Attempts to complete thisPromisewith the given exception.- Parameters:
exception- the exception to complete thisPromisewith- Returns:
trueif thePromisewas completed successfully,falseif it was already completed
-
-