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 Details

    • get

      public static <T> T get(@NonNull CompletableFuture<T> future)
      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)
      Type Parameters:
      T - The type to return
      Parameters:
      future - The completable future to get the value from.
      timeout - the maximum time to wait
      unit - the time unit of the timeout argument
      Returns:
      the result value
    • getNow

      public static <T> T getNow(@NonNull CompletableFuture<T> future, T valueIfAbsent)
      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

      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<org.hawaiiframework.util.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)
      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)
      Parameters:
      timeout - the timeout.
      unit - the timeunit.
      futures - the completable futures.
    • waitForCompletion

      public static void waitForCompletion(Long timeout, TimeUnit unit, CompletableFuture<?> future)
      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 asynchronous function to each element of inputs. 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>, or null if the input is null.
    • asyncStreamAndMapToSingleList

      public static <I, T> List<T> asyncStreamAndMapToSingleList(Collection<I> inputs, Function<I,CompletableFuture<List<T>>> function)
      Applies the asynchronous function to each element of inputs. 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>, or null if 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>, or null if the input is null.
    • asyncMap

      public static <I, T> List<CompletableFuture<T>> asyncMap(Collection<I> inputs, Function<I,CompletableFuture<T>> function)
      Applies the asynchronous function to each element of inputs.
      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 null if 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.