Package io.pravega.common.util
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 anIteratorthat wraps this instance.default AsyncIterator<T>asSequential(java.util.concurrent.Executor executor)Returns a newAsyncIteratorthat wraps this instance which serializes the execution of all calls togetNext()(no two executions ofgetNext()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 specifiedPredicatereturns 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 newAsyncIteratorthat 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 newAsyncIteratorthat 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 theAsyncIteratormay get corrupted. Consider invokingasSequential(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 newAsyncIteratorthat wraps this instance which serializes the execution of all calls togetNext()(no two executions ofgetNext()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 specifiedPredicatereturns false.- Parameters:
collector- APredicatethat 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 newAsyncIteratorthat wraps this instance and converts all items from this one into items of a new type.- Type Parameters:
U- New type.- Parameters:
converter- AFunctionthat 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 newAsyncIteratorthat 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- AFunctionthat will convert T to U.- Returns:
- A new
AsyncIterator.
-
asIterator
default java.util.Iterator<T> asIterator()
Returns anIteratorthat wraps this instance.- Returns:
- A new
BlockingAsyncIteratorwrapping this instance.
-
-