T
- The result object type returned by this futurepublic class ImmediateResultListenableFuture<T>
extends java.lang.Object
ListenableFuture
that will immediately return a result.
Meaning listeners added will immediately be ran/executed, FutureCallback
's will
immediately get called with the result provided, and get()
calls will never block.ListenableFuture.ListenerOptimizationStrategy
Modifier and Type | Field and Description |
---|---|
static ImmediateResultListenableFuture<java.lang.Boolean> |
BOOLEAN_FALSE_RESULT
Static instance of
ImmediateResultListenableFuture which provides a Boolean
in the false state as the result. |
static ImmediateResultListenableFuture<java.lang.Boolean> |
BOOLEAN_TRUE_RESULT
Static instance of
ImmediateResultListenableFuture which provides a Boolean
in the true state as the result. |
static ImmediateResultListenableFuture<?> |
NULL_RESULT
Static instance of
ImmediateResultListenableFuture which provides a null
result. |
Constructor and Description |
---|
ImmediateResultListenableFuture(T result)
Constructs a completed future that will return the provided result.
|
Modifier and Type | Method and Description |
---|---|
void |
addCallback(FutureCallback<? super T> callback)
Add a
FutureCallback to be called once the future has completed. |
void |
addCallback(FutureCallback<? super T> callback,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimize)
Add a
FutureCallback to be called once the future has completed. |
void |
addListener(java.lang.Runnable listener)
Add a listener to be called once the future has completed.
|
void |
addListener(java.lang.Runnable listener,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimize)
Add a listener to be called once the future has completed.
|
boolean |
cancel(boolean mayInterruptIfRunning)
This has no effect in this implementation, as this future can not be canceled.
|
<TT extends java.lang.Throwable> |
flatMapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<TT,ListenableFuture<T>> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Similar to
ListenableFuture.mapFailure(Class, Function, Executor, ListenerOptimizationStrategy) except
that this mapper function returns a ListenableFuture if it needs to map the Throwable
into a result or another failure. |
T |
get() |
T |
get(long timeout,
java.util.concurrent.TimeUnit unit) |
boolean |
isCancelled() |
boolean |
isDone() |
<TT extends java.lang.Throwable> |
mapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<TT,T> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Similar to
ListenableFuture.throwMap(Function, Executor, ListenerOptimizationStrategy) except this
mapper will only be invoked when the future is in a failure state (from either the original
computation or an earlier mapper throwing an exception). |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addCallback, addListener, flatMap, flatMap, flatMap, flatMapFailure, flatMapFailure, map, map, map, mapFailure, mapFailure, throwMap, throwMap, throwMap
public static final ImmediateResultListenableFuture<?> NULL_RESULT
ImmediateResultListenableFuture
which provides a null
result. Since this is a common case this can avoid GC overhead. If you want to get this in
any generic type use FutureUtils.immediateResultFuture(Object)
, which when provided a
null
will always return this static instance.public static final ImmediateResultListenableFuture<java.lang.Boolean> BOOLEAN_TRUE_RESULT
ImmediateResultListenableFuture
which provides a Boolean
in the true
state as the result.public static final ImmediateResultListenableFuture<java.lang.Boolean> BOOLEAN_FALSE_RESULT
ImmediateResultListenableFuture
which provides a Boolean
in the false
state as the result.public ImmediateResultListenableFuture(T result)
result
- Result that is returned by futurepublic <TT extends java.lang.Throwable> ListenableFuture<T> mapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<TT,T> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
ListenableFuture
ListenableFuture.throwMap(Function, Executor, ListenerOptimizationStrategy)
except this
mapper will only be invoked when the future is in a failure state (from either the original
computation or an earlier mapper throwing an exception). If this future does resolve in a
failure state, and that exception class matches the one provided here. The mapper function
will then be provided that throwable, it can then map that throwable back into a result
perhaps an Optional
), or re-throw either the same or a different exception keep the
future in a failure state. If the future completes with a normal result, this mapper will be
ignored, and the result will be forwarded on without invoking this mapper.TT
- The type of throwable that should be handledthrowableType
- The class referencing to the type of throwable this mapper handlesmapper
- The mapper to convert a thrown exception to either a result or thrown exceptionexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolListenableFuture
that will resolve after the mapper is consideredpublic <TT extends java.lang.Throwable> ListenableFuture<T> flatMapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<TT,ListenableFuture<T>> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
ListenableFuture
ListenableFuture.mapFailure(Class, Function, Executor, ListenerOptimizationStrategy)
except
that this mapper function returns a ListenableFuture
if it needs to map the Throwable
into a result or another failure. The mapper function can return a Future that will (or may)
provide a result, or it can provide a future that will result in the same or another failure.
Similar to ListenableFuture.mapFailure(Class, Function, Executor, ListenerOptimizationStrategy)
the
mapper can also throw an exception directly.TT
- The type of throwable that should be handledthrowableType
- The class referencing to the type of throwable this mapper handlesmapper
- Function to invoke in order to transform the futures resultexecutor
- Executor to invoke mapper function on, or null
to invoke on this thread or future complete thread (depending on future state)optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolListenableFuture
that will resolve after the mapper is consideredpublic void addCallback(FutureCallback<? super T> callback)
ListenableFuture
FutureCallback
to be called once the future has completed. If the future has
already finished, this will be called immediately.
The callback from this call will execute on the same thread the result was produced on, or on the adding thread if the future is already complete. If the callback has high complexity, consider passing an executor in for it to be called on.
callback
- to be invoked when the computation is completepublic void addCallback(FutureCallback<? super T> callback, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimize)
ListenableFuture
FutureCallback
to be called once the future has completed. If the future has
already finished, this will be called immediately.
If the provided Executor
is null, the callback will execute on the thread which
computed the original future (once it is done). If the future has already completed, the
callback will execute immediately on the thread which is adding the callback.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
callback
- to be invoked when the computation is completeexecutor
- Executor
the callback should be ran on, or null
optimize
- true
to avoid listener queuing for execution if already on the desired poolpublic T get()
public T get(long timeout, java.util.concurrent.TimeUnit unit)
public boolean isDone()
isDone
in interface java.util.concurrent.Future<T>
public void addListener(java.lang.Runnable listener)
ListenableFuture
The listener from this call will execute on the same thread the result was produced on, or on
the adding thread if the future is already complete. If the runnable has high complexity,
consider using ListenableFuture.addListener(Runnable, Executor)
.
addListener
in interface ListenableFuture<T>
listener
- the listener to run when the computation is completepublic void addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimize)
ListenableFuture
If the provided Executor
is null, the listener will execute on the thread which
computed the original future (once it is done). If the future has already completed, the
listener will execute immediately on the thread which is adding the listener.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
addListener
in interface ListenableFuture<T>
listener
- the listener to run when the computation is completeexecutor
- Executor
the listener should be ran on, or null
optimize
- true
to avoid listener queuing for execution if already on the desired poolpublic boolean cancel(boolean mayInterruptIfRunning)
cancel
in interface java.util.concurrent.Future<T>
mayInterruptIfRunning
- will be ignoredfalse
, as this future can't be canceledpublic boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<T>