Package javascalautils.concurrent
Interface Promise<T>
-
- Type Parameters:
T- The type this Promise will produce as result
public interface Promise<T>The Promise is the promise to deliver a value at some time in the future.
This is the handle the actual computation side of of the job uses.
Once a job is finished it will report the outcome to the Promise which in turn relays it to the Future the client/application is monitoring.
A promise can be fulfilled either by invoking success or failure but not both.
Nor cansuccess/failurebe invoked twice.
The principle is that aPromisewill deliver exactly one successful or failure response.
The successful response is of any type whilst the failure is expected to be of type (or subclass of)Throwable.
Together with aFuturethis allows a safe publication of asynchronously calculated results into another thread.
The basic principle is to first create aPromise.
Promise<String> promise = Promise.apply();
Using that instance one can get hold of theFuturethat is the container for the value-to-be.
Future<String> future = promise.future();
To complete the Promise and by extension completing the Future one can use any of several methods: E.g.promise.success("Peter was here");
Note that only one of the methods may be invoked as the Promise can only be fulfilled once.
The above methods come with a variant tryNNN which allows for multiple invocations without raising an exception.
Though still only the first invocation/completion counts.- Since:
- 1.2
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static <T> Promise<T>apply()Creates an instance of Promise.voidcomplete(Try<T> result)voidcompleteWith(Future<T> future)voidfailure(java.lang.Throwable throwable)Completes thePromisewith an exception.Future<T>future()Get aFuturethat will hold the value once this Promise is completed.booleanisCompleted()Check if thePromisehave been completed, with a value or an exception.voidsuccess(T result)Completes thePromisewith a value.booleantryComplete(Try<T> result)booleantryCompleteWith(Future<T> future)booleantryFailure(java.lang.Throwable throwable)Tries to complete thePromisewith an exception.booleantrySuccess(T result)Tries to complete thePromisewith a value.
-
-
-
Method Detail
-
apply
static <T> Promise<T> apply()
Creates an instance of Promise.
E.g.Promise<String> p = Promise.apply();- Type Parameters:
T- The type this Promise will produce as result- Returns:
- The Promise instance
- Since:
- 1.2
-
future
Future<T> future()
Get aFuturethat will hold the value once this Promise is completed.
EachPromiseis connected to a singleFuture, invoking this method multiple times will always return the sameFutureinstance.- Returns:
- A Future that will hold the value once this Promise is completed.
- Since:
- 1.2
-
isCompleted
boolean isCompleted()
Check if thePromisehave been completed, with a value or an exception.- Returns:
trueif thePromisehas been completed.falseotherwise.- Since:
- 1.2
-
complete
void complete(Try<T> result)
- Parameters:
result- The result to complete with.- Throws:
java.lang.IllegalStateException- Thrown if the Promise is already completed.- Since:
- 1.3
-
completeWith
void completeWith(Future<T> future)
- Parameters:
future- The future whose value will complete this Promise- Throws:
java.lang.IllegalStateException- Thrown if the Promise is already completed.- Since:
- 1.3
-
success
void success(T result)
Completes thePromisewith a value.- Parameters:
result- The value to complete with.- Throws:
java.lang.IllegalStateException- Thrown if the Promise is already completed.- Since:
- 1.2
-
failure
void failure(java.lang.Throwable throwable)
Completes thePromisewith an exception.- Parameters:
throwable- The Throwable to complete with.- Throws:
java.lang.IllegalStateException- Thrown if the Promise is already completed.- Since:
- 1.2
-
tryComplete
boolean tryComplete(Try<T> result)
Tries to complete thePromisewith either aSuccessor aFailure.
Contrary to thecomplete(Try)method this does not throw an exception in case the Promise is already completed.- Parameters:
result- The result to complete with.- Returns:
trueif the Promise was not completed before,falseotherwise- Since:
- 1.3
-
tryCompleteWith
boolean tryCompleteWith(Future<T> future)
Tries to complete thisPromisewith the value from the providedFutureonce that is completed.
Contrary to thecomplete(Try)method this does not throw an exception in case the Promise is already completed.- Parameters:
future- The future whose value will complete this Promise- Returns:
trueif the Promise was not completed before,falseotherwise- Since:
- 1.3
-
trySuccess
boolean trySuccess(T result)
Tries to complete thePromisewith a value.
Contrary to thesuccess(Object)method this does not throw an exception in case the Promise is already completed.- Parameters:
result- The value to complete with.- Returns:
trueif the Promise was not completed before,falseotherwise- Since:
- 1.3
-
tryFailure
boolean tryFailure(java.lang.Throwable throwable)
Tries to complete thePromisewith an exception.
Contrary to thefailure(Throwable)method this does not throw an exception in case the Promise is already completed.- Parameters:
throwable- The Throwable to complete with.- Returns:
trueif the Promise was not completed before,falseotherwise- Since:
- 1.3
-
-