Class OverwritingRingBuffer<T>
java.lang.Object
com.abstractkamen.datastructures.impl.queues.OverwritingRingBuffer<T>
- Type Parameters:
T- the type of elements stored in the buffer.
- All Implemented Interfaces:
RingBuffer<T>,Iterable<T>
Represents a ring buffer that overwrites the oldest element when full.
-
Constructor Summary
ConstructorsConstructorDescriptionOverwritingRingBuffer(int initialCapacity) Constructs a newOverwritingRingBufferwith the specified initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionintcapacity()Returns the total capacity of the buffer.dequeue()Dequeues and returns the oldest item from the buffer.booleanEnqueues the specified item into the buffer, overwriting the oldest item if the buffer is full.booleanisEmpty()True if buffer is empty.iterator()Returns an iterator over the items in the buffer.intsize()Returns the number of elements currently stored in the buffer.Returns a sequentialSpliteratorof items in the buffer.stream()Returns a sequentialStreamof items in the buffer.toString()
-
Constructor Details
-
OverwritingRingBuffer
public OverwritingRingBuffer(int initialCapacity) Constructs a newOverwritingRingBufferwith the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the ring buffer (must be greater than zero).- Throws:
IllegalArgumentException- if the initial capacity is lesser than one.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Description copied from interface:RingBufferTrue if buffer is empty.- Specified by:
isEmptyin interfaceRingBuffer<T>- Returns:
- true if buffer is empty
-
size
public int size()Description copied from interface:RingBufferReturns the number of elements currently stored in the buffer.- Specified by:
sizein interfaceRingBuffer<T>- Returns:
- the number of elements in the buffer.
-
capacity
public int capacity()Description copied from interface:RingBufferReturns the total capacity of the buffer.- Specified by:
capacityin interfaceRingBuffer<T>- Returns:
- the capacity of the buffer.
-
enqueue
Enqueues the specified item into the buffer, overwriting the oldest item if the buffer is full.- Specified by:
enqueuein interfaceRingBuffer<T>- Parameters:
item- the item to be enqueued.- Returns:
truealways.
-
dequeue
Dequeues and returns the oldest item from the buffer.- Specified by:
dequeuein interfaceRingBuffer<T>- Returns:
- the oldest item in the buffer.
- Throws:
NoSuchElementException- if the buffer is empty.
-
stream
Returns a sequentialStreamof items in the buffer.- Specified by:
streamin interfaceRingBuffer<T>- Returns:
- a sequential stream of items in the buffer.
-
spliterator
Returns a sequentialSpliteratorof items in the buffer.- Specified by:
spliteratorin interfaceIterable<T>- Returns:
- a sequential spliterator of items in the buffer.
-
iterator
Returns an iterator over the items in the buffer. -
toString
-