Class 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 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
        Specified by:
        offer in interface Queue<E>
        Parameters:
        obj - what we're trying to append to the queue
        Returns:
        whether or not we added the object to the queue
      • add

        public boolean add​(E obj)
        add - appends an element to the end of this queue
        Specified by:
        add in interface Collection<E>
        Specified by:
        add in interface Queue<E>
        Overrides:
        add in class AbstractCollection<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
        Specified by:
        peek in interface Queue<E>
      • element

        public E element()
        element Returns the element current under the read pointer Does NOT update the read position
        Specified by:
        element in interface Queue<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 interface Queue<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
        Specified by:
        poll in interface Queue<E>
        Returns:
        the former head of this queue
      • isEmpty

        public boolean isEmpty()
        isEmpty - are there any items we can get from this queue?
        Specified by:
        isEmpty in interface Collection<E>
        Overrides:
        isEmpty in class AbstractCollection<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 interface Collection<E>
        Specified by:
        size in class AbstractCollection<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 interface Collection<E>
        Overrides:
        clear in class AbstractCollection<E>
      • contains

        public boolean contains​(Object obj)
        contains - checks whether or not an object is in this queue
        Specified by:
        contains in interface Collection<E>
        Overrides:
        contains in class AbstractCollection<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 interface Collection<E>
        Specified by:
        iterator in interface Iterable<E>
        Specified by:
        iterator in class AbstractCollection<E>
        Returns:
        an instance of ring iterator