bb.util
Class BufferFixed<E>

java.lang.Object
  extended by bb.util.BufferFixed<E>

public class BufferFixed<E>
extends Object

Simple data structure whose maximum number of elements stored is fixed to a constant value. This size limit is enforced via a "drop oldest" policy; see add.

Motivation: this class is useful when memory consumption must be bounded and old events may be safely discarded.

This class is not multithread safe.

Author:
Brent Boyer

Nested Class Summary
static class BufferFixed.State<E>
          Used to record a snapshot of the state of a BufferFixed instance.
 
Field Summary
private  Deque<E> deque
          Underlying data structure that holds elements.
private  long numDropped
          Number of old elements that were dropped from deque since the last call to getAndResetState.
private  int sizeMax
          Maximum number of elements that can be stored in deque.
private static int sizeMax_default
          Default value for sizeMax.
 
Constructor Summary
BufferFixed()
          Calls this(sizeMax_default).
BufferFixed(int sizeMax)
          Constructor.
 
Method Summary
 void add(E element)
          Adds element to an internal queue.
 BufferFixed.State<E> getAndResetState()
          Returns a new BufferFixed.State instance which holds the complete state of this instance.
 int size()
          Returns deque.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sizeMax_default

private static final int sizeMax_default
Default value for sizeMax.

See Also:
Constant Field Values

sizeMax

private final int sizeMax
Maximum number of elements that can be stored in deque.

Contract: is > 0.


deque

private Deque<E> deque
Underlying data structure that holds elements.

Contract: is never null.


numDropped

private long numDropped
Number of old elements that were dropped from deque since the last call to getAndResetState.

Contract: is either >= 0 if is a legit value, or else is pegged at Long.MIN_VALUE if overflow occurred.

Constructor Detail

BufferFixed

public BufferFixed()
Calls this(sizeMax_default).


BufferFixed

public BufferFixed(int sizeMax)
            throws IllegalArgumentException
Constructor.

Throws:
IllegalArgumentException - if sizeMax <= 0
Method Detail

add

public void add(E element)
         throws IllegalArgumentException
Adds element to an internal queue. If that queue size already equals sizeMax, then first removes the oldest item from the queue before adding element.

Throws:
IllegalArgumentException - if element is null

getAndResetState

public BufferFixed.State<E> getAndResetState()
Returns a new BufferFixed.State instance which holds the complete state of this instance. The caller is free to manipulate the result in any way (e.g. mutate the BufferFixed.State.deque field, such as remove undesired elements).

Side effect: before return, resets all this instance's state to its initial condition.


size

public int size()
Returns deque.size.