info.monitorenter.util.collections
Class RingBufferArray

java.lang.Object
  extended by info.monitorenter.util.collections.RingBufferArrayFast
      extended by info.monitorenter.util.collections.RingBufferArray
All Implemented Interfaces:
IRingBuffer, Serializable, Cloneable

public class RingBufferArray
extends RingBufferArrayFast

A RingBuffer can be used to store a limited number of entries of any type within a buffer.

As soon as the maximum number of entries is reached, the next entry is added to the end and the first entry is removed from it. In this case, all elements are stored in a Object[]. But don't worry there will not be a single call to System.arraycopy caused by invocation of the add(Object element)- method. Internal indexes into the array for the head and tail allow to reuse the same memory again and again.
No element is lost: If setBufferSize(int asize) decreases the size of the buffer and it will get smaller than the actual amount of elements stored, they will get cached until removed.

For allowing high performance single-threaded use this implementation and the implementations of the retrieveable Iterator- instances are not synchronized at all.

Version:
$Revision: 1.5 $
Author:
Achim Westermann
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface info.monitorenter.util.collections.IRingBuffer
IRingBuffer.RingBufferException
 
Field Summary
protected  List m_pendingremove
          Elements that stores elements that have to be removed due to an invocation to setBufferSize(int) with a smaller argument than the amount of elements stored.
 
Fields inherited from class info.monitorenter.util.collections.RingBufferArrayFast
DEBUG, m_buffer, m_empty, m_headpointer, m_size, m_tailpointer
 
Constructor Summary
RingBufferArray(int aSize)
          Constructs a RingBuffer with the given size.
 
Method Summary
 boolean isEmpty()
          Tests wether no elements are stored in the buffer.
 Iterator iteratorF2L()
          Returns an Iterator that will return the elements in exactly the inverse order the subsequent call to remove() would do.
 Iterator iteratorL2F()
          Returns an Iterator that will return the elements in exactly the order the subsequent call to remove() would do.
 Object remove()
          Removes the oldest element from the buffer.
 Object[] removeAll()
          Clears the buffer.
 void setBufferSize(int newSize)
          Sets a new buffer- size.
 int size()
          Returns the actual amount of elements stored in the buffer.
 String toString()
          Returns a string representation of the RingBuffer and it's contents.
 
Methods inherited from class info.monitorenter.util.collections.RingBufferArrayFast
add, clear, clone, getBufferSize, getOldest, getYoungest, isFull
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

m_pendingremove

protected List m_pendingremove
Elements that stores elements that have to be removed due to an invocation to setBufferSize(int) with a smaller argument than the amount of elements stored.

Constructor Detail

RingBufferArray

public RingBufferArray(int aSize)
Constructs a RingBuffer with the given size.

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

isEmpty

public boolean isEmpty()
Description copied from interface: IRingBuffer
Tests wether no elements are stored in the buffer.

Specified by:
isEmpty in interface IRingBuffer
Overrides:
isEmpty in class RingBufferArrayFast
Returns:
true if no element is stored in the buffer.
See Also:
IRingBuffer.isEmpty()

iteratorF2L

public Iterator iteratorF2L()
Description copied from class: RingBufferArrayFast
Returns an Iterator that will return the elements in exactly the inverse order the subsequent call to remove() would do.

The youngest elements are returned first. The Iterator returned is not thread- safe!

Specified by:
iteratorF2L in interface IRingBuffer
Overrides:
iteratorF2L in class RingBufferArrayFast
Returns:
an Iterator that will return the elements in exactly the inverse order the subsequent call to remove() would do.
See Also:
IRingBuffer.iteratorF2L()

iteratorL2F

public Iterator iteratorL2F()
Description copied from class: RingBufferArrayFast
Returns an Iterator that will return the elements in exactly the order the subsequent call to remove() would do.

The oldest elements are returned first. The Iterator returned is not thread- safe!

Specified by:
iteratorL2F in interface IRingBuffer
Overrides:
iteratorL2F in class RingBufferArrayFast
Returns:
an Iterator that will return the elements in exactly the order the subsequent call to remove() would do.
See Also:
IRingBuffer.iteratorL2F()

remove

public Object remove()
Description copied from interface: IRingBuffer
Removes the oldest element from the buffer.

Specified by:
remove in interface IRingBuffer
Overrides:
remove in class RingBufferArrayFast
Returns:
the removed oldest element from the buffer.
See Also:
IRingBuffer.remove()

removeAll

public Object[] removeAll()
Description copied from interface: IRingBuffer
Clears the buffer. It will return all of it's stored elements.

Specified by:
removeAll in interface IRingBuffer
Overrides:
removeAll in class RingBufferArrayFast
Returns:
all removed elements.
See Also:
IRingBuffer.removeAll()

setBufferSize

public void setBufferSize(int newSize)
Sets a new buffer- size.

A new size is assigned but the elements "overhanging" are returned by the Object remove()- method first. This may take time until the buffer has its actual size again. Don't pretend on calling this method for saving of memory very often as the whole buffer has to be copied into a new array every time- and if newSize < getSize() additional the overhanging elements references have to be moved to the internal List pendingremove.

Specified by:
setBufferSize in interface IRingBuffer
Overrides:
setBufferSize in class RingBufferArrayFast
Parameters:
newSize - the new size of the buffer.

size

public int size()
Description copied from interface: IRingBuffer
Returns the actual amount of elements stored in the buffer.

Specified by:
size in interface IRingBuffer
Overrides:
size in class RingBufferArrayFast
Returns:
the actual amount of elements stored in the buffer.
See Also:
IRingBuffer.size()

toString

public String toString()
Returns a string representation of the RingBuffer and it's contents.

Don't call this in your application too often: hard arraycopy - operation an memalloc are triggered.

Overrides:
toString in class RingBufferArrayFast
Returns:
a string representation of the RingBuffer and it's contents.


Copyright © 2001 - 2007 LGPL, All Rights Footloose.