Package org.hawaiiframework.async
Class AsyncUtil
- java.lang.Object
-
- org.hawaiiframework.async.AsyncUtil
-
public final class AsyncUtil extends Object
Utility class to wrap an asynchronous call and catch all errors.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <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> 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<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 Detail
-
invoke
public static <T> CompletableFuture<T> invoke(AsyncCallable<T> body)
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
public static <T> CompletableFuture<T> invoke(boolean logError, AsyncCallable<T> body)
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
public static CompletableFuture<Void> invoke(AsyncInvoke body)
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
public static void waitForCompletion(Long timeout, TimeUnit unit, CompletableFuture<?>... futures)
Delegates toCompletableFuture.get(long, TimeUnit)}.- Parameters:
timeout- the timeout.unit- the timeunit.futures- the completable futures.
-
waitForCompletion
public static void waitForCompletion(Long timeout, TimeUnit unit, CompletableFuture<?> future)
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
public static <T> List<T> awaitAndMapToSingleList(List<CompletableFuture<List<T>>> futures)
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
public static <T> List<T> awaitAndGet(List<CompletableFuture<T>> futures)
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
public static <T> List<CompletableFuture<T>> awaitCompletion(List<CompletableFuture<T>> futures)
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.
-
-