public class RingBuffer<E> extends AbstractCollection<E> implements Queue<E>
This class implements a generic ring buffer class which we can use to keep track of a finite amount of data.
For the ingest service, this is mostly used for window
Constructor and Description |
---|
RingBuffer(int capacity)
Constructs a ring buffer with a specified size
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E obj)
add - appends an element to the end of this queue
|
void |
clear()
clear - effectively empties the array by resetting
all of our pointers and clearing our buffer
|
boolean |
contains(Object obj)
contains - checks whether or not an object is in this queue
|
E |
element()
element Returns the element current under the read pointer
Does NOT update the read position
|
int |
getCapacity()
getCapacity - gives back the capacity of this buffer
|
boolean |
isEmpty()
isEmpty - are there any items we can get from this queue?
|
Iterator<E> |
iterator()
iterator - returns an iterator that allows you to view all
elements in this queue
This iterator does NOT allow for remove()
|
boolean |
offer(E obj)
offer - attempts to add an object to the queue,
if the queue is at capacity, return false
|
E |
peek()
peek - Returns the element currently under the read pointer
Does NOT update the read position
If there is no valid element, return null
|
E |
poll()
poll - returns the head of this queue and removes it from the queue.
|
E |
remove()
remove - returns the current head of this queue and removes it
This *does* update the read position
|
int |
size()
size - returns the number of occupied slots in this queue
|
Object[] |
toArray()
toArray - builds an array containing all objects in this queue
|
addAll, containsAll, remove, removeAll, retainAll, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addAll, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray
public RingBuffer(int capacity)
capacity
- the maximum capacity of this buffIllegalArgumentException
- if capacity is less than 1public boolean offer(E obj)
public boolean add(E obj)
add
in interface Collection<E>
add
in interface Queue<E>
add
in class AbstractCollection<E>
obj
- - the object we're trying to add to the queueIllegalStateException
- if we are at capacitypublic E peek()
public E element()
element
in interface Queue<E>
IllegalStateException
- if we have no valid readable objectspublic E remove()
remove
in interface Queue<E>
NoSuchElementException
- if we have no items to be consumedpublic E poll()
public boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in class AbstractCollection<E>
public int getCapacity()
public int size()
size
in interface Collection<E>
size
in class AbstractCollection<E>
public void clear()
clear
in interface Collection<E>
clear
in class AbstractCollection<E>
public Object[] toArray()
toArray
in interface Collection<E>
toArray
in class AbstractCollection<E>
ArrayStoreException
- - if the elementpublic boolean contains(Object obj)
contains
in interface Collection<E>
contains
in class AbstractCollection<E>
obj
- the needle we're searching for in the queuepublic Iterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in class AbstractCollection<E>
Copyright © 2020. All rights reserved.