T
- The result object type returned by this futurepublic interface ListenableFuture<T>
extends java.util.concurrent.Future<T>
Modifier and Type | Interface and Description |
---|---|
static class |
ListenableFuture.ListenerOptimizationStrategy
Strategy to use for optimizing listener execution.
|
Modifier and Type | Method and Description |
---|---|
default ListenableFuture<T> |
callback(FutureCallback<? super T> callback)
Add a
FutureCallback to be called once the future has completed. |
default ListenableFuture<T> |
callback(FutureCallback<? super T> callback,
java.util.concurrent.Executor executor)
Add a
FutureCallback to be called once the future has completed. |
default ListenableFuture<T> |
callback(FutureCallback<? super T> callback,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Add a
FutureCallback to be called once the future has completed. |
default ListenableFuture<T> |
failureCallback(java.util.function.Consumer<java.lang.Throwable> callback)
Add a
Consumer to be called once the future has completed. |
default ListenableFuture<T> |
failureCallback(java.util.function.Consumer<java.lang.Throwable> callback,
java.util.concurrent.Executor executor)
Add a
Consumer to be called once the future has completed. |
default ListenableFuture<T> |
failureCallback(java.util.function.Consumer<java.lang.Throwable> callback,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Add a
Consumer to be called once the future has completed. |
default <R> ListenableFuture<R> |
flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper)
Similar to
map(Function) , in that this will apply a mapper function once the applied
to future completes. |
default <R> ListenableFuture<R> |
flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper,
java.util.concurrent.Executor executor)
Similar to
map(Function, Executor) , in that this will apply a mapper function once
the applied to future completes. |
default <R> ListenableFuture<R> |
flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Similar to
map(Function, Executor) , in that this will apply a mapper function once
the applied to future completes. |
default <R> ListenableFuture<R> |
flatMap(ListenableFuture<R> future)
Convenience function for mapping this future in with an existing future, ignoring the result
of this current future.
|
default <TT extends java.lang.Throwable> |
flatMapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,ListenableFuture<T>> mapper)
Similar to
mapFailure(Class, Function) except that this mapper function returns a
ListenableFuture if it needs to map the Throwable / failure into a result or another
failure. |
default <TT extends java.lang.Throwable> |
flatMapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,ListenableFuture<T>> mapper,
java.util.concurrent.Executor executor)
Similar to
mapFailure(Class, Function, Executor) except that this mapper function
returns a ListenableFuture if it needs to map the Throwable / failure into a result
or another failure. |
default <TT extends java.lang.Throwable> |
flatMapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,ListenableFuture<T>> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Similar to
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. |
java.lang.Throwable |
getFailure()
Similar to
Future.get() except instead of providing a result, this will provide a thrown
exception if isCompletedExceptionally() returns true . |
java.lang.Throwable |
getFailure(long timeout,
java.util.concurrent.TimeUnit unit)
Similar to
Future.get(long, TimeUnit) except instead of providing a result, this will
provide a thrown exception if isCompletedExceptionally() returns true . |
java.lang.StackTraceElement[] |
getRunningStackTrace()
A best effort to return the stack trace for for the executing thread of either this future,
or a future which this depends on through the use of
map(Function) or similar
functions. |
boolean |
isCompletedExceptionally()
Returns
true if the future is both done and has completed with an error or was
canceled. |
default ListenableFuture<T> |
listener(java.lang.Runnable listener)
Add a listener to be called once the future has completed.
|
default ListenableFuture<T> |
listener(java.lang.Runnable listener,
java.util.concurrent.Executor executor)
Add a listener to be called once the future has completed.
|
ListenableFuture<T> |
listener(java.lang.Runnable listener,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Add a listener to be called once the future has completed.
|
default <R> ListenableFuture<R> |
map(java.util.function.Function<? super T,? extends R> mapper)
Transform this future's result into another result by applying the provided mapper function.
|
default <R> ListenableFuture<R> |
map(java.util.function.Function<? super T,? extends R> mapper,
java.util.concurrent.Executor executor)
Transform this future's result into another result by applying the provided mapper function.
|
default <R> ListenableFuture<R> |
map(java.util.function.Function<? super T,? extends R> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Transform this future's result into another result by applying the provided mapper function.
|
default <TT extends java.lang.Throwable> |
mapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,? extends T> mapper)
Similar to
throwMap(Function) 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). |
default <TT extends java.lang.Throwable> |
mapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,? extends T> mapper,
java.util.concurrent.Executor executor)
Similar to
throwMap(Function, Executor) 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). |
default <TT extends java.lang.Throwable> |
mapFailure(java.lang.Class<TT> throwableType,
java.util.function.Function<? super TT,? extends T> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Similar to
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). |
default ListenableFuture<T> |
resultCallback(java.util.function.Consumer<? super T> callback)
Add a
Consumer to be called once the future has completed. |
default ListenableFuture<T> |
resultCallback(java.util.function.Consumer<? super T> callback,
java.util.concurrent.Executor executor)
Add a
Consumer to be called once the future has completed. |
default ListenableFuture<T> |
resultCallback(java.util.function.Consumer<? super T> callback,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Add a
Consumer to be called once the future has completed. |
default <R> ListenableFuture<R> |
throwMap(java.util.function.Function<? super T,? extends R> mapper)
Transform this future's result into another result by applying the provided mapper function.
|
default <R> ListenableFuture<R> |
throwMap(java.util.function.Function<? super T,? extends R> mapper,
java.util.concurrent.Executor executor)
Transform this future's result into another result by applying the provided mapper function.
|
default <R> ListenableFuture<R> |
throwMap(java.util.function.Function<? super T,? extends R> mapper,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Transform this future's result into another result by applying the provided mapper function.
|
boolean isCompletedExceptionally()
true
if the future is both done and has completed with an error or was
canceled. If this returns true
the Throwable
responsible for the error can
be retrieved using getFailure()
;true
if this ListenableFuture completed by a thrown Exception or was canceledjava.lang.Throwable getFailure() throws java.lang.InterruptedException
Future.get()
except instead of providing a result, this will provide a thrown
exception if isCompletedExceptionally()
returns true
. If the future has not
completed yet this function will block until completion. If the future completed normally,
this will return null
.null
if completed normallyjava.lang.InterruptedException
- If the current thread was interrupted while blockingjava.lang.Throwable getFailure(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
Future.get(long, TimeUnit)
except instead of providing a result, this will
provide a thrown exception if isCompletedExceptionally()
returns true
. If
the future has not completed yet this function will block until completion. If the future
completed normally, this will return null
.timeout
- The maximum time to waitunit
- The time unit of the timeout argumentnull
if completed normallyjava.lang.InterruptedException
- If the current thread was interrupted while blockingjava.util.concurrent.TimeoutException
- If the timeout was reached before the future completeddefault <R> ListenableFuture<R> map(java.util.function.Function<? super T,? extends R> mapper)
ExceptionUtils.handleException(Throwable)
will be invoked. If you
don't want mapped exceptions to be treated as unexpected / uncaught please see
throwMap(Function)
.
This can be easily used to chain together a series of operations, happening async (or in
calling thread if already complete) until the final result is actually needed.
map(Function, Executor)
can be used if transformation mapper is expensive and thus
async execution is absolutely required.
If the future is complete already, the function may be invoked on the invoking thread. If the future is not complete then the function will be invoked on the thread which completes the future (immediately after it completes).
If your function returns a future, consider using flatMap(Function)
as an alternative.
Example use:
public Integer countSomething(String value);
public ListenableFuture<String> lookupSomething();
ListenableFuture<Integer> count = lookupSomething().map((s) -> countSomething(s));
R
- The type for the object returned from the mappermapper
- Function to invoke in order to transform the futures resultListenableFuture
with the specified result typedefault <R> ListenableFuture<R> map(java.util.function.Function<? super T,? extends R> mapper, java.util.concurrent.Executor executor)
ExceptionUtils.handleException(Throwable)
will be invoked. If you
don't want mapped exceptions to be treated as unexpected / uncaught please see
throwMap(Function, Executor)
.
This can be easily used to chain together a series of operations, happening async until the
final result is actually needed. Once the future completes the mapper function will be invoked
on the executor (if provided). Because of that providing an executor can ensure this will
never block. If an executor is not provided then the mapper may be invoked on the calling
thread (if the future is already complete), or on the same thread which the future completes
on. If the mapper function is very fast and cheap to run then map(Function)
or
providing null
for the executor can allow more efficient operation.
If your function returns a future, consider using flatMap(Function, Executor)
as an
alternative.
R
- The type for the object returned from the mappermapper
- 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)ListenableFuture
with the specified result typedefault <R> ListenableFuture<R> map(java.util.function.Function<? super T,? extends R> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
ExceptionUtils.handleException(Throwable)
will be invoked. If you
don't want mapped exceptions to be treated as unexpected / uncaught please see
throwMap(Function, Executor, ListenerOptimizationStrategy)
.
This can be easily used to chain together a series of operations, happening async until the
final result is actually needed. Once the future completes the mapper function will be invoked
on the executor (if provided). Because of that providing an executor can ensure this will
never block. If an executor is not provided then the mapper may be invoked on the calling
thread (if the future is already complete), or on the same thread which the future completes
on. If the mapper function is very fast and cheap to run then map(Function)
or
providing null
for the executor can allow more efficient operation.
If your function returns a future, consider using flatMap(Function, Executor)
as an
alternative.
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.
R
- The type for the object returned from the mappermapper
- 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
with the specified result typedefault <R> ListenableFuture<R> throwMap(java.util.function.Function<? super T,? extends R> mapper)
This function differs from map(Function)
only in how exceptions thrown from the
mapper
function are handled. Please see map(Function)
for a general
understanding for the behavior of this operation. This is to be used when the mapper function
throwing an exception is EXPECTED. Used to avoid treating mapping an exception as an
unexpected behavior.
Say for example you wanted to use FutureUtils.scheduleWhile()
to retry an operation
as long as it kept throwing an exception. You might put the result and exception into
a Pair
so it can be checked for a maximum number of times like this:
ListenableFuture<Pair<ResultType, RetryException>> exceptionConvertingLoop =
FutureUtils.scheduleWhile(scheduler, retryDelayMillis, true, () -> {
try {
return new Pair<>(makeResultOrThrow(), null);
} catch (RetryException e) {
// we can't throw or we will break the loop
return new Pair<>(null, e);
}
}, new Predicate<Pair<?, RetryException>>() {
private int selfRetryCount = 0;
public boolean test(Pair<?, RetryException> p) {
return p.getLeft() == null && ++selfRetryCount <= maxRetryCount;
}
});
You would then need to use throwMap(Function)
in order to convert that Pair
back into a ListenableFuture
with either a result, or the contained failure. For
example:
ListenableFuture<ResultType> resultFuture =
exceptionConvertingLoop.throwMap((p) -> {
if (p.getLeft() != null) {
return p.getLeft();
} else {
throw p.getRight();
}
});
R
- The type for the object returned from the mappermapper
- Function to invoke in order to transform the futures resultListenableFuture
with the specified result typedefault <R> ListenableFuture<R> throwMap(java.util.function.Function<? super T,? extends R> mapper, java.util.concurrent.Executor executor)
This function differs from map(Function, Executor)
only in how exceptions thrown
from the mapper
function are handled. Please see map(Function, Executor)
for a general understanding for the behavior of this operation. This is to be used when the
mapper function throwing an exception is EXPECTED. Used to avoid treating mapping an
exception as an unexpected behavior. Please see throwMap(Function)
for more usage
examples.
Once the future completes the mapper function will be invoked on the executor (if provided).
Because of that providing an executor can ensure this will never block. If an executor is not
provided then the mapper may be invoked on the calling thread (if the future is already
complete), or on the same thread which the future completes on. If the mapper function is
very fast and cheap to run then throwMap(Function)
or providing null
for
the executor can allow more efficient operation.
R
- The type for the object returned from the mappermapper
- 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)ListenableFuture
with the specified result typedefault <R> ListenableFuture<R> throwMap(java.util.function.Function<? super T,? extends R> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
This can be easily used to chain together a series of operations, happening async until the
final result is actually needed. Once the future completes the mapper function will be invoked
on the executor (if provided). Because of that providing an executor can ensure this will
never block. If an executor is not provided then the mapper may be invoked on the calling
thread (if the future is already complete), or on the same thread which the future completes
on. If the mapper function is very fast and cheap to run then throwMap(Function)
or
providing null
for the executor can allow more efficient operation.
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.
R
- The type for the object returned from the mappermapper
- 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
with the specified result typedefault <R> ListenableFuture<R> flatMap(ListenableFuture<R> future)
flatMap((ignored) -> future)
, and
conceptually the same as FutureUtils.makeFailurePropagatingCompleteFuture(Iterable)
with both futures being provided (though this allows us to capture the result of the provided
future).
Please be aware that flatMap(futureProducer.get())
is NOT equivalent to
flatMap((ignored) -> futureProducer.get())
. As the second version would delay starting
the future generation until this future completes. By calling into this with a future you will
be starting its execution immediately.
R
- The type of result returned from the provided futurefuture
- The future to flat mpa against this oneListenableFuture
that will complete when both this and the provided future doesdefault <R> ListenableFuture<R> flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper)
map(Function)
, in that this will apply a mapper function once the applied
to future completes. Once this future resolves it will provide the result into the provided
function. Unlike map(Function)
, this will then unwrap a future provided from the
function so that instead of having ListenableFuture<ListenableFuture<R>>
you can
simply extract the final value. The returned future will only resolve once the future of the
provided function completes.
If the future is complete already, the function may be invoked on the invoking thread. If the future is not complete then the function will be invoked on the thread which completes the future (immediately after it completes).
Example use:
public ListenableFuture<Integer> countSomething(String value);
public ListenableFuture<String> lookupSomething();
ListenableFuture<Integer> count = lookupSomething().flatMap((s) -> countSomething(s));
R
- The type for the object contained in the future which is returned from the mappermapper
- Function to invoke in order to transform the futures resultListenableFuture
with the specified result typedefault <R> ListenableFuture<R> flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper, java.util.concurrent.Executor executor)
map(Function, Executor)
, in that this will apply a mapper function once
the applied to future completes. Once this future resolves it will provide the result into
the provided function. Unlike map(Function, Executor)
, this will then unwrap a
future provided from the function so that instead of having
ListenableFuture<ListenableFuture<R>>
you can simply extract the final value. The
returned future will only resolve once the future of the provided function completes.
Once the future completes the mapper function will be invoked on the executor (if provided).
Because of that providing an executor can ensure this will never block. If an executor is
not provided then the mapper may be invoked on the calling thread (if the future is already
complete), or on the same thread which the future completes on. If the mapper function is
very fast and cheap to run then flatMap(Function)
or providing null
for the
executor can allow more efficient operation.
R
- The type for the object contained in the future which is returned from the mappermapper
- 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)ListenableFuture
with the specified result typedefault <R> ListenableFuture<R> flatMap(java.util.function.Function<? super T,ListenableFuture<R>> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
map(Function, Executor)
, in that this will apply a mapper function once
the applied to future completes. Once this future resolves it will provide the result into
the provided function. Unlike map(Function, Executor)
, this will then unwrap a
future provided from the function so that instead of having
ListenableFuture<ListenableFuture<R>>
you can simply extract the final value. The
returned future will only resolve once the future of the provided function completes.
Once the future completes the mapper function will be invoked on the executor (if provided).
Because of that providing an executor can ensure this will never block. If an executor is
not provided then the mapper may be invoked on the calling thread (if the future is already
complete), or on the same thread which the future completes on. If the mapper function is
very fast and cheap to run then flatMap(Function)
or providing null
for the
executor can allow more efficient operation.
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.
R
- The type for the object contained in the future which is returned from the mappermapper
- 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
with the specified result typedefault <TT extends java.lang.Throwable> ListenableFuture<T> mapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,? extends T> mapper)
throwMap(Function)
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 exceptionListenableFuture
that will resolve after the mapper is considereddefault <TT extends java.lang.Throwable> ListenableFuture<T> mapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,? extends T> mapper, java.util.concurrent.Executor executor)
throwMap(Function, Executor)
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)ListenableFuture
that will resolve after the mapper is considereddefault <TT extends java.lang.Throwable> ListenableFuture<T> mapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,? extends T> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
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 considereddefault <TT extends java.lang.Throwable> ListenableFuture<T> flatMapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,ListenableFuture<T>> mapper)
mapFailure(Class, Function)
except that this mapper function returns a
ListenableFuture
if it needs to map the Throwable / failure 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
mapFailure(Class, Function)
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 resultListenableFuture
that will resolve after the mapper is considereddefault <TT extends java.lang.Throwable> ListenableFuture<T> flatMapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,ListenableFuture<T>> mapper, java.util.concurrent.Executor executor)
mapFailure(Class, Function, Executor)
except that this mapper function
returns a ListenableFuture
if it needs to map the Throwable / failure 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 mapFailure(Class, Function, Executor)
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)ListenableFuture
that will resolve after the mapper is considereddefault <TT extends java.lang.Throwable> ListenableFuture<T> flatMapFailure(java.lang.Class<TT> throwableType, java.util.function.Function<? super TT,ListenableFuture<T>> mapper, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
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 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 considereddefault ListenableFuture<T> listener(java.lang.Runnable listener)
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 listener(Runnable, Executor)
.
listener
- the listener to run when the computation is completethis
instance to add more listeners or other functional operationsdefault ListenableFuture<T> listener(java.lang.Runnable listener, java.util.concurrent.Executor executor)
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.
listener
- the listener to run when the computation is completeexecutor
- Executor
the listener should be ran on, or null
this
instance to add more listeners or other functional operationsListenableFuture<T> listener(java.lang.Runnable listener, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
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.
listener
- the listener to run when the computation is completeexecutor
- Executor
the listener should be ran on, or null
optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolthis
instance to add more listeners or other functional operationsdefault ListenableFuture<T> callback(FutureCallback<? super T> callback)
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.
If you only care about the success result case please see resultCallback(Consumer)
or
conversely if you only want to be invoked for failure cases please see
failureCallback(Consumer)
.
callback
- to be invoked when the computation is completethis
instance to add more callbacks or other functional operationsdefault ListenableFuture<T> callback(FutureCallback<? super T> callback, java.util.concurrent.Executor executor)
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.
If you only care about the success result case please see
resultCallback(Consumer, Executor)
or conversely if you only want to be invoked for
failure cases please see failureCallback(Consumer, Executor)
.
callback
- to be invoked when the computation is completeexecutor
- Executor
the callback should be ran on, or null
this
instance to add more callbacks or other functional operationsdefault ListenableFuture<T> callback(FutureCallback<? super T> callback, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
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.
If you only care about the success result case please see
resultCallback(Consumer, Executor, ListenerOptimizationStrategy)
or conversely if you
only want to be invoked for failure cases please see
failureCallback(Consumer, Executor, ListenerOptimizationStrategy)
.
callback
- to be invoked when the computation is completeexecutor
- Executor
the callback should be ran on, or null
optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolthis
instance to add more callbacks or other functional operationsdefault ListenableFuture<T> resultCallback(java.util.function.Consumer<? super T> callback)
Consumer
to be called once the future has completed. If the future has
already finished, this will be called immediately. Assuming the future has completed without
error, the result will be provided to the Consumer
, otherwise it will go un-invoked.
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 completethis
instance to add more callbacks or other functional operationsdefault ListenableFuture<T> resultCallback(java.util.function.Consumer<? super T> callback, java.util.concurrent.Executor executor)
Consumer
to be called once the future has completed. If the future has
already finished, this will be called immediately. Assuming the future has completed without
error, the result will be provided to the Consumer
, otherwise it will go un-invoked.
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.
callback
- to be invoked when the computation is completeexecutor
- Executor
the callback should be ran on, or null
this
instance to add more callbacks or other functional operationsdefault ListenableFuture<T> resultCallback(java.util.function.Consumer<? super T> callback, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Consumer
to be called once the future has completed. If the future has
already finished, this will be called immediately. Assuming the future has completed without
error, the result will be provided to the Consumer
, otherwise it will go un-invoked.
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
optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolthis
instance to add more callbacks or other functional operationsdefault ListenableFuture<T> failureCallback(java.util.function.Consumer<java.lang.Throwable> callback)
Consumer
to be called once the future has completed. If the future has
already finished, this will be called immediately. Assuming the future has completed with an
error, the Throwable
will be provided to the Consumer
, otherwise if no error
occurred, the callback will go un-invoked.
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 completethis
instance to add more callbacks or other functional operationsdefault ListenableFuture<T> failureCallback(java.util.function.Consumer<java.lang.Throwable> callback, java.util.concurrent.Executor executor)
Consumer
to be called once the future has completed. If the future has
already finished, this will be called immediately. Assuming the future has completed with an
error, the Throwable
will be provided to the Consumer
, otherwise if no error
occurred, the callback will go un-invoked.
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.
callback
- to be invoked when the computation is completeexecutor
- Executor
the callback should be ran on, or null
this
instance to add more callbacks or other functional operationsdefault ListenableFuture<T> failureCallback(java.util.function.Consumer<java.lang.Throwable> callback, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimizeExecution)
Consumer
to be called once the future has completed. If the future has
already finished, this will be called immediately. Assuming the future has completed with an
error, the Throwable
will be provided to the Consumer
, otherwise if no error
occurred, the callback will go un-invoked.
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
optimizeExecution
- true
to avoid listener queuing for execution if already on the desired poolthis
instance to add more callbacks or other functional operationsjava.lang.StackTraceElement[] getRunningStackTrace()
map(Function)
or similar
functions. If there is no thread executing the future yet, or the future has already
completed, then this will return null
.
This is done without locking (though generating a stack trace still requires a JVM safe point), so the resulting stack trace is NOT guaranteed to be accurate. In most cases (particularly when blocking) this should be accurate though.
null
if unavailable