Class RingBufferArrayFast<T>
- java.lang.Object
-
- info.monitorenter.util.collections.RingBufferArrayFast<T>
-
- Type Parameters:
T
- the type of instances to store.
- All Implemented Interfaces:
IRingBuffer<T>
,java.io.Serializable
,java.lang.Cloneable
,java.lang.Iterable<T>
- Direct Known Subclasses:
RingBufferArray
public class RingBufferArrayFast<T> extends java.lang.Object implements java.lang.Cloneable, IRingBuffer<T>
Fast ring buffer implementation.This implementation differs from the
RingBufferArray
in one point:
IfsetBufferSize(int asize)
decreases the size of the buffer and it will get smaller than the actual amount of elements stored, they will get lost. This avoids the need for an internal List to store elements overhanging. Some tests may be left out that may speed up thisIRingBuffer
. Adding 5000000 elements was about 25 % faster compared to theRingBufferArray
on an Athlon 1200, 256 MB RAM.For allowing high performance single-threaded use this implementation and the implementations of the retrievable
Iterator
- instances are not synchronized at all.- Author:
- Achim Westermann
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
RingBufferArrayFast.ARingBufferIterator
Base for ring buffer iterators that has access to the ring buffer by being an non-static inner class.-
Nested classes/interfaces inherited from interface info.monitorenter.util.collections.IRingBuffer
IRingBuffer.RingBufferException
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
DEBUG
Flip the switch and you will see how the compiler changes the size of the class file.protected java.lang.Object[]
m_buffer
The internal array used as buffer.protected boolean
m_empty
Flag that marks whether this buffer is empty or not.protected int
m_headpointer
The internal index to buffer where the next element is going to be placed (not placed yet!).protected int
m_size
The internal size of the buffer.protected int
m_tailpointer
The internal index to buffer where the next element is going to be read.
-
Constructor Summary
Constructors Constructor Description RingBufferArrayFast(int aSize)
Constructs a RingBuffer with the given size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
add(T anObject)
Adds an element to the ring buffer, potentially removing the first element to make more room.void
clear()
Fast method to clear the buffer - only needs to set three primitive members.protected java.lang.Object
clone()
boolean
equals(java.lang.Object obj)
int
getBufferSize()
Returns the absolute amount of space in the buffer.T
getOldest()
Returns the oldest element from the buffer.T
getYoungest()
Returns the last element added.int
hashCode()
boolean
isEmpty()
Tests whether no elements are stored in the buffer.boolean
isFull()
Returns true if no more space in the buffer is available.java.util.Iterator<T>
iterator()
Delegates to
.iteratorL2F()
java.util.Iterator<T>
iteratorF2L()
Returns anIterator
that will return the elements in exactly the inverse order the subsequent call toremove()
would do.java.util.Iterator<T>
iteratorL2F()
Returns anIterator
that will return the elements in exactly the order the subsequent call toremove()
would do.T
remove()
Removes the oldest element from the buffer.T[]
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.java.lang.String
toString()
Returns a string representation of the RingBuffer and it's contents.
-
-
-
Field Detail
-
DEBUG
public static final boolean DEBUG
Flip the switch and you will see how the compiler changes the size of the class file.- See Also:
- Constant Field Values
-
m_buffer
protected java.lang.Object[] m_buffer
The internal array used as buffer.
-
m_empty
protected boolean m_empty
Flag that marks whether this buffer is empty or not.headpointer | +---+---+---+---+ | 0 | 1 | 2 | 3 | +---+---+---+---+ | tailpointer From where to where are the elements? Where is empty space? empty == true: 0 elements are contained: buffer empty empty == false: 4 elements are contained: buffer full remember: -the headpointer points to the space where the next element will be inserted. -the tailpointer points to the space to read the next element from.
-
m_headpointer
protected int m_headpointer
The internal index to buffer where the next element is going to be placed (not placed yet!).
-
m_size
protected int m_size
The internal size of the buffer.For performance reasons the size of the buffer -1!
-
m_tailpointer
protected int m_tailpointer
The internal index to buffer where the next element is going to be read.
-
-
Method Detail
-
add
public T add(T anObject)
Adds an element to the ring buffer, potentially removing the first element to make more room.- Specified by:
add
in interfaceIRingBuffer<T>
- Parameters:
anObject
- the instance to add.- Returns:
- the oldest Object, if RingBuffer was filled with 'maxsize' elements before, or null.
-
clear
public void clear()
Fast method to clear the buffer - only needs to set three primitive members.- Specified by:
clear
in interfaceIRingBuffer<T>
- See Also:
IRingBuffer.clear()
-
clone
protected java.lang.Object clone() throws java.lang.CloneNotSupportedException
- Overrides:
clone
in classjava.lang.Object
- Throws:
java.lang.CloneNotSupportedException
- See Also:
Object.clone()
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
- See Also:
Object.equals(java.lang.Object)
-
getBufferSize
public int getBufferSize()
Description copied from interface:IRingBuffer
Returns the absolute amount of space in the buffer.- Specified by:
getBufferSize
in interfaceIRingBuffer<T>
- Returns:
- the absolute amount of space in the buffer.
- See Also:
IRingBuffer.getBufferSize()
-
getOldest
public T getOldest() throws IRingBuffer.RingBufferException
Description copied from interface:IRingBuffer
Returns the oldest element from the buffer. This method does not remove the element.- Specified by:
getOldest
in interfaceIRingBuffer<T>
- Returns:
- the oldest element from the buffer.
- Throws:
IRingBuffer.RingBufferException
- if the buffer is empty.- See Also:
IRingBuffer.getOldest()
-
getYoungest
public T getYoungest() throws IRingBuffer.RingBufferException
Description copied from interface:IRingBuffer
Returns the last element added. This method does not remove the element.- Specified by:
getYoungest
in interfaceIRingBuffer<T>
- Returns:
- the last element added.
- Throws:
IRingBuffer.RingBufferException
- if the buffer is empty.- See Also:
IRingBuffer.getYoungest()
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
- See Also:
Object.hashCode()
-
isEmpty
public boolean isEmpty()
Description copied from interface:IRingBuffer
Tests whether no elements are stored in the buffer.- Specified by:
isEmpty
in interfaceIRingBuffer<T>
- Returns:
- true if no element is stored in the buffer.
- See Also:
IRingBuffer.isEmpty()
-
isFull
public boolean isFull()
Description copied from interface:IRingBuffer
Returns true if no more space in the buffer is available. This method should be used to test before callingIRingBuffer.add(Object)
.- Specified by:
isFull
in interfaceIRingBuffer<T>
- Returns:
- true if no more space in the buffer is available.
- See Also:
IRingBuffer.isFull()
-
iterator
public java.util.Iterator<T> iterator()
Delegates to
.iteratorL2F()
- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- See Also:
Iterable.iterator()
-
iteratorF2L
public java.util.Iterator<T> iteratorF2L()
Returns anIterator
that will return the elements in exactly the inverse order the subsequent call toremove()
would do.The youngest elements are returned first. The
Iterator
returned is not thread- safe!- Specified by:
iteratorF2L
in interfaceIRingBuffer<T>
- Returns:
- an
Iterator
that will return the elements in exactly the inverse order the subsequent call toremove()
would do.
-
iteratorL2F
public java.util.Iterator<T> iteratorL2F()
Returns anIterator
that will return the elements in exactly the order the subsequent call toremove()
would do.The oldest elements are returned first. The
Iterator
returned is not thread- safe!- Specified by:
iteratorL2F
in interfaceIRingBuffer<T>
- Returns:
- an
Iterator
that will return the elements in exactly the order the subsequent call toremove()
would do.
-
remove
public T remove()
Description copied from interface:IRingBuffer
Removes the oldest element from the buffer.- Specified by:
remove
in interfaceIRingBuffer<T>
- Returns:
- the removed oldest element from the buffer.
- See Also:
IRingBuffer.remove()
-
removeAll
public T[] removeAll()
Description copied from interface:IRingBuffer
Clears the buffer. It will return all of it's stored elements.- Specified by:
removeAll
in interfaceIRingBuffer<T>
- 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 internalList pendingremove
.- Specified by:
setBufferSize
in interfaceIRingBuffer<T>
- 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 interfaceIRingBuffer<T>
- Returns:
- the actual amount of elements stored in the buffer.
- See Also:
IRingBuffer.size()
-
toString
public java.lang.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 malloc are triggered.
- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of the RingBuffer and it's contents.
-
-