Class CallbackPipe<T>

  • Type Parameters:
    T - The item data type
    All Implemented Interfaces:
    Closeable, AutoCloseable, BasePipe, Pipe<T>

    public class CallbackPipe<T>
    extends DelegatePipe<T>
    A pipe invoking a callback method per item passed through it. The callback is never invoked on null values which indicate end of input pipe. Also supports a termination action, which is run exactly once when pipe termination is detected. Pipe output is same as pipe input.
    Author:
    Eyal Schneider
    • Constructor Detail

      • CallbackPipe

        public CallbackPipe​(Pipe<T> input,
                            FailableConsumer<? super T,​? extends PipeException> itemCallback,
                            FailableRunnable<? extends PipeException> terminationCallback)
        Constructor
        Parameters:
        input - The input pipe
        itemCallback - The callback to invoke. Allows throwing a PipeException to signal pipeline termination.
        terminationCallback - A termination callback, executed once, when pipe termination is detected. Note that this callback can still fail the processing with PipeException if needed. If input pipe isn't fully consumed, this callback won't be triggered.
      • CallbackPipe

        public CallbackPipe​(Pipe<T> input,
                            FailableConsumer<? super T,​? extends PipeException> itemCallback)
        Constructor
        Parameters:
        input - The input pipe
        itemCallback - The callback to invoke
      • CallbackPipe

        public CallbackPipe​(Pipe<T> input,
                            FailableRunnable<? extends PipeException> terminationCallback)
        Constructor
        Parameters:
        input - The input pipe
        terminationCallback - A termination callback, executed once, when pipe termination is detected. If input pipe isn't fully consumed, this callback won't be triggered.
    • Method Detail

      • next

        public T next()
               throws PipeException,
                      InterruptedException
        Specified by:
        next in interface Pipe<T>
        Overrides:
        next in class DelegatePipe<T>
        Returns:
        The next item in this pipe output, or null if the output end has been reached. May be a blocking operation.
        Throws:
        PipeException - In case of pipe errors in this pipe or somewhere up-stream while trying to prepare next item to return.
        InterruptedException - In case that the operation has been interrupted by another thread.
      • peek

        public T peek()
               throws PipeException
        Specified by:
        peek in interface Pipe<T>
        Overrides:
        peek in class DelegatePipe<T>
        Returns:
        The next item in the pipe's output. Does not remove it, so next call to next() will return it.
        Throws:
        PipeException - In case of pipe errors in this pipe or somewhere up-stream while trying to prepare next item to return.