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 booleanadd(E obj)add - appends an element to the end of this queuevoidclear()clear - effectively empties the array by resetting all of our pointers and clearing our bufferbooleancontains(Object obj)contains - checks whether or not an object is in this queueEelement()element Returns the element current under the read pointer Does NOT update the read positionintgetCapacity()getCapacity - gives back the capacity of this bufferbooleanisEmpty()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()booleanoffer(E obj)offer - attempts to add an object to the queue, if the queue is at capacity, return falseEpeek()peek - Returns the element currently under the read pointer Does NOT update the read position If there is no valid element, return nullEpoll()poll - returns the head of this queue and removes it from the queue.Eremove()remove - returns the current head of this queue and removes it This *does* update the read positionintsize()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:
addin interfaceCollection<E>- Specified by:
addin interfaceQueue<E>- Overrides:
addin 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:
elementin 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:
removein 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:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin 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:
sizein interfaceCollection<E>- Specified by:
sizein 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:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-
toArray
public Object[] toArray()
toArray - builds an array containing all objects in this queue- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin 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:
containsin interfaceCollection<E>- Overrides:
containsin 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:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>- Returns:
- an instance of ring iterator
-
-