Class CircularBuffer<E>

java.lang.Object
org.wso2.carbon.utils.logging.CircularBuffer<E>

public class CircularBuffer<E> extends Object
This is a Circular Buffer implementation. In this implementaion it is assumed that items will never be removed from this buffer. This can be used for a case such as a Rolling Log. A client can request the latest 'n' number of items that are stored in this buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a circular buffer with the maximum allowed size
    CircularBuffer(int size)
    Create a circular buffer with the given size
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append(E element)
    Append elements while preserving the circular nature of the buffer.
    void
    Clear the circular buffer and reset the indices.
    get(int amount)
    Retrieve the given amount of elements from the circular buffer.
    getObjects(int amount)
    This method is added for backward compatibility.
    int
    Return the capacity of the circular buffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CircularBuffer

      public CircularBuffer(int size)
      Create a circular buffer with the given size
      Parameters:
      size - - fixed size of the buffer
    • CircularBuffer

      public CircularBuffer()
      Create a circular buffer with the maximum allowed size
  • Method Details

    • append

      public void append(E element)
      Append elements while preserving the circular nature of the buffer.
      Parameters:
      element - - element to be appended
    • get

      public List<E> get(int amount)
      Retrieve the given amount of elements from the circular buffer. This is a forgiving operation, if the amount asked is greater than the size of the buffer it will return all the available elements
      Parameters:
      amount - - no of elements to return
      Returns:
      - a list of elements
    • getObjects

      public Object[] getObjects(int amount)
      This method is added for backward compatibility.
      Parameters:
      amount - - amount of elements to return from the buffer
      Returns:
      - an object array of amount number of elements in the buffer
    • clear

      public void clear()
      Clear the circular buffer and reset the indices.
    • getSize

      public int getSize()
      Return the capacity of the circular buffer. This is set during buffer initialization.
      Returns:
      - capacity of the buffer