Package org.bsc.async

Interface AsyncGenerator<E>

    • Method Detail

      • next

        AsyncGenerator.Data<E> next()
        Retrieves the next asynchronous element.
        Returns:
        the next element from the generator
      • empty

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

        static <E,​U> AsyncGenerator<U> map​(Iterator<E> iterator,
                                                 Function<E,​CompletableFuture<U>> mapFunction)
        create a generator, mapping each element to an asynchronous counterpart.
        Type Parameters:
        E - the type of elements in the collection
        U - the type of elements in the CompletableFuture
        Parameters:
        iterator - the elements iterator
        mapFunction - the function to map elements to CompletableFuture
        Returns:
        an AsyncGenerator instance with mapped elements
      • collect

        static <E,​U> AsyncGenerator<U> collect​(Iterator<E> iterator,
                                                     BiConsumer<E,​Consumer<CompletableFuture<U>>> consumer)
        Collects asynchronous elements from an iterator.
        Type Parameters:
        E - the type of elements in the iterator
        U - the type of elements in the CompletableFuture
        Parameters:
        iterator - the iterator containing elements to collect
        consumer - the function to consume elements and add them to the accumulator
        Returns:
        an AsyncGenerator instance with collected elements
      • map

        static <E,​U> AsyncGenerator<U> map​(Collection<E> collection,
                                                 Function<E,​CompletableFuture<U>> mapFunction)
        create a generator, mapping each element to an asynchronous counterpart.
        Type Parameters:
        E - the type of elements in the collection
        U - the type of elements in the CompletableFuture
        Parameters:
        collection - the collection of elements to map
        mapFunction - the function to map elements to CompletableFuture
        Returns:
        an AsyncGenerator instance with mapped elements
      • collect

        static <E,​U> AsyncGenerator<U> collect​(Collection<E> collection,
                                                     BiConsumer<E,​Consumer<CompletableFuture<U>>> consumer)
        Collects asynchronous elements from a collection.
        Type Parameters:
        E - the type of elements in the iterator
        U - the type of elements in the CompletableFuture
        Parameters:
        collection - the iterator containing elements to collect
        consumer - the function to consume elements and add them to the accumulator
        Returns:
        an AsyncGenerator instance with collected elements
      • toCompletableFuture

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

        default CompletableFuture<Object> forEachAsync​(Consumer<E> consumer,
                                                       Executor executor)
        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
        executor - the executor to use for the asynchronous iteration
        Returns:
        a CompletableFuture representing the completion of the iteration process. Return the result value
      • 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.
      • collectAsync

        default <R extends List<E>> CompletableFuture<Object> collectAsync​(R result,
                                                                           Consumer<E> consumer,
                                                                           Executor executor)
        Collects elements from the AsyncGenerator asynchronously into a list.
        Type Parameters:
        R - the type of the result list
        Parameters:
        result - the result list to collect elements into
        consumer - the consumer function for processing elements
        executor - the executor to use for the asynchronous collection
        Returns:
        a CompletableFuture representing the completion of the collection process
      • collectAsync

        default <R extends List<E>> CompletableFuture<Object> collectAsync​(R result,
                                                                           Consumer<E> consumer)
        Collects elements from the AsyncGenerator asynchronously into a list.
        Type Parameters:
        R - the type of the result list
        Parameters:
        result - the result list to collect elements into
        consumer - the consumer function for processing elements
        Returns:
        a CompletableFuture representing the completion of the collection process
      • 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