Package net.snowflake.ingest.utils
Class RingBuffer<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- net.snowflake.ingest.utils.RingBuffer<E>
-
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Queue<E>
public class RingBuffer<E> extends AbstractCollection<E> implements Queue<E>
- Author:
- obabarinsa
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 Summary
Constructors Constructor Description RingBuffer(int capacity)
Constructs a ring buffer with a specified size
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E obj)
add - appends an element to the end of this queuevoid
clear()
clear - effectively empties the array by resetting all of our pointers and clearing our bufferboolean
contains(Object obj)
contains - checks whether or not an object is in this queueE
element()
element Returns the element current under the read pointer Does NOT update the read positionint
getCapacity()
getCapacity - gives back the capacity of this bufferboolean
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 falseE
peek()
peek - Returns the element currently under the read pointer Does NOT update the read position If there is no valid element, return nullE
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 positionint
size()
size - returns the number of occupied slots in this queueObject[]
toArray()
toArray - builds an array containing all objects in this queue-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, remove, removeAll, retainAll, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
-
-
-
-
Constructor Detail
-
RingBuffer
public RingBuffer(int capacity)
Constructs a ring buffer with a specified size- Parameters:
capacity
- the maximum capacity of this buff- Throws:
IllegalArgumentException
- if capacity is less than 1
-
-
Method Detail
-
offer
public boolean offer(E obj)
offer - attempts to add an object to the queue, if the queue is at capacity, return false
-
add
public boolean add(E obj)
add - appends an element to the end of this queue- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceQueue<E>
- Overrides:
add
in classAbstractCollection<E>
- Parameters:
obj
- - the object we're trying to add to the queue- Returns:
- always returns true if it doesn't throw
- Throws:
IllegalStateException
- if we are at capacity
-
peek
public 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
-
element
public E element()
element Returns the element current under the read pointer Does NOT update the read position- Specified by:
element
in interfaceQueue<E>
- Throws:
IllegalStateException
- if we have no valid readable objects
-
remove
public E remove()
remove - returns the current head of this queue and removes it This *does* update the read position- Specified by:
remove
in interfaceQueue<E>
- Returns:
- the object which was formerly the head of this queue
- Throws:
NoSuchElementException
- if we have no items to be consumed
-
poll
public E poll()
poll - returns the head of this queue and removes it from the queue. If no element exists returns null
-
isEmpty
public boolean isEmpty()
isEmpty - are there any items we can get from this queue?- Specified by:
isEmpty
in interfaceCollection<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
- Returns:
- whether or not there are any consumable items in this buffer
-
getCapacity
public int getCapacity()
getCapacity - gives back the capacity of this buffer- Returns:
- the capacity of this buffer
-
size
public int size()
size - returns the number of occupied slots in this queue- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the number of slots currently in use in this buffer
-
clear
public void clear()
clear - effectively empties the array by resetting all of our pointers and clearing our buffer- Specified by:
clear
in interfaceCollection<E>
- Overrides:
clear
in classAbstractCollection<E>
-
toArray
public Object[] toArray()
toArray - builds an array containing all objects in this queue- Specified by:
toArray
in interfaceCollection<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Returns:
- an array containing all elements of this queue
- Throws:
ArrayStoreException
- - if the element
-
contains
public boolean contains(Object obj)
contains - checks whether or not an object is in this queue- Specified by:
contains
in interfaceCollection<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
obj
- the needle we're searching for in the queue- Returns:
- did we find an object equal to this one in the queue
-
iterator
public Iterator<E> iterator()
iterator - returns an iterator that allows you to view all elements in this queue This iterator does NOT allow for remove()- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in classAbstractCollection<E>
- Returns:
- an instance of ring iterator
-
-