Class ChannelSuppliers

java.lang.Object
io.activej.csp.ChannelSuppliers

public final class ChannelSuppliers extends Object
Provides additional functionality for managing ChannelSuppliers. Includes helper classes: ChannelSupplierOfException, ChannelSupplierOfIterator, ChannelSupplierOfValue, ChannelSupplierEmpty.
  • Constructor Details

    • ChannelSuppliers

      public ChannelSuppliers()
  • Method Details

    • concat

      public static <T> ChannelSupplier<T> concat(ChannelSupplier<? extends T> supplier1, ChannelSupplier<? extends T> supplier2)
      See Also:
    • concat

      @SafeVarargs public static <T> ChannelSupplier<T> concat(ChannelSupplier<? extends T>... suppliers)
      See Also:
    • concat

      public static <T> ChannelSupplier<T> concat(List<ChannelSupplier<? extends T>> suppliers)
      See Also:
    • concat

      public static <T> ChannelSupplier<T> concat(Iterator<? extends ChannelSupplier<? extends T>> iterator)
      Creates a new ChannelSupplier which on get() call returns the result wrapped in promise of the first ChannelSuppliers' promise that was successfully completed with a non-null result. If all the ChannelSuppliers of the iterator have a null promise result, a promise of null will be returned.

      If one of the ChannelSuppliers' promises completes with an exception, all subsequent elements of the iterator will be closed and a promise of exception will be returned.

      Type Parameters:
      T - type of data wrapped in the ChannelSuppliers
      Parameters:
      iterator - an iterator of ChannelSuppliers
      Returns:
      a ChannelSupplier of <T>
    • collect

      public static <T, A, R> io.activej.promise.Promise<R> collect(ChannelSupplier<T> supplier, A initialValue, io.activej.common.function.BiConsumerEx<A,T> accumulator, io.activej.common.function.FunctionEx<A,R> finisher)
      Collects data provided by the supplier asynchronously and returns a promise of accumulated result. This process will be getting values from the supplier, until a promise of null is returned, which represents end of stream.

      If get returns a promise of exception or there was an exception while accumulator accepted values, a promise of exception will be returned and the process will stop.

      Type Parameters:
      T - a data type provided by the supplier
      A - an intermediate accumulation data type
      R - a data type of final result of finisher
      Parameters:
      supplier - a ChannelSupplier which provides data to be collected
      initialValue - a value which will accumulate the results of accumulator
      accumulator - a BiConsumer which may perform some operations over provided by supplier data and accumulates the result to the initialValue
      finisher - a Function which performs the final transformation of the accumulated value
      Returns:
      a promise of accumulated result, transformed by the finisher
    • streamTo

      public static <T> io.activej.promise.Promise<Void> streamTo(io.activej.promise.Promise<ChannelSupplier<T>> supplier, io.activej.promise.Promise<ChannelConsumer<T>> consumer)
    • streamTo

      public static <T> io.activej.promise.Promise<Void> streamTo(io.activej.common.collection.Try<ChannelSupplier<T>> supplier, io.activej.common.collection.Try<ChannelConsumer<T>> consumer)
    • streamTo

      public static <T> io.activej.promise.Promise<Void> streamTo(ChannelSupplier<T> supplier, ChannelConsumer<T> consumer)
      Streams data from the supplier to the consumer until get() of supplier returns a promise of null.

      If get returns a promise of exception or there was an exception while consumer accepted values, a promise of exception will be returned and the process will stop.

      Type Parameters:
      T - a data type of values passed from the supplier to consumer
      Parameters:
      supplier - a supplier which provides some data
      consumer - a consumer which accepts the provided by supplier data
      Returns:
      a promise of null as a marker of completion of stream, or promise of exception, if there was an exception while streaming
    • prefetch

      public static <T> ChannelSupplier<T> prefetch(int count, ChannelSupplier<T> actual)
    • prefetch

      public static <T> ChannelSupplier<T> prefetch(ChannelSupplier<T> actual)
    • remap

      public static <T, V> ChannelSupplier<V> remap(ChannelSupplier<T> supplier, Function<? super T,? extends Iterator<? extends V>> fn)
      Transforms this ChannelSupplier data of type with provided fn, which returns an Iterator of a type. Then provides this value to ChannelSupplier of .
    • inputStreamAsChannelSupplier

      public static ChannelSupplier<io.activej.bytebuf.ByteBuf> inputStreamAsChannelSupplier(Executor executor, io.activej.common.MemSize bufSize, InputStream is)
      See Also:
    • inputStreamAsChannelSupplier

      public static ChannelSupplier<io.activej.bytebuf.ByteBuf> inputStreamAsChannelSupplier(Executor executor, InputStream is)
      Creates an asynchronous ChannelSupplier out of some InputStream.

      Uses a default buffer size of 8 kilobytes

      See Also:
    • inputStreamAsChannelSupplier

      public static ChannelSupplier<io.activej.bytebuf.ByteBuf> inputStreamAsChannelSupplier(Executor executor, int bufSize, InputStream inputStream)
      Creates an asynchronous ChannelSupplier<io.activej.bytebuf.ByteBuf> out of some InputStream.

      I/O operations are executed using a specified Executor, so that the channel supplier operations does not block the eventloop thread.

      A size of a ByteBuf returned by ChannelSupplier.get() will not exceed specified limit

      Passed InputStream will be closed once a resulting ChannelSupplier<io.activej.bytebuf.ByteBuf> is closed or in case an error occurs during channel supplier operations.

      This method should be called from within eventloop thread

      Parameters:
      executor - an executor that will execute blocking I/O
      bufSize - a limit on a size of a byte buf supplied by returned ChannelSupplier<io.activej.bytebuf.ByteBuf>
      inputStream - an InputStream that is transformed into a ChannelSupplier<io.activej.bytebuf.ByteBuf>
      Returns:
      a ChannelSupplier<io.activej.bytebuf.ByteBuf> out ouf an InputStream
    • channelSupplierAsInputStream

      public static InputStream channelSupplierAsInputStream(io.activej.eventloop.Eventloop eventloop, ChannelSupplier<io.activej.bytebuf.ByteBuf> channelSupplier)
      Creates an InputStream out of a ChannelSupplier<io.activej.bytebuf.ByteBuf>.

      Asynchronous operations are executed in a context of a specified Eventloop

      Passed ChannelSupplier<io.activej.bytebuf.ByteBuf> will be closed once a resulting InputStream is closed or in case an error occurs while reading data.

      InputStream's methods are blocking, so they should not be called from an eventloop thread

      Parameters:
      eventloop - an eventloop that will execute asynchronous operations
      channelSupplier - a ChannelSupplier<io.activej.bytebuf.ByteBuf> that is transformed to an InputStream
      Returns:
      an InputStream out ouf a ChannelSupplier<io.activej.bytebuf.ByteBuf>