Package org.hawaiiframework.async
Class AsyncUtil
java.lang.Object
org.hawaiiframework.async.AsyncUtil
Utility class to wrap an asynchronous call and catch all errors.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <I,T> List<CompletableFuture<T>> asyncMap(Collection<I> inputs, Function<I, CompletableFuture<T>> function) Applies the asynchronousfunctionto each element ofinputs.static <I,T> List<T> asyncStreamAndMap(Collection<I> inputs, Function<I, CompletableFuture<T>> function) Applies the asynchronousfunctionto each element ofinputs.static <I,T> List<T> asyncStreamAndMapToSingleList(Collection<I> inputs, Function<I, CompletableFuture<List<T>>> function) Applies the asynchronousfunctionto each element ofinputs.static <T> List<T>awaitAndGet(List<CompletableFuture<T>> futures) Await the completion of all futures and return the futures' answers.static <T> List<T>awaitAndMapToSingleList(List<CompletableFuture<List<T>>> futures) Await the completion of the futures and return the results in one list.static <T> List<CompletableFuture<T>>awaitCompletion(List<CompletableFuture<T>> futures) Await completion of the set of futures.static <T> Tget(CompletableFuture<T> future) Delegates toCompletableFuture.get().static <T> Tget(CompletableFuture<T> future, Long timeout, TimeUnit unit) Delegates toCompletableFuture.get(long, TimeUnit)}.static <T> TgetNow(CompletableFuture<T> future, T valueIfAbsent) Delegates toCompletableFuture.getNow(Object).static <T> CompletableFuture<T>invoke(boolean logError, AsyncCallable<T> body) Invoke the body and return the response wrapped as a future.static <T> CompletableFuture<T>invoke(AsyncCallable<T> body) Invoke the body and return the response wrapped as a future.static CompletableFuture<org.hawaiiframework.util.Void>invoke(AsyncInvoke body) Invoke the body and return the response wrapped as a future.static voidwaitForCompletion(Long timeout, TimeUnit unit, CompletableFuture<?> future) Delegates toCompletableFuture.get(long, TimeUnit)}.static voidwaitForCompletion(Long timeout, TimeUnit unit, CompletableFuture<?>... futures) Delegates toCompletableFuture.get(long, TimeUnit)}.static <T> voidwaitForCompletion(Long timeout, TimeUnit unit, List<CompletableFuture<T>> futures) Delegates toCompletableFuture.get(long, TimeUnit)}.
-
Method Details
-
get
Delegates toCompletableFuture.get().- Type Parameters:
T- The type to return- Parameters:
future- The completable future to get the value from.- Returns:
- the result value
-
get
public static <T> T get(@NonNull CompletableFuture<T> future, @NonNull Long timeout, @NonNull TimeUnit unit) Delegates toCompletableFuture.get(long, TimeUnit)}.- Type Parameters:
T- The type to return- Parameters:
future- The completable future to get the value from.timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the result value
-
getNow
Delegates toCompletableFuture.getNow(Object).- Type Parameters:
T- The type to return- Parameters:
future- The completable future to get the value from.valueIfAbsent- The value to return if not completed- Returns:
- the result value, if completed, else the given valueIfAbsent
-
invoke
Invoke the body and return the response wrapped as a future.- Type Parameters:
T- the return type- Parameters:
body- the body.- Returns:
- the response wrapped as a future
-
invoke
Invoke the body and return the response wrapped as a future.- Type Parameters:
T- the return type- Parameters:
logError- indicates if errors need to be logged to error.body- the body.- Returns:
- the response wrapped as a future
-
invoke
Invoke the body and return the response wrapped as a future.- Parameters:
body- the body.- Returns:
- the response wrapped as a future
-
waitForCompletion
public static <T> void waitForCompletion(Long timeout, TimeUnit unit, List<CompletableFuture<T>> futures) Delegates toCompletableFuture.get(long, TimeUnit)}.- Type Parameters:
T- the return type- Parameters:
timeout- the timeout.unit- the timeunit.futures- the list of completable futures.
-
waitForCompletion
Delegates toCompletableFuture.get(long, TimeUnit)}.- Parameters:
timeout- the timeout.unit- the timeunit.futures- the completable futures.
-
waitForCompletion
Delegates toCompletableFuture.get(long, TimeUnit)}.- Parameters:
timeout- the timeout.unit- the timeunit.future- the completable future.
-
asyncStreamAndMap
public static <I,T> List<T> asyncStreamAndMap(Collection<I> inputs, Function<I, CompletableFuture<T>> function) Applies the asynchronousfunctionto each element ofinputs. It then awaits the completion of the calls and returns the function's returns.- Type Parameters:
I- The input object's type.T- The return types.- Parameters:
inputs- The collection of input objects.function- The function to apply- Returns:
- A list of type
<T>, ornullif the input is null.
-
asyncStreamAndMapToSingleList
public static <I,T> List<T> asyncStreamAndMapToSingleList(Collection<I> inputs, Function<I, CompletableFuture<List<T>>> function) Applies the asynchronousfunctionto each element ofinputs. It then awaits the completion of the calls and returns the function's returns. Instead of returning a list of lists, this method flap maps the lists' contents onto one result.- Type Parameters:
I- The input object's type.T- The return types.- Parameters:
inputs- The collection of input objects.function- The function to apply- Returns:
- A list of type
<T>, ornullif the input is null.
-
awaitAndMapToSingleList
Await the completion of the futures and return the results in one list.- Type Parameters:
T- The return types.- Parameters:
futures- The list of futures.- Returns:
- A list of type
<T>, ornullif the input is null.
-
asyncMap
public static <I,T> List<CompletableFuture<T>> asyncMap(Collection<I> inputs, Function<I, CompletableFuture<T>> function) Applies the asynchronousfunctionto each element ofinputs.- Type Parameters:
I- The input object's type.T- The return types.- Parameters:
inputs- The collection of input objects.function- The function to apply- Returns:
- A list of completable futures, or
nullif the input is null or empty.
-
awaitAndGet
Await the completion of all futures and return the futures' answers.- Type Parameters:
T- The return types.- Parameters:
futures- The list of completable futures.- Returns:
- The list of the futures' answers.
-
awaitCompletion
Await completion of the set of futures.- Type Parameters:
T- the return type- Parameters:
futures- the stream of completable futures.- Returns:
- the completed list of futures.
-