net.floodlightcontroller.debugevent
Class CircularBuffer<T>

java.lang.Object
  extended by net.floodlightcontroller.debugevent.CircularBuffer<T>
All Implemented Interfaces:
java.lang.Iterable<T>

public class CircularBuffer<T>
extends java.lang.Object
implements java.lang.Iterable<T>


Field Summary
protected static org.slf4j.Logger log
           
 
Constructor Summary
CircularBuffer(int capacity)
           
 
Method Summary
 T add(T e)
          Adding an element to the circular buffer implies adding the element to the tail of the deque.
 java.util.ArrayList<T> addAll(java.util.ArrayList<T> elist, int uptoIndex)
          The basic idea here is that an ArrayList has been passed in, which may or may not have a size bigger that the actual number of elements that are meant to be flushed to the Circular Buffer.
 void clear()
          Atomically removes all elements in the circular buffer
 java.util.Iterator<T> iterator()
          Returns an iterator over the elements in the circular buffer in proper sequence.
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.slf4j.Logger log
Constructor Detail

CircularBuffer

public CircularBuffer(int capacity)
Method Detail

add

public T add(T e)
Adding an element to the circular buffer implies adding the element to the tail of the deque. In doing so, if the capacity of the buffer has been exhausted, then the deque's head should be removed to preserve the circular nature of the buffer. A LinkedBlockingDeque is used because of its concurrent nature and the fact that adding to tail and removing from head are both O(1) operations. The removed head is returned to the caller for reuse.

Parameters:
e - the element to be added
Returns:
removed element (for reuse) or null

addAll

public java.util.ArrayList<T> addAll(java.util.ArrayList<T> elist,
                                     int uptoIndex)
The basic idea here is that an ArrayList has been passed in, which may or may not have a size bigger that the actual number of elements that are meant to be flushed to the Circular Buffer. Thus the 'uptoIndex' parameter specifies the number of elements that need to be flushed starting from index 0. Note that after flushing, the circular buffer may return a memory unit (of type T) for reuse in the list, if the circular buffer popped off memory to preserve its circular nature. Or it may just return null if nothing was popped off. Either way, the list that is returned by this method, is of the SAME SIZE as the list passed in, as ArrayLists can hold null elements. The only difference is that the list returned will have elements that reference old popped-off memory from the circular-buffer or null.

Parameters:
elist - the ArrayList to flush into the circular buffer.
uptoIndex - flush starting from index 0 upto but not including index 'uptoIndex'.
Returns:
the 'elist' passed in with members now pointing to to null or old-memory for reuse. The returned list if of the same size as 'elist'.

iterator

public java.util.Iterator<T> iterator()
Returns an iterator over the elements in the circular buffer in proper sequence. The elements will be returned in order from most-recent to oldest. The returned Iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
iterator in interface java.lang.Iterable<T>

size

public int size()

clear

public void clear()
Atomically removes all elements in the circular buffer