Interface ChannelConsumer<T>

All Superinterfaces:
io.activej.async.process.AsyncCloseable
All Known Implementing Classes:
AbstractChannelConsumer, ChannelFileWriter, OfAnotherReactor, OfAsyncConsumer, OfConsumer, OfException, OfLazyProvider, OfOutputStream, OfPromise, Recycling

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
    io.activej.promise.Promise<Void>
    accept(T value)
    Consumes a provided value and returns a Promise as a marker of success.
    default io.activej.promise.Promise<Void>
    acceptAll(Iterator<? extends T> it)
    Passes iterator's values to the output until it hasNext(), then returns a promise of null as a marker of completion.
    default io.activej.promise.Promise<Void>
    acceptAll(List<T> list)
     
    default 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.
    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 executor)
    Creates a wrapper ChannelConsumer which executes current ChannelConsumer's accept(T value) within the executor.

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

    close, closeEx
  • Method Details

    • accept

      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

      default 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

      default io.activej.promise.Promise<Void> acceptAll(Iterator<? extends T> it)
      Passes iterator's values to the output until it hasNext(), then returns a promise of null as a marker of completion.

      If there was an exception while accepting iterator, a promise of exception will be returned.

      Parameters:
      it - an Iterator which provides some values
      Returns:
      a promise of null as a marker of completion
    • acceptAll

      default io.activej.promise.Promise<Void> acceptAll(List<T> list)
      See Also:
    • 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 executor)
      Creates a wrapper ChannelConsumer which executes current ChannelConsumer's accept(T value) within the executor.
      Parameters:
      executor - executes ChannelConsumer
      Returns:
      a wrapper of current ChannelConsumer which executes in provided executor
    • 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