Class AsyncUtil


  • public final class AsyncUtil
    extends Object
    Utility class to wrap an asynchronous call and catch all errors.
    • 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
      • 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.