Class RoundRobinQueue<T>

  • Type Parameters:
    T - type of object being queued.
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, java.util.Queue<T>
    Direct Known Subclasses:
    RoundRobinBlockingQueue

    public class RoundRobinQueue<T>
    extends java.lang.Object
    implements java.util.Queue<T>
    An abstract base class for round-robin queueing. Sub classes should implement the getQueueId(Object) to control how objects are added to queues.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(T e)
      Add an item to the queue.
      boolean addAll​(java.util.Collection<? extends T> c)
      ======================= COLLECTION METHODS =========================
      void clear()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      T element()
      Retrieves, but does not remove, the head of this queue.
      protected java.lang.String getQueueId​(T object)
      This method determines which queue an object uses.
      boolean isEmpty()  
      java.util.Iterator<T> iterator()  
      boolean offer​(T e)
      Add an item to the queue, if possible.
      T peek()
      Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
      T poll()
      Retrieves and removes the head of this queue.
      T remove()
      Retrieves and removes the head of this queue.
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      boolean retainAll​(java.util.Collection<?> c)  
      int size()  
      java.lang.Object[] toArray()  
      <T2> T2[] toArray​(T2[] array)  
      java.util.List<T> toList()
      Flatten queue to a list.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Constructor Detail

      • RoundRobinQueue

        public RoundRobinQueue()
        Default constructor.
      • RoundRobinQueue

        public RoundRobinQueue​(RoundRobinQueue<T> that)
        Deep copy of another RoundRobinQueue. This method is used for semi-destructive iteration methods. NOTE: this assumes getQueueId(Object) behaves the same for this and that.
        Parameters:
        that - a RoundRobinQueue to make a deep copy of
    • Method Detail

      • getQueueId

        protected java.lang.String getQueueId​(T object)
        This method determines which queue an object uses.
        Parameters:
        object - the object being added.
        Returns:
        id of the queue where object should be added.
      • add

        public boolean add​(T e)
        Add an item to the queue.
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.Queue<T>
        Parameters:
        e - item to add
        Returns:
        true if added.
      • offer

        public boolean offer​(T e)
        Add an item to the queue, if possible.
        Specified by:
        offer in interface java.util.Queue<T>
        Parameters:
        e - item to add
        Returns:
        true if added, false otherwise.
      • remove

        public T remove()
        Retrieves and removes the head of this queue.
        Specified by:
        remove in interface java.util.Queue<T>
        Returns:
        first element in queue.
        Throws:
        java.util.NoSuchElementException - if queue is empty.
      • element

        public T element()
        Retrieves, but does not remove, the head of this queue. This method differs from the peek() method only in that it throws an exception if this queue is empty.
        Specified by:
        element in interface java.util.Queue<T>
        Returns:
        the head of this queue.
        Throws:
        java.util.NoSuchElementException - if this queue is empty.
      • poll

        public T poll()
        Retrieves and removes the head of this queue.
        Specified by:
        poll in interface java.util.Queue<T>
        Returns:
        the head of this queue, or null if this queue is empty.
      • peek

        public T peek()
        Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
        Specified by:
        peek in interface java.util.Queue<T>
        Returns:
        the head of this queue, or null if this queue is empty.
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> c)
        ======================= COLLECTION METHODS =========================
        Specified by:
        addAll in interface java.util.Collection<T>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<T>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
      • toList

        public java.util.List<T> toList()
        Flatten queue to a list. Creates a copy (see RoundRobinQueue(RoundRobinQueue), then builds list by polling until it is empty.
        Returns:
        list of all items currently in queue.
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
      • toArray

        public <T2> T2[] toArray​(T2[] array)
        Specified by:
        toArray in interface java.util.Collection<T>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<T>