T - the type of value that can be setpublic class ReadyPartialFuture<T> extends Object implements PartialFuture<T>
PartialFuture that is always in the "set" state. No call on a ReadyPartialFuture
will block, since this is either a value or an error from its time of creation. A
ReadyPartialFuture is useful for returning the result of an asynchronous process.Future<Void> to indicate completion, ReadyFuture.DONE
should be used.| Constructor and Description |
|---|
ReadyPartialFuture(Error error)
Create a new
ReadyFuture set to an error. |
ReadyPartialFuture(Error error,
Executor executor)
Create a new
ReadyFuture set to an error. |
ReadyPartialFuture(Exception error)
Create a new
ReadyPartialFuture set to an error. |
ReadyPartialFuture(Exception error,
Executor executor)
Create a new
ReadyFuture set to an error. |
ReadyPartialFuture(T value)
Create a new
ReadyFuture set to a value. |
ReadyPartialFuture(T value,
Executor executor)
Create a new
ReadyPartialFuture set to a value. |
| Modifier and Type | Method and Description |
|---|---|
void |
blockInterruptibly()
Blocks until this
Future is set to either a value or an error. |
void |
blockUntilReady()
Always returns immediately.
|
void |
cancel()
Does nothing, as a
ReadyPartialFuture cannot be cancelled. |
void |
dispose()
Equivalent to calling
PartialFuture.cancel(). |
<V> PartialFuture<V> |
flatMap(PartialFunction<? super T,? extends PartialFuture<V>> m)
Applies the successful result of this operation as the input into another asynchronous
operation and returns a handle to that result.
|
T |
get()
Blocks until a value is set on this
PartialFuture and returns it. |
Throwable |
getError()
Gets the error from a
Future if it has been set to this state. |
T |
getInterruptibly()
Blocks until a value is set on this
PartialFuture and returns it. |
boolean |
isDone()
A
ReadyFuture is always done. |
boolean |
isError()
Tests if this
Future is set to an error |
<V> PartialFuture<V> |
map(PartialFunction<? super T,V> m)
Apply a function to the successful result of this operation.
|
void |
onReady(Runnable r)
Registers a
Runnable to run as the callback for this Future
on a default thread. |
boolean |
onReadyAlready(Runnable r)
Checks if this
Future is ready and, if not, atomically registers a
Runnable to run as the callback for this future on a default thread. |
Future<T> |
rescue(Function<? super Exception,Future<T>> m)
Convert an error output of this operation into another asynchronous process.
|
PartialFuture<T> |
rescue(PartialFunction<? super Exception,? extends PartialFuture<T>> m)
Map any error result of this
Future to another asynchronous process. |
public ReadyPartialFuture(T value)
ReadyFuture set to a value.value - the final value of this Futurepublic ReadyPartialFuture(Error error)
ReadyFuture set to an error. If error is null the
Future will be set to a null value and NOT an error.error - the final error of this Futurepublic ReadyPartialFuture(Exception error)
ReadyPartialFuture set to an error. If error is
null the Future will be set to a null value and
NOT an error.error - the final error of this Futurepublic ReadyPartialFuture(T value, Executor executor)
ReadyPartialFuture set to a value.value - the final value of this Futureexecutor - the executor with which to execute callbackspublic ReadyPartialFuture(Error error, Executor executor)
ReadyFuture set to an error. If error is null the
Future will be set to a null value and NOT an error.error - the final error of this Futureexecutor - the executor with which to execute callbackspublic ReadyPartialFuture(Exception error, Executor executor)
ReadyFuture set to an error. If error is null the
Future will be set to a null value and NOT an error.error - the final error of this Futureexecutor - the executor with which to execute callbackspublic void cancel()
ReadyPartialFuture cannot be cancelled.cancel in interface Cancellablecancel in interface PartialFuture<T>public void onReady(Runnable r)
PartialFutureRunnable to run as the callback for this Future
on a default thread. It will be called after the future becomes ready, including
if the future is set to a value, set to an error, or cancelled.PartialFuture.map(PartialFunction) and PartialFuture.flatMap(PartialFunction)
instead, as they automatically provide error propagation and cancellation.onReady in interface PartialFuture<T>r - the code to run when the future is set either to value or errorpublic boolean onReadyAlready(Runnable r)
PartialFutureFuture is ready and, if not, atomically registers a
Runnable to run as the callback for this future on a default thread. The
callback will not be called if the future is ready. Callers can rely on the guarantee
that if this function returns false their callback will be executed on
completion (including if the future is set to a value, set to an error, or cancelled),
and if true they can then call PartialFuture.get() without blocking.onReadyAlready in interface PartialFuture<T>r - the code to run when the future is set if it is not currently readytrue if the future is ready, false otherwisepublic <V> PartialFuture<V> flatMap(PartialFunction<? super T,? extends PartialFuture<V>> m)
PartialFutureFuture is complete and a result
is returned, the Mapper m is invoked to begin a new asynchronous process
using the result.
Errors in the execution of either operation are sent to the resulting Future.Future is cancelled the input
input Future will also be cancelled.flatMap in interface PartialFuture<T>m - the map to the asynchronous process to be runFuture that passes errors and converts output of this Future
to the output of another asynchronous process.public <V> PartialFuture<V> map(PartialFunction<? super T,V> m)
PartialFutureFuture.Future is cancelled the input
input Future will also be cancelled.map in interface PartialFuture<T>m - the map to the conversion to be runFuture that passes errors and converts output of this Future.public Future<T> rescue(Function<? super Exception,Future<T>> m)
PartialFuturerescue in interface PartialFuture<T>m - the process to run on completion into error state.Future output with modified error behaviorpublic PartialFuture<T> rescue(PartialFunction<? super Exception,? extends PartialFuture<T>> m)
PartialFutureFuture to another asynchronous process. This can be
used when an error is expected or can be logically dealt with in some way. If the
rescue process cannot handle the error, or if the rescue process itself throws an error,
that error will be set on this Future. If the result
of this call is used in place of this Future, the user of the output
Future
will see successes of this process, successes of the rescue process, or errors from
the rescue process.rescue in interface PartialFuture<T>m - the process to run that maps an error into another asynchronous operationFuture with modified error behaviorpublic void blockUntilReady()
blockUntilReady in interface PartialFuture<T>public void blockInterruptibly()
PartialFutureFuture is set to either a value or an error.
When this function returns without an error, PartialFuture.isDone() will return
true.
blockInterruptibly in interface PartialFuture<T>public T get() throws Exception
PartialFuturePartialFuture and returns it.
If this PartialFuture is set to an Exception or Error
this call will throw that error.get in interface PartialFuture<T>Exception - if the process was not successfully executed.InterruptedException - if the blocked thread is interruptedpublic T getInterruptibly() throws Exception
PartialFuturePartialFuture and returns it.
If this PartialFuture is set to an Exception or Error
this call will throw that error.getInterruptibly in interface PartialFuture<T>Exception - if the process was not successfully executed.InterruptedException - if the blocked thread is interruptedpublic boolean isDone()
ReadyFuture is always done.isDone in interface PartialFuture<T>true.public boolean isError()
PartialFutureFuture is set to an errorisError in interface PartialFuture<T>true if in error state, false otherwisepublic Throwable getError()
PartialFutureFuture if it has been set to this state. If
this Future is not yet set, or has not been set to an error, throws
an exception. Although this method returns a Throwable, the set of
possible types is more limited. A PartialFuture is limited to be set in
an error state to an Exception (or a subclass thereof) or an unchecked
Error.getError in interface PartialFuture<T>Futurepublic void dispose()
PartialFuturePartialFuture.cancel().dispose in interface PartialFuture<T>dispose in interface Disposable