Package gov.usgs.earthquake.util
Class RoundRobinQueue<T>
- java.lang.Object
-
- gov.usgs.earthquake.util.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 thegetQueueId(Object)
to control how objects are added to queues.
-
-
Constructor Summary
Constructors Constructor Description RoundRobinQueue()
Default constructor.RoundRobinQueue(RoundRobinQueue<T> that)
Deep copy of another RoundRobinQueue.
-
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
-
-
-
-
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 assumesgetQueueId(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.
-
offer
public boolean offer(T e)
Add an item to the queue, if possible.- Specified by:
offer
in interfacejava.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 interfacejava.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 thepeek()
method only in that it throws an exception if this queue is empty.- Specified by:
element
in interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.util.Collection<T>
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Collection<T>
-
containsAll
public boolean containsAll(java.util.Collection<?> c)
- Specified by:
containsAll
in interfacejava.util.Collection<T>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Collection<T>
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
- Specified by:
removeAll
in interfacejava.util.Collection<T>
-
size
public int size()
- Specified by:
size
in interfacejava.util.Collection<T>
-
contains
public boolean contains(java.lang.Object o)
- Specified by:
contains
in interfacejava.util.Collection<T>
-
remove
public boolean remove(java.lang.Object o)
- Specified by:
remove
in interfacejava.util.Collection<T>
-
toList
public java.util.List<T> toList()
Flatten queue to a list. Creates a copy (seeRoundRobinQueue(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()
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArray
in interfacejava.util.Collection<T>
-
toArray
public <T2> T2[] toArray(T2[] array)
- Specified by:
toArray
in interfacejava.util.Collection<T>
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
- Specified by:
retainAll
in interfacejava.util.Collection<T>
-
-