Class RingBufferArray<T>
- Type Parameters:
T- the type to store.
- All Implemented Interfaces:
IRingBuffer<T>,Serializable,Cloneable,Iterable<T>
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 retrievable Iterator- instances are not
synchronized at all.
- Version:
- $Revision: 1.10 $
- Author:
- Achim Westermann
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface info.monitorenter.util.collections.IRingBuffer
IRingBuffer.RingBufferException -
Field Summary
FieldsModifier and TypeFieldDescriptionElements that stores elements that have to be removed due to an invocation tosetBufferSize(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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleaninthashCode()booleanisEmpty()Tests whether no elements are stored in the buffer.Returns anIteratorthat will return the elements in exactly the inverse order the subsequent call toremove()would do.Returns anIteratorthat will return the elements in exactly the order the subsequent call toremove()would do.remove()Removes the oldest element from the buffer.T[]Clears the buffer.voidsetBufferSize(int newSize) Sets a new buffer- size.intsize()Returns the actual amount of elements stored in the buffer.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, iteratorMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
m_pendingremove
Elements that stores elements that have to be removed due to an invocation tosetBufferSize(int)with a smaller argument than the amount of elements stored.
-
-
Constructor Details
-
RingBufferArray
public RingBufferArray(int aSize) Constructs a RingBuffer with the given size.- Parameters:
aSize- the size of the buffer.
-
-
Method Details
-
equals
- Overrides:
equalsin classRingBufferArrayFast<T>- See Also:
-
hashCode
public int hashCode()- Overrides:
hashCodein classRingBufferArrayFast<T>- See Also:
-
isEmpty
public boolean isEmpty()Description copied from interface:IRingBufferTests whether no elements are stored in the buffer.- Specified by:
isEmptyin interfaceIRingBuffer<T>- Overrides:
isEmptyin classRingBufferArrayFast<T>- Returns:
- true if no element is stored in the buffer.
- See Also:
-
iteratorF2L
Description copied from class:RingBufferArrayFastReturns anIteratorthat will return the elements in exactly the inverse order the subsequent call toremove()would do.The youngest elements are returned first. The
Iteratorreturned is not thread- safe!- Specified by:
iteratorF2Lin interfaceIRingBuffer<T>- Overrides:
iteratorF2Lin classRingBufferArrayFast<T>- Returns:
- an
Iteratorthat will return the elements in exactly the inverse order the subsequent call toremove()would do. - See Also:
-
iteratorL2F
Description copied from class:RingBufferArrayFastReturns anIteratorthat will return the elements in exactly the order the subsequent call toremove()would do.The oldest elements are returned first. The
Iteratorreturned is not thread- safe!- Specified by:
iteratorL2Fin interfaceIRingBuffer<T>- Overrides:
iteratorL2Fin classRingBufferArrayFast<T>- Returns:
- an
Iteratorthat will return the elements in exactly the order the subsequent call toremove()would do. - See Also:
-
remove
Description copied from interface:IRingBufferRemoves the oldest element from the buffer.- Specified by:
removein interfaceIRingBuffer<T>- Overrides:
removein classRingBufferArrayFast<T>- Returns:
- the removed oldest element from the buffer.
- See Also:
-
removeAll
Description copied from interface:IRingBufferClears the buffer. It will return all of it's stored elements.- Specified by:
removeAllin interfaceIRingBuffer<T>- Overrides:
removeAllin classRingBufferArrayFast<T>- Returns:
- all removed elements.
- See Also:
-
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:
setBufferSizein interfaceIRingBuffer<T>- Overrides:
setBufferSizein classRingBufferArrayFast<T>- Parameters:
newSize- the new size of the buffer.
-
size
public int size()Description copied from interface:IRingBufferReturns the actual amount of elements stored in the buffer.- Specified by:
sizein interfaceIRingBuffer<T>- Overrides:
sizein classRingBufferArrayFast<T>- Returns:
- the actual amount of elements stored in the buffer.
- See Also:
-
toString
Returns a string representation of the RingBuffer and it's contents.Don't call this in your application too often: hard array copy - operation and memory allocations are triggered.
- Overrides:
toStringin classRingBufferArrayFast<T>- Returns:
- a string representation of the RingBuffer and it's contents.
-