qflib 0.99

de.qfs.lib.util
Class RingBuffer

java.lang.Object
  extended byde.qfs.lib.util.RingBuffer

public class RingBuffer
extends java.lang.Object

A simple thread-safe FIFO buffer that stores Objects in an array. If the buffer overflows old Objects are dropped. To pop all Objects from the RingBuffer you can either synchronize on the RingBuffer object - which is deadlock prone - or call popAll.

Since:
0.99.0
Author:
Gregor Schmid

Constructor Summary
RingBuffer(int size)
          Create a new RingBuffer.
 
Method Summary
 int getMaxSize()
          Get the total size of the RingBuffer.
 java.lang.Object pop()
          Pop an object from the back of the buffer.
 java.lang.Object[] popAll()
          Pop all objects from the RingBuffer.
 void push(java.lang.Object object)
          Push an Object to the front of the buffer.
 void setMaxSize(int newsize)
          Set the total size of the RingBuffer.
 int size()
          Get the number of Objects stored in the RingBuffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RingBuffer

public RingBuffer(int size)
Create a new RingBuffer.

Parameters:
size - The size of the buffer.
Method Detail

push

public void push(java.lang.Object object)
Push an Object to the front of the buffer.

Parameters:
object - The object to push.

pop

public java.lang.Object pop()
                     throws java.lang.IllegalStateException
Pop an object from the back of the buffer.

Returns:
The popped object.
Throws:
java.lang.IllegalStateException - If the buffer is empty.

popAll

public java.lang.Object[] popAll()
Pop all objects from the RingBuffer.

Returns:
An array with all the objects on the buffer or null if the buffer is empty.

size

public int size()
Get the number of Objects stored in the RingBuffer.

Returns:
The number of Objects stored in the RingBuffer.

getMaxSize

public int getMaxSize()
Get the total size of the RingBuffer.

Returns:
The total size of the RingBuffer.

setMaxSize

public void setMaxSize(int newsize)
Set the total size of the RingBuffer.

Parameters:
newsize - The total size to set.

qflib 0.99