Class BufferedConsumer<T>

  • Type Parameters:
    T - the type of the input to the operation
    All Implemented Interfaces:
    Flushable, Consumer<T>

    public abstract class BufferedConsumer<T>
    extends Object
    implements Consumer<T>, Flushable
    Buffers the consumed elements so that they can be processing by chunks. This class is useful in case performing one action on a list of elements provides better performance than an action for each element of the list.
    Since:
    1.10.0
    • Constructor Detail

      • BufferedConsumer

        protected BufferedConsumer​(int bufferSize)
        Creates a consumer whose buffer with a specified maximum size. Once the size of the buffer of consumed elements reaches this maximum, the flush() method is called to process the buffer.
        Parameters:
        bufferSize - the size of the buffer.
    • Method Detail

      • accept

        public void accept​(T t)
        This implementation appends the element into the buffer. If the buffer of consumed elements reaches this maximum size, the flush() method is called to process the buffer.
        Specified by:
        accept in interface Consumer<T>
      • flush

        public void flush()
        Calls flush(List) to process the buffer of consumed elements at once. The buffer is then reset to empty.
        Specified by:
        flush in interface Flushable
      • flush

        protected abstract void flush​(List<T> buffer)
        Performs the operation on the buffer of consumed elements.
        Parameters:
        buffer - the consumed elements to process.