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
ConstructorDescriptionOverwritingRingBuffer
(int initialCapacity) Constructs a newOverwritingRingBuffer
with the specified initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionint
capacity()
Returns the total capacity of the buffer.dequeue()
Dequeues and returns the oldest item from the buffer.boolean
Enqueues the specified item into the buffer, overwriting the oldest item if the buffer is full.boolean
isEmpty()
True if buffer is empty.iterator()
Returns an iterator over the items in the buffer.int
size()
Returns the number of elements currently stored in the buffer.Returns a sequentialSpliterator
of items in the buffer.stream()
Returns a sequentialStream
of items in the buffer.toString()
-
Constructor Details
-
OverwritingRingBuffer
public OverwritingRingBuffer(int initialCapacity) Constructs a newOverwritingRingBuffer
with 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:RingBuffer
True if buffer is empty.- Specified by:
isEmpty
in interfaceRingBuffer<T>
- Returns:
- true if buffer is empty
-
size
public int size()Description copied from interface:RingBuffer
Returns the number of elements currently stored in the buffer.- Specified by:
size
in interfaceRingBuffer<T>
- Returns:
- the number of elements in the buffer.
-
capacity
public int capacity()Description copied from interface:RingBuffer
Returns the total capacity of the buffer.- Specified by:
capacity
in 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:
enqueue
in interfaceRingBuffer<T>
- Parameters:
item
- the item to be enqueued.- Returns:
true
always.
-
dequeue
Dequeues and returns the oldest item from the buffer.- Specified by:
dequeue
in interfaceRingBuffer<T>
- Returns:
- the oldest item in the buffer.
- Throws:
NoSuchElementException
- if the buffer is empty.
-
stream
Returns a sequentialStream
of items in the buffer.- Specified by:
stream
in interfaceRingBuffer<T>
- Returns:
- a sequential stream of items in the buffer.
-
spliterator
Returns a sequentialSpliterator
of items in the buffer.- Specified by:
spliterator
in interfaceIterable<T>
- Returns:
- a sequential spliterator of items in the buffer.
-
iterator
Returns an iterator over the items in the buffer. -
toString
-