Interface ChannelConsumer<T>

All Superinterfaces:
io.activej.async.process.AsyncCloseable
All Known Implementing Classes:
AbstractChannelConsumer, ChannelFileWriter, RecyclingChannelConsumer

public interface ChannelConsumer<T> extends io.activej.async.process.AsyncCloseable
This interface represents consumer of data items that should be used serially (each consecutive accept(Object) operation should be called only after previous accept(Object) operation finishes)

After consumer is closed, all subsequent calls to accept(Object) will return a completed exceptionally promise.

If any exception is caught while consuming data items, AsyncCloseable.closeEx(Exception) method should be called. All resources should be freed and the caught exception should be propagated to all related processes.

If accept(Object) takes null as argument, it represents end-of-stream and means that no additional data should be consumed.

  • Field Summary

    Fields inherited from interface io.activej.async.process.AsyncCloseable

    STATIC
  • Method Summary

    Modifier and Type
    Method
    Description
    @NotNull io.activej.promise.Promise<Void>
    accept(T value)
    Consumes a provided value and returns a Promise as a marker of success.
    default @NotNull io.activej.promise.Promise<Void>
    acceptAll(@NotNull Iterator<? extends T> it)
     
    default io.activej.promise.Promise<Void>
    acceptAll(@NotNull List<T> list)
     
    default @NotNull io.activej.promise.Promise<Void>
    acceptAll(T... items)
    Accepts provided items and returns Promise as a marker of completion.
    default io.activej.promise.Promise<Void>
     
     
    filter(Predicate<? super T> predicate)
    Creates a wrapper ChannelConsumer - when its accept(T value) is called, current ChannelConsumer will accept the value only of it passes Predicate test.
    default <V> ChannelConsumer<V>
    map(io.activej.common.function.FunctionEx<? super V,? extends T> fn)
    Creates a wrapper ChannelConsumer - when its accept(T value) is called, fn will be applied to the provided value first and the result of the fn will be accepted by current ChannelConsumer.
    default <V> ChannelConsumer<V>
    mapAsync(Function<? super V,io.activej.promise.Promise<T>> fn)
    Creates a wrapper ChannelConsumer - when its accept(T value) is called, fn will be applied to the provided value first and the result of the fn will be accepted by current ChannelConsumer asynchronously.
    static <T> ChannelConsumer<T>
    of(@NotNull io.activej.async.function.AsyncConsumer<T> consumer)
    Wraps AsyncConsumer in ChannelConsumer.
    static <T> ChannelConsumer<T>
    of(@NotNull io.activej.async.function.AsyncConsumer<T> consumer, @Nullable io.activej.async.process.AsyncCloseable closeable)
    Wraps AsyncConsumer in ChannelConsumer.
    static <T> ChannelConsumer<T>
    ofAnotherEventloop(@NotNull io.activej.eventloop.Eventloop anotherEventloop, @NotNull ChannelConsumer<T> anotherEventloopConsumer)
     
    static <T> ChannelConsumer<T>
    ofConsumer(@NotNull io.activej.common.function.ConsumerEx<T> consumer)
    Wraps a ConsumerEx in ChannelConsumer.
    static <T> ChannelConsumer<T>
    Creates a consumer which always returns Promise of exception when accepts values.
    static <T> ChannelConsumer<T>
    ofLazyProvider(Supplier<? extends ChannelConsumer<T>> provider)
    Returns a ChannelConsumer wrapped in Supplier and calls its accept() when accept() method is called.
    static <T> ChannelConsumer<T>
    ofPromise(io.activej.promise.Promise<? extends ChannelConsumer<T>> promise)
    Unwraps ChannelConsumer of provided Promise.
    static ChannelConsumer<io.activej.bytebuf.ByteBuf>
    static <T> ChannelConsumer<T>
    ofSupplier(io.activej.async.function.AsyncConsumer<ChannelSupplier<T>> supplierConsumer)
     
    static <T> ChannelConsumer<T>
    ofSupplier(io.activej.async.function.AsyncConsumer<ChannelSupplier<T>> supplierConsumer, ChannelQueue<T> queue)
     
    peek(Consumer<? super T> fn)
    Creates a wrapper ChannelConsumer - when its accept(T value) is called, if provided value doesn't equal null, it will be accepted by the provided fn first and then by this ChannelConsumer.
    default <R> R
    Transforms current ChannelConsumer with provided ChannelConsumerTransformer.
    withAcknowledgement(UnaryOperator<io.activej.promise.Promise<Void>> fn)
    Creates a wrapper ChannelConsumer - after its accept(T value) is called and completed, an acknowledgement is returned.
    withExecutor(io.activej.async.process.AsyncExecutor asyncExecutor)
    Creates a wrapper ChannelConsumer which executes current ChannelConsumer's accept(T value) within the asyncExecutor.

    Methods inherited from interface io.activej.async.process.AsyncCloseable

    close, closeEx
  • Method Details

    • accept

      @NotNull @NotNull io.activej.promise.Promise<Void> accept(@Nullable T value)
      Consumes a provided value and returns a Promise as a marker of success.
    • acceptEndOfStream

      default io.activej.promise.Promise<Void> acceptEndOfStream()
    • acceptAll

      @NotNull default @NotNull io.activej.promise.Promise<Void> acceptAll(T... items)
      Accepts provided items and returns Promise as a marker of completion. If one of the items was accepted with an error, subsequent items will be recycled and a Promise of exception will be returned.
    • acceptAll

      @NotNull default @NotNull io.activej.promise.Promise<Void> acceptAll(@NotNull @NotNull Iterator<? extends T> it)
      See Also:
    • acceptAll

      default io.activej.promise.Promise<Void> acceptAll(@NotNull @NotNull List<T> list)
      See Also:
    • of

      static <T> ChannelConsumer<T> of(@NotNull @NotNull io.activej.async.function.AsyncConsumer<T> consumer)
      Wraps AsyncConsumer in ChannelConsumer.
      See Also:
    • of

      static <T> ChannelConsumer<T> of(@NotNull @NotNull io.activej.async.function.AsyncConsumer<T> consumer, @Nullable @Nullable io.activej.async.process.AsyncCloseable closeable)
      Wraps AsyncConsumer in ChannelConsumer.
      Type Parameters:
      T - type of data to be consumed
      Parameters:
      consumer - AsyncConsumer to be wrapped
      closeable - a Cancellable, which will be set to the returned ChannelConsumer
      Returns:
      AbstractChannelConsumer which wraps AsyncConsumer
    • ofConsumer

      static <T> ChannelConsumer<T> ofConsumer(@NotNull @NotNull io.activej.common.function.ConsumerEx<T> consumer)
      Wraps a ConsumerEx in ChannelConsumer.
    • ofException

      static <T> ChannelConsumer<T> ofException(Exception e)
      Creates a consumer which always returns Promise of exception when accepts values.
      Type Parameters:
      T - type of data to be consumed
      Parameters:
      e - an exception which is wrapped in returned Promise when accept() is called
      Returns:
      an AbstractChannelConsumer which always returns Promise of exception when accepts values
    • ofSupplier

      static <T> ChannelConsumer<T> ofSupplier(io.activej.async.function.AsyncConsumer<ChannelSupplier<T>> supplierConsumer)
      See Also:
    • ofSupplier

      static <T> ChannelConsumer<T> ofSupplier(io.activej.async.function.AsyncConsumer<ChannelSupplier<T>> supplierConsumer, ChannelQueue<T> queue)
    • ofPromise

      static <T> ChannelConsumer<T> ofPromise(io.activej.promise.Promise<? extends ChannelConsumer<T>> promise)
      Unwraps ChannelConsumer of provided Promise. If provided Promise is already successfully completed, its result will be returned, otherwise an AbstractChannelConsumer is created, which waits for the Promise to be completed before accepting any value. A Promise of Exception will be returned if Promise was completed with an exception.
      Type Parameters:
      T - type of data to be consumed
      Parameters:
      promise - Promise of ChannelConsumer
      Returns:
      ChannelConsumer b
    • ofAnotherEventloop

      static <T> ChannelConsumer<T> ofAnotherEventloop(@NotNull @NotNull io.activej.eventloop.Eventloop anotherEventloop, @NotNull @NotNull ChannelConsumer<T> anotherEventloopConsumer)
    • ofLazyProvider

      static <T> ChannelConsumer<T> ofLazyProvider(Supplier<? extends ChannelConsumer<T>> provider)
      Returns a ChannelConsumer wrapped in Supplier and calls its accept() when accept() method is called.
      Parameters:
      provider - provider of the ChannelConsumer
      Returns:
      a ChannelConsumer which was wrapped in the provider
    • ofSocket

      static ChannelConsumer<io.activej.bytebuf.ByteBuf> ofSocket(AsyncTcpSocket socket)
      Returns:
      ChannelConsumer of ByteBufs that will be sent to network
    • transformWith

      default <R> R transformWith(ChannelConsumerTransformer<T,R> fn)
      Transforms current ChannelConsumer with provided ChannelConsumerTransformer.
      Type Parameters:
      R - result value after transformation
      Parameters:
      fn - transformer of the ChannelConsumer
      Returns:
      result of transformation applied to the current ChannelConsumer
    • async

      default ChannelConsumer<T> async()
    • withExecutor

      default ChannelConsumer<T> withExecutor(io.activej.async.process.AsyncExecutor asyncExecutor)
      Creates a wrapper ChannelConsumer which executes current ChannelConsumer's accept(T value) within the asyncExecutor.
      Parameters:
      asyncExecutor - executes ChannelConsumer
      Returns:
      a wrapper of current ChannelConsumer which executes in provided asyncExecutor
    • peek

      default ChannelConsumer<T> peek(Consumer<? super T> fn)
      Creates a wrapper ChannelConsumer - when its accept(T value) is called, if provided value doesn't equal null, it will be accepted by the provided fn first and then by this ChannelConsumer.
      Parameters:
      fn - Consumer which accepts the value passed by apply(T value)
      Returns:
      a wrapper ChannelConsumer
    • map

      default <V> ChannelConsumer<V> map(io.activej.common.function.FunctionEx<? super V,? extends T> fn)
      Creates a wrapper ChannelConsumer - when its accept(T value) is called, fn will be applied to the provided value first and the result of the fn will be accepted by current ChannelConsumer. If provide value is null, fn won't be applied.
      Type Parameters:
      V - type of data accepted and returned by the fn and accepted by ChannelConsumer
      Parameters:
      fn - Function to be applied to the value of apply(T value)
      Returns:
      a wrapper ChannelConsumer
    • mapAsync

      default <V> ChannelConsumer<V> mapAsync(Function<? super V,io.activej.promise.Promise<T>> fn)
      Creates a wrapper ChannelConsumer - when its accept(T value) is called, fn will be applied to the provided value first and the result of the fn will be accepted by current ChannelConsumer asynchronously. If provided value is null, fn won't be applied.
      Type Parameters:
      V - type of data accepted by the fn and ChannelConsumer
      Parameters:
      fn - Function to be applied to the value of apply(T value)
      Returns:
      a wrapper ChannelConsumer
    • filter

      default ChannelConsumer<T> filter(Predicate<? super T> predicate)
      Creates a wrapper ChannelConsumer - when its accept(T value) is called, current ChannelConsumer will accept the value only of it passes Predicate test.
      Parameters:
      predicate - Predicate which is used to filter accepted value
      Returns:
      a wrapper ChannelConsumer
    • withAcknowledgement

      default ChannelConsumer<T> withAcknowledgement(UnaryOperator<io.activej.promise.Promise<Void>> fn)
      Creates a wrapper ChannelConsumer - after its accept(T value) is called and completed, an acknowledgement is returned. An acknowledgement is a SettablePromise which is accepted by the provided fn and then materialized.
      Parameters:
      fn - a function applied to the SettablePromise which is then materialized and returned
      Returns:
      a wrapper ChannelConsumer