Class FutureUtil
java.lang.Object
nl.pim16aap2.animatedarchitecture.core.util.FutureUtil
Utility class for futures.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Texceptionally(Throwable throwable) Seeexceptionally(Throwable, Object)with a null fallback value.static <T> Texceptionally(Throwable throwable, T fallback) Logs a throwable and returns a fallback value.static <T> VoidexceptionallyCompletion(Throwable throwable, CompletableFuture<T> target) Handles exceptional completion of aCompletableFuture.static <T,U> T exceptionallyCompletion(Throwable throwable, T fallback, CompletableFuture<U> target) Handles exceptional completion of aCompletableFuture.static <T> List<T> exceptionallyList(Throwable throwable) Seeexceptionally(Throwable, Object)with a fallback value ofCollections.emptyList().static <T> Optional<T> exceptionallyOptional(Throwable throwable) Seeexceptionally(Throwable, Object)with a fallback value ofOptional.empty().static <T> CompletableFuture<List<T>> getAllCompletableFutureResults(Collection<CompletableFuture<T>> futures) static <T> CompletableFuture<List<T>> getAllCompletableFutureResults(CompletableFuture<T>... futures) Maps a group of CompletableFutures to a single CompletableFuture with a list of results.static <T> CompletableFuture<List<T>> getAllCompletableFutureResultsFlatMap(Collection<CompletableFuture<? extends Collection<T>>> futures) static <T> CompletableFuture<List<T>> getAllCompletableFutureResultsFlatMap(CompletableFuture<? extends Collection<T>>... futures) Maps a group of CompletableFutures that each have a list as result to a single CompletableFuture with a list.static voidlogPossibleDeadlockTimeout(com.google.common.flogger.LazyArg<?> context, int timeoutMs, boolean isMainThread) Logs a possible deadlock timeout.
-
Method Details
-
logPossibleDeadlockTimeout
public static void logPossibleDeadlockTimeout(com.google.common.flogger.LazyArg<?> context, int timeoutMs, boolean isMainThread) Logs a possible deadlock timeout.- Parameters:
context- The context in which the deadlock occurred.timeoutMs- The timeout in milliseconds. This is the time that the thread was waiting before considering it a deadlock.isMainThread- Whether the thread that timed out is the main thread.
-
getAllCompletableFutureResults
public static <T> CompletableFuture<List<T>> getAllCompletableFutureResults(Collection<CompletableFuture<T>> futures) -
getAllCompletableFutureResults
@SafeVarargs public static <T> CompletableFuture<List<T>> getAllCompletableFutureResults(CompletableFuture<T>... futures) Maps a group of CompletableFutures to a single CompletableFuture with a list of results.The result will wait for each of the futures to complete and once all of them have completed gather the results and return the list.
Each entry in the list maps to the result of a single future.
- Type Parameters:
T- The type of data.- Parameters:
futures- The completable futures whose results to collect into a list.- Returns:
- The list of results obtained from the CompletableFutures in the same order as provided. The list will have a size that matches the number of input futures.
-
getAllCompletableFutureResultsFlatMap
public static <T> CompletableFuture<List<T>> getAllCompletableFutureResultsFlatMap(Collection<CompletableFuture<? extends Collection<T>>> futures) -
getAllCompletableFutureResultsFlatMap
@SafeVarargs public static <T> CompletableFuture<List<T>> getAllCompletableFutureResultsFlatMap(CompletableFuture<? extends Collection<T>>... futures) Maps a group of CompletableFutures that each have a list as result to a single CompletableFuture with a list.The result will wait for each of the futures to complete and once all of them have completed gather the results and return the list.
The results of the futures are flatMapped into a single list.
- Type Parameters:
T- The type of data in the lists.- Parameters:
futures- The completable futures whose results to flatMap into a list.- Returns:
- The list of results obtained from the CompletableFutures.
-
exceptionally
@Contract("_, !null -> !null") @Nullable public static <T> T exceptionally(Throwable throwable, @Nullable T fallback) Logs a throwable and returns a fallback value.Mostly useful for
CompletableFuture.exceptionally(Function).- Type Parameters:
T- The type of the fallback value.- Parameters:
throwable- The throwable to send to the logger.fallback- The fallback value to return.- Returns:
- The fallback value.
-
exceptionally
Seeexceptionally(Throwable, Object)with a null fallback value.- Returns:
- Always null
-
exceptionallyOptional
Seeexceptionally(Throwable, Object)with a fallback value ofOptional.empty().- Returns:
- Always
Optional.empty().
-
exceptionallyList
Seeexceptionally(Throwable, Object)with a fallback value ofCollections.emptyList().- Returns:
- Always
Collections.emptyList().
-
exceptionallyCompletion
public static <T,U> T exceptionallyCompletion(Throwable throwable, T fallback, CompletableFuture<U> target) Handles exceptional completion of aCompletableFuture. This ensures that the target is finished exceptionally as well, to propagate the exception.- Parameters:
throwable- TheThrowableto log.fallback- The fallback value to return.target- TheCompletableFutureto complete.- Returns:
- The fallback value.
-
exceptionallyCompletion
Handles exceptional completion of aCompletableFuture. This ensures that the target is finished exceptionally as well, to propagate the exception.- Parameters:
throwable- TheThrowableto log.target- TheCompletableFutureto complete.- Returns:
- Always null;
-