Package org.bsc.async

Interface AsyncGenerator<E>

Type Parameters:
E - the type of elements. The generator will emit CompletableFutures<E> elements
All Superinterfaces:
Iterable<E>
All Known Subinterfaces:
AsyncGenerator.Cancellable<E>
All Known Implementing Classes:
AsyncGenerator.Base, AsyncGenerator.BaseCancellable, AsyncGenerator.WithEmbed, AsyncGenerator.WithResult, AsyncGeneratorQueue.Generator, GeneratorSubscriber

public interface AsyncGenerator<E> extends Iterable<E>
An asynchronous generator interface that allows generating asynchronous elements.
  • Method Details

    • resultValue

      static Optional<Object> resultValue(AsyncGenerator<?> generator)
    • resultValue

      static Optional<Object> resultValue(Iterator<?> iterator)
    • next

      Retrieves the next asynchronous element.
      Returns:
      the next element from the generator
    • executor

      Executor executor()
    • map

      default <U> AsyncGenerator<U> map(Function<E,U> mapFunction)
      Maps the elements of this generator to a new asynchronous generator.
      Type Parameters:
      U - the type of elements in the new generator
      Parameters:
      mapFunction - the function to map elements to a new asynchronous counterpart
      Returns:
      a generator with mapped elements
    • flatMap

      default <U> AsyncGenerator<U> flatMap(Function<E,CompletableFuture<U>> mapFunction)
      Maps the elements of this generator to a new asynchronous generator, and flattens the resulting nested generators.
      Type Parameters:
      U - the type of elements in the new generator
      Parameters:
      mapFunction - the function to map elements to a new asynchronous counterpart
      Returns:
      a generator with mapped and flattened elements
    • forEachAsync

      default CompletableFuture<Object> forEachAsync(Consumer<E> consumer)
      Asynchronously iterates over the elements of the AsyncGenerator and applies the given consumer to each element.
      Parameters:
      consumer - the consumer function to be applied to each element
      Returns:
      a CompletableFuture representing the completion of the iteration process.
    • reduce

      default <R> CompletableFuture<R> reduce(R result, BiFunction<R,E,R> reducer)
    • reduceAsync

      default <R> CompletableFuture<R> reduceAsync(R result, BiFunction<R,E,R> reducer)
    • toCompletableFuture

      default CompletableFuture<Object> toCompletableFuture()
      Converts the AsyncGenerator to a CompletableFuture.
      Returns:
      a CompletableFuture representing the completion of the AsyncGenerator
    • stream

      default Stream<E> stream()
      Returns a sequential Stream with the elements of this AsyncGenerator. Each CompletableFuture is resolved and then make available to the stream.
      Returns:
      a Stream of elements from the AsyncGenerator
    • iterator

      default Iterator<E> iterator()
      Returns an iterator over the elements of this AsyncGenerator. Each call to `next` retrieves the next "resolved" asynchronous element from the generator.
      Specified by:
      iterator in interface Iterable<E>
      Returns:
      an iterator over the elements of this AsyncGenerator
    • empty

      static <E> AsyncGenerator<E> empty()
      Returns an empty AsyncGenerator.
      Type Parameters:
      E - the type of elements
      Returns:
      an empty AsyncGenerator
    • from

      static <E> AsyncGenerator<E> from(Iterator<E> iterator)
      Collects asynchronous elements from an iterator.
      Type Parameters:
      E - the type of elements in the iterator
      Parameters:
      iterator - the iterator containing elements to collect
      Returns:
      an AsyncGenerator instance with collected elements