java.lang.Object
io.aiven.commons.collections.RingBuffer<K>
- Type Parameters:
K- the type of item in the queue. Must support equality check.
Implements a ring buffer of items. Items are inserted until maximum size is
reached and then the earliest items are removed when newer items are added.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumHow to handle the duplicates in the buffer. -
Constructor Summary
ConstructorsConstructorDescriptionRingBuffer(int size) Create a Ring Buffer of a maximum size that rejects duplicates.RingBuffer(int size, RingBuffer.DuplicateHandling duplicateHandling) Create a Ring Buffer of specified maximum size and potentially allowing duplicates. -
Method Summary
Modifier and TypeMethodDescriptionAdds a new item if it is not already present.booleanDetermines if the item is in the buffer.booleanGets the next item to be ejected.inthashCode()head()Returns but does not remove the head of the buffer.booleanisFull()Returnstrueif the buffer is full.voidRemoves a single instance of the item from the buffer.tail()Returns but does not remove the teal of the buffer.toString()
-
Constructor Details
-
RingBuffer
public RingBuffer(int size) Create a Ring Buffer of a maximum size that rejects duplicates. If the size is less than or equal to 0 then the buffer is always empty.- Parameters:
size- The maximum size of the ring buffer- See Also:
-
RingBuffer
Create a Ring Buffer of specified maximum size and potentially allowing duplicates. If the size is less than or equal to 0 then the buffer is always empty.- Parameters:
size- The maximum size of the ring bufferduplicateHandling- defines how to handle duplicate values in the buffer.
-
-
Method Details
-
toString
-
add
Adds a new item if it is not already present.- If the buffer is always empty the item is ignored and not enqueued.
- If the buffer already contains the item it is ignored and not enqueued.
- If the buffer is full the oldest entry in the buffer is ejected.
- Parameters:
item- Item T which is to be added to the Queue- Returns:
- The item that was ejected. May be
null.
-
remove
Removes a single instance of the item from the buffer.- Parameters:
item- the item to remove.
-
contains
Determines if the item is in the buffer.- Parameters:
item- the item to look for.- Returns:
trueif the item is in the buffer,falseothersie.
-
head
Returns but does not remove the head of the buffer.- Returns:
- the item at the head of the buffer. May be
null.
-
tail
Returns but does not remove the teal of the buffer.- Returns:
- the item at the tail of the buffer. May be
null.
-
isFull
public boolean isFull()Returnstrueif the buffer is full.- Returns:
trueif the buffer is full.
-
getNextEjected
Gets the next item to be ejected. If the buffer is full this will return the oldest value in the buffer. If the buffer is not full this method will returnnull.- Returns:
- A value T from the last place in the buffer, returns null if buffer is not full.
-
equals
-
hashCode
public int hashCode()
-