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
/failure
be invoked twice.
The principle is that aPromise
will 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 aFuture
this 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 theFuture
that 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.void
complete(Try<T> result)
void
completeWith(Future<T> future)
void
failure(java.lang.Throwable throwable)
Completes thePromise
with an exception.Future<T>
future()
Get aFuture
that will hold the value once this Promise is completed.boolean
isCompleted()
Check if thePromise
have been completed, with a value or an exception.void
success(T result)
Completes thePromise
with a value.boolean
tryComplete(Try<T> result)
boolean
tryCompleteWith(Future<T> future)
boolean
tryFailure(java.lang.Throwable throwable)
Tries to complete thePromise
with an exception.boolean
trySuccess(T result)
Tries to complete thePromise
with 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 aFuture
that will hold the value once this Promise is completed.
EachPromise
is connected to a singleFuture
, invoking this method multiple times will always return the sameFuture
instance.- Returns:
- A Future that will hold the value once this Promise is completed.
- Since:
- 1.2
-
isCompleted
boolean isCompleted()
Check if thePromise
have been completed, with a value or an exception.- Returns:
true
if thePromise
has been completed.false
otherwise.- 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 thePromise
with 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 thePromise
with 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 thePromise
with either aSuccess
or 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:
true
if the Promise was not completed before,false
otherwise- Since:
- 1.3
-
tryCompleteWith
boolean tryCompleteWith(Future<T> future)
Tries to complete thisPromise
with the value from the providedFuture
once 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:
true
if the Promise was not completed before,false
otherwise- Since:
- 1.3
-
trySuccess
boolean trySuccess(T result)
Tries to complete thePromise
with 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:
true
if the Promise was not completed before,false
otherwise- Since:
- 1.3
-
tryFailure
boolean tryFailure(java.lang.Throwable throwable)
Tries to complete thePromise
with 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:
true
if the Promise was not completed before,false
otherwise- Since:
- 1.3
-
-