Class RoundRobinQueue<T>

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

public class RoundRobinQueue<T> extends Object implements 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.
  • Constructor Details

    • 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 Details

    • getQueueId

      protected 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 Collection<T>
      Specified by:
      add in interface 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 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 Queue<T>
      Returns:
      first element in queue.
      Throws:
      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 Queue<T>
      Returns:
      the head of this queue.
      Throws:
      NoSuchElementException - if this queue is empty.
    • poll

      public T poll()
      Retrieves and removes the head of this queue.
      Specified by:
      poll in interface 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 Queue<T>
      Returns:
      the head of this queue, or null if this queue is empty.
    • addAll

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

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

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

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

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

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

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<T>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<T>
    • toList

      public 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 Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
    • toArray

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

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

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