@InterfaceAudience.Private public final class FutureUtils extends Object
Modifier and Type | Field and Description |
---|---|
private static org.slf4j.Logger |
LOG |
Modifier | Constructor and Description |
---|---|
private |
FutureUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> void |
addListener(CompletableFuture<T> future,
BiConsumer<? super T,? super Throwable> action)
This is method is used when you just want to add a listener to the given future.
|
static <T> void |
addListener(CompletableFuture<T> future,
BiConsumer<? super T,? super Throwable> action,
Executor executor)
Almost the same with
addListener(CompletableFuture, BiConsumer) method above, the only
exception is that we will call
CompletableFuture.whenCompleteAsync(BiConsumer, Executor) . |
static <T> CompletableFuture<T> |
failedFuture(Throwable e)
Returns a CompletableFuture that is already completed exceptionally with the given exception.
|
static <T> T |
get(Future<T> future)
A helper class for getting the result of a Future, and convert the error to an
IOException . |
static Throwable |
unwrapCompletionException(Throwable error)
Get the cause of the
Throwable if it is a CompletionException . |
static <T> CompletableFuture<T> |
wrapFuture(CompletableFuture<T> future,
Executor executor)
Return a
CompletableFuture which is same with the given future , but execute all
the callbacks in the given executor . |
private static final org.slf4j.Logger LOG
private FutureUtils()
public static <T> void addListener(CompletableFuture<T> future, BiConsumer<? super T,? super Throwable> action)
CompletableFuture.whenComplete(BiConsumer)
to register the action
to the
future
. Ignoring the return value of a Future is considered as a bad practice as it may
suppress exceptions thrown from the code that completes the future, and this method will catch
all the exception thrown from the action
to catch possible code bugs.
And the error phone check will always report FutureReturnValueIgnored because every method in
the CompletableFuture
class will return a new CompletableFuture
, so you always
have one future that has not been checked. So we introduce this method and add a suppress
warnings annotation here.public static <T> void addListener(CompletableFuture<T> future, BiConsumer<? super T,? super Throwable> action, Executor executor)
addListener(CompletableFuture, BiConsumer)
method above, the only
exception is that we will call
CompletableFuture.whenCompleteAsync(BiConsumer, Executor)
.public static <T> CompletableFuture<T> wrapFuture(CompletableFuture<T> future, Executor executor)
CompletableFuture
which is same with the given future
, but execute all
the callbacks in the given executor
.public static Throwable unwrapCompletionException(Throwable error)
Throwable
if it is a CompletionException
.public static <T> T get(Future<T> future) throws IOException
IOException
.IOException
public static <T> CompletableFuture<T> failedFuture(Throwable e)
Copyright © 2007–2019 The Apache Software Foundation. All rights reserved.