Class MoreAsyncUtil

    • Method Detail

      • filterIterable

        @Nonnull
        public static <T> AsyncIterable<T> filterIterable​(@Nonnull
                                                          AsyncIterable<T> iterable,
                                                          @Nonnull
                                                          Function<T,​Boolean> filter)
        Filter items from an async iterable.
        Type Parameters:
        T - the source type
        Parameters:
        iterable - the source
        filter - only items in iterable for which this function returns true will appear in the return value
        Returns:
        a new AsyncIterable that only contains those items in iterable for which filter returns true
      • dedupIterable

        @Nonnull
        public static <T> AsyncIterable<T> dedupIterable​(@Nonnull
                                                         AsyncIterable<T> iterable)
        Remove adjacent duplicates form iterable. Note: if iterable is sorted, this will actually remove duplicates.
        Type Parameters:
        T - the source type
        Parameters:
        iterable - the source
        Returns:
        a new AsyncIterable that only contains those items in iterable for which the previous item was different
      • concatIterables

        @Nonnull
        public static <T> AsyncIterable<T> concatIterables​(@Nonnull
                                                           AsyncIterable<T>... iterables)
        Create a new iterable that has the contents of all the parameters in order.
        Type Parameters:
        T - the source type
        Parameters:
        iterables - a list of iterables to concatenate together
        Returns:
        a new AsyncIterable that starts with all the elements of the first iterable provided, then all the elements of the second iterable and so on
      • mapConcatIterable

        @Nonnull
        public static <T1,​T2> AsyncIterable<T2> mapConcatIterable​(@Nonnull
                                                                        AsyncIterable<T1> iterable,
                                                                        @Nonnull
                                                                        Function<T1,​AsyncIterable<T2>> func,
                                                                        int pipelineSize)
        Maps each value in an iterable to a new iterable and returns the concatenated results. This will start a pipeline of asynchronous requests for up to a requested number of elements of the iterable, in parallel with requests to the mapping results. This does not pipeline the overlapping concatenations, i.e. it won't grab the first item of the second result of func, until it has exhausted the first result of func.
        Type Parameters:
        T1 - the type of the source
        T2 - the type of the destination iterables
        Parameters:
        iterable - the source
        func - mapping function from each element of iterable to a new iterable
        pipelineSize - the number of elements to pipeline
        Returns:
        the results of all the AsyncIterables returned by func for each value of iterable, concatenated
      • filterIterablePipelined

        @Nonnull
        public static <T> AsyncIterable<T> filterIterablePipelined​(@Nonnull
                                                                   AsyncIterable<T> iterable,
                                                                   @Nonnull
                                                                   Function<T,​CompletableFuture<Boolean>> filter,
                                                                   int pipelineSize)
        Filter an iterable, pipelining the asynchronous filter functions. Unlike filterIterable, the filter here is asynchronous. As items comes back from iterable, a pipeline of filter futures is kept without advancing the iterable.
        Type Parameters:
        T - the source type
        Parameters:
        iterable - the source
        filter - only the values of iterable for which the future returned by this filter returns true will be in the resulting iterable
        pipelineSize - the number of filter results to pipeline
        Returns:
        a new AsyncIterable containing the elements of iterable for which filter returns a true future
      • mapIterablePipelined

        @Nonnull
        public static <T1,​T2> AsyncIterable<T2> mapIterablePipelined​(@Nonnull
                                                                           AsyncIterable<T1> iterable,
                                                                           @Nonnull
                                                                           Function<T1,​CompletableFuture<T2>> func,
                                                                           int pipelineSize)
        Maps an AsyncIterable using an asynchronous mapping function
        Type Parameters:
        T1 - the source type
        T2 - the destination type
        Parameters:
        iterable - the source
        func - Maps items of iterable to a new value asynchronously
        pipelineSize - the number of map results to pipeline. As items comes back from iterable, this will have up to this many func futures in waiting before waiting on them without advancing the iterable.
        Returns:
        a new AsyncIterable with the results of applying func to each of the elements of iterable
      • reduce

        @Nullable
        public static <U,​T> CompletableFuture<U> reduce​(@Nonnull
                                                              AsyncIterator<T> iterator,
                                                              U identity,
                                                              BiFunction<U,​? super T,​U> accumulator)
        Reduce contents of iterator to single value.
        Type Parameters:
        U - the result type of the reduction
        T - the element type of the iterator
        Parameters:
        iterator - source of values
        identity - initial value for reduction
        accumulator - function that takes previous reduced value and computes new value combining iterator element
        Returns:
        the reduced result
      • isCompletedNormally

        @API(MAINTAINED)
        public static boolean isCompletedNormally​(@Nonnull
                                                  CompletableFuture<?> future)
        Returns whether the given CompletableFuture has completed normally, i.e., not exceptionally. If the future is yet to complete or if the future completed with an error, then this will return false.
        Parameters:
        future - the future to check for normal completion
        Returns:
        whether the future has completed without exception
      • delayedFuture

        @API(MAINTAINED)
        @Nonnull
        public static CompletableFuture<Void> delayedFuture​(long delay,
                                                            @Nonnull
                                                            TimeUnit unit)
        Creates a future that will be ready after the given delay. Creating the delayed future does not use more than one thread for all of the futures together, and it is safe to create many delayed futures at once. The guarantee given by this function is that the future will not be ready sooner than the delay specified. It may, however, fire after the given delay (especially if there are multiple delayed futures that are trying to fire at once).
        Parameters:
        delay - the time from now to delay execution
        unit - the time unit of the delay parameter
        Returns:
        a CompletableFuture that will fire after the given delay
      • getWithDeadline

        @API(EXPERIMENTAL)
        public static <T> CompletableFuture<T> getWithDeadline​(long deadlineTimeMillis,
                                                               @Nonnull
                                                               Supplier<CompletableFuture<T>> supplier)
        Get a completable future that will either complete within the specified deadline time or complete exceptionally with MoreAsyncUtil.DeadlineExceededException. If deadlineTimeMillis is set to Long.MAX_VALUE, then no deadline is imposed on the future.
        Type Parameters:
        T - the return type for the get operation
        Parameters:
        deadlineTimeMillis - the maximum time to wait for the asynchronous operation to complete, specified in milliseconds
        supplier - the Supplier of the asynchronous result
        Returns:
        a future that will either complete with the result of the asynchronous get operation or complete exceptionally if the deadline is exceeded
      • closeIterator

        @API(MAINTAINED)
        public static void closeIterator​(@Nonnull
                                         Iterator<?> iterator)
        Close the given iterator, or at least cancel it.
        Parameters:
        iterator - iterator to close
      • handleOnException

        public static <V> CompletableFuture<V> handleOnException​(Supplier<CompletableFuture<V>> futureSupplier,
                                                                 Function<Throwable,​CompletableFuture<V>> handlerOnException)
        Handle when futureSupplier encounters an exception when supplying a future, or the future is completed exceptionally. Unlike the "handle" in CompletableFuture, handlerOnException is not executed if the future is successful.
        Type Parameters:
        V - the result type of the future
        Parameters:
        futureSupplier - the supplier of future which needs to be handled
        handlerOnException - the handler when the future encounters an exception
        Returns:
        future that completes exceptionally if the handler has exception