Class RingBuffer<T>

java.lang.Object
org.apache.nifi.util.RingBuffer<T>

public class RingBuffer<T> extends Object
Thread-safe implementation of a RingBuffer
  • Field Details

    • buffer

      private final Object[] buffer
    • insertionPointer

      private int insertionPointer
    • filled

      private boolean filled
    • rwLock

      private final ReadWriteLock rwLock
    • readLock

      private final Lock readLock
    • writeLock

      private final Lock writeLock
  • Constructor Details

    • RingBuffer

      public RingBuffer(int size)
  • Method Details

    • add

      public T add(T value)
      Adds the given value to the RingBuffer and returns the value that was removed in order to make room.
      Parameters:
      value - the new value to add
      Returns:
      value previously in the buffer
    • getSize

      public int getSize()
    • getSelectedElements

      public List<T> getSelectedElements(RingBuffer.Filter<T> filter)
    • getSelectedElements

      public List<T> getSelectedElements(RingBuffer.Filter<T> filter, int maxElements)
    • countSelectedElements

      public int countSelectedElements(RingBuffer.Filter<T> filter)
    • removeSelectedElements

      public int removeSelectedElements(RingBuffer.Filter<T> filter)
      Removes all elements from the RingBuffer that match the given filter
      Parameters:
      filter - to use for deciding what is removed
      Returns:
      always zero
    • asList

      public List<T> asList()
    • getOldestElement

      public T getOldestElement()
    • getNewestElement

      public T getNewestElement()
    • getElementData

      private T getElementData(int index)
    • forEach

      public void forEach(RingBuffer.ForEachEvaluator<T> evaluator)
      Iterates over each element in the RingBuffer, calling the evaluate method on each element in the RingBuffer. If the Evaluator returns false, the method will skip all remaining elements in the RingBuffer; otherwise, the next element will be evaluated until all elements have been evaluated.
      Parameters:
      evaluator - used to evaluate each item in the ring buffer
    • forEach

      public void forEach(RingBuffer.ForEachEvaluator<T> evaluator, RingBuffer.IterationDirection iterationDirection)
      Iterates over each element in the RingBuffer, calling the evaluate method on each element in the RingBuffer. If the Evaluator returns false, the method will skip all remaining elements in the RingBuffer; otherwise, the next element will be evaluated until all elements have been evaluated.
      Parameters:
      evaluator - the evaluator
      iterationDirection - the order in which to iterate over the elements in the RingBuffer