Interface AsyncIterator<T>

  • Type Parameters:
    T - Element type.
    All Known Implementing Classes:
    ContinuationTokenAsyncIterator

    public interface AsyncIterator<T>
    Defines an Iterator for which every invocation results in an async call with a delayed response.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default java.util.Iterator<T> asIterator()
      Returns an Iterator that wraps this instance.
      default AsyncIterator<T> asSequential​(java.util.concurrent.Executor executor)
      Returns a new AsyncIterator that wraps this instance which serializes the execution of all calls to getNext() (no two executions of getNext() will ever execute at the same time; they will be run in the order of invocation, but only after the previous one completes).
      default java.util.concurrent.CompletableFuture<java.lang.Void> collectRemaining​(java.util.function.Predicate<? super T> collector)
      Processes the remaining elements in the AsyncIterator until the specified Predicate returns false.
      default java.util.concurrent.CompletableFuture<java.lang.Void> forEachRemaining​(java.util.function.Consumer<? super T> consumer, java.util.concurrent.Executor executor)
      Processes the remaining elements in the AsyncIterator.
      java.util.concurrent.CompletableFuture<T> getNext()
      Attempts to get the next element in the iteration.
      default <U> AsyncIterator<U> thenApply​(@NonNull java.util.function.Function<? super T,​? extends U> converter)
      Returns a new AsyncIterator that wraps this instance and converts all items from this one into items of a new type.
      default <U> AsyncIterator<U> thenCompose​(@NonNull java.util.function.Function<? super T,​java.util.concurrent.CompletableFuture<U>> converter)
      Returns a new AsyncIterator that wraps this instance and converts all items from this one into items of a new type using an async call.
    • Method Detail

      • getNext

        java.util.concurrent.CompletableFuture<T> getNext()
        Attempts to get the next element in the iteration. Note: since this is an async call, it is possible to invoke this method before the previous call to it completed; in this case the behavior is undefined and, depending on the actual implementation, the internal state of the AsyncIterator may get corrupted. Consider invoking asSequential(Executor) which will provide a thin wrapper on top of this instance that serializes calls to this method.
        Returns:
        A CompletableFuture that, when completed, will contain the next element in the iteration. If the iteration has reached its end, this will complete with null. If an exception occurred, this will be completed exceptionally with the causing exception, and the iteration will end.
      • forEachRemaining

        default java.util.concurrent.CompletableFuture<java.lang.Void> forEachRemaining​(java.util.function.Consumer<? super T> consumer,
                                                                                        java.util.concurrent.Executor executor)
        Processes the remaining elements in the AsyncIterator.
        Parameters:
        consumer - A Consumer that will be invoked for each remaining element. The consumer will be invoked using the given Executor, but any new invocation will wait for the previous invocation to complete.
        executor - An Executor to run async tasks on.
        Returns:
        A CompletableFuture that, when completed, will indicate that the processing is complete.
      • asSequential

        default AsyncIterator<T> asSequential​(java.util.concurrent.Executor executor)
        Returns a new AsyncIterator that wraps this instance which serializes the execution of all calls to getNext() (no two executions of getNext() will ever execute at the same time; they will be run in the order of invocation, but only after the previous one completes).
        Parameters:
        executor - An Executor to run async tasks on.
        Returns:
        A new AsyncIterator.
      • collectRemaining

        default java.util.concurrent.CompletableFuture<java.lang.Void> collectRemaining​(java.util.function.Predicate<? super T> collector)
        Processes the remaining elements in the AsyncIterator until the specified Predicate returns false.
        Parameters:
        collector - A Predicate that decides if iterating over the collection can continue.
        Returns:
        A CompletableFuture that, when completed, will indicate that the processing is complete.
      • thenApply

        default <U> AsyncIterator<U> thenApply​(@NonNull
                                               @NonNull java.util.function.Function<? super T,​? extends U> converter)
        Returns a new AsyncIterator that wraps this instance and converts all items from this one into items of a new type.
        Type Parameters:
        U - New type.
        Parameters:
        converter - A Function that will convert T to U.
        Returns:
        A new AsyncIterator.
      • thenCompose

        default <U> AsyncIterator<U> thenCompose​(@NonNull
                                                 @NonNull java.util.function.Function<? super T,​java.util.concurrent.CompletableFuture<U>> converter)
        Returns a new AsyncIterator that wraps this instance and converts all items from this one into items of a new type using an async call.
        Type Parameters:
        U - New type.
        Parameters:
        converter - A Function that will convert T to U.
        Returns:
        A new AsyncIterator.
      • asIterator

        default java.util.Iterator<T> asIterator()
        Returns an Iterator that wraps this instance.
        Returns:
        A new BlockingAsyncIterator wrapping this instance.