edu.wpi.prc.task.queue
Class MultiPriorityQueue<T extends Prioritizable>

java.lang.Object
  extended by edu.wpi.prc.task.queue.MultiPriorityQueue<T>
Type Parameters:
T - The type of Prioritizable to store in this MultiPriorityQueue.
All Implemented Interfaces:
TransitionerQueue<T>, java.io.Serializable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>

public class MultiPriorityQueue<T extends Prioritizable>
extends java.lang.Object
implements java.io.Serializable, TransitionerQueue<T>

Stores queues of Prioritizable objects. Although it appears as a single queue when iterating over it, the MultiPriorityQueue actually stores many different queues internally. Each internal queue is associated with a different priority. When iterating over the queue it is guaranteed that higher-priority objects will be returned before lower-priority ones. If two objects in the queue have the same priority, normal FIFO queue semantics are obeyed. When an iterator is requested, a copy of the MultiPriorityQueue is made, and the iterator uses the copy. Therefore, if the MultiPriorityQueue is modified after an iterator is returned, a new iterator will need to be requested for those changes to be returned by the iterator.

Author:
James Baldassari
See Also:
Serialized Form

Nested Class Summary
private  class MultiPriorityQueue.MultiPriorityQueueIterator<P extends T>
          This is the iterator for the MultiPriorityQueue.
 
Field Summary
private  java.util.SortedSet<java.lang.Integer> priorities
           
private  java.util.Map<java.lang.Integer,java.util.LinkedList<T>> queues
           
private static long serialVersionUID
           
(package private)  int size
           
 
Constructor Summary
MultiPriorityQueue()
           
 
Method Summary
 boolean add(T t)
          Adds the given object to the MultiPriorityQueue.
 boolean addAll(java.util.Collection<? extends T> t)
          Adds all objects in the given collection to the MultiPriorityQueue.
 boolean addAll(T[] tArray)
          Adds all objects in the given array to the MultiPriorityQueue.
 void clear()
          Removes all objects from the MultiPriorityQueue.
 boolean contains(java.lang.Object obj)
          Searches through the MultiPriorityQueue for the given object.
 boolean containsAll(java.util.Collection<?> c)
          Checks whether all of the objects in the given collection appear in the MultiPriorityQueue.
 T first()
           
 boolean isEmpty()
           
 java.util.Iterator<T> iterator()
          Returns an iterator for this MultiPriorityQueue.
 T peek()
           
 boolean remove(java.lang.Object obj)
          Removes the given object from the MultiPriorityQueue.
 boolean removeAll(java.util.Collection<?> c)
          Removes all objects contained in the given collection from the MultiPriorityQueue.
 T removeFirst()
          Removes the first (highest-priority) object in the MultiPriorityQueue and returns it.
 boolean retainAll(java.util.Collection<?> c)
          Removes all objects from the MultiPriorityQueue except the objects contained in the given collection.
 int size()
           
 java.lang.Object[] toArray()
           
<P> P[]
toArray(P[] arg0)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Set
equals, hashCode
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

priorities

private java.util.SortedSet<java.lang.Integer> priorities

queues

private java.util.Map<java.lang.Integer,java.util.LinkedList<T extends Prioritizable>> queues

size

int size
Constructor Detail

MultiPriorityQueue

public MultiPriorityQueue()
Method Detail

iterator

public java.util.Iterator<T> iterator()
Returns an iterator for this MultiPriorityQueue. When iterating over the queue it is guaranteed that higher-priority objects will be returned before lower-priority ones. If two objects in the queue have the same priority, normal FIFO queue semantics are obeyed. When an iterator is requested, a copy of the MultiPriorityQueue is made, and the iterator uses the copy. Therefore, if the MultiPriorityQueue is modified after an iterator is returned, a new iterator will need to be requested for those changes to be returned by the iterator.

Specified by:
iterator in interface java.lang.Iterable<T extends Prioritizable>
Specified by:
iterator in interface java.util.Collection<T extends Prioritizable>
Specified by:
iterator in interface java.util.Set<T extends Prioritizable>

size

public int size()
Specified by:
size in interface java.util.Collection<T extends Prioritizable>
Specified by:
size in interface java.util.Set<T extends Prioritizable>
Returns:
Returns the number of Prioritizable objects in the MultiPriorityQueue.

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Collection<T extends Prioritizable>
Specified by:
isEmpty in interface java.util.Set<T extends Prioritizable>
Returns:
Returns true if there are no objects in the MultiPriorityQueue, false if there is at least one.

contains

public boolean contains(java.lang.Object obj)
Searches through the MultiPriorityQueue for the given object.

Specified by:
contains in interface java.util.Collection<T extends Prioritizable>
Specified by:
contains in interface java.util.Set<T extends Prioritizable>
Parameters:
obj - The object to search for.
Returns:
Returns true if the object was found in the MultiPriorityQueue, false otherwise.

containsAll

public boolean containsAll(java.util.Collection<?> c)
Checks whether all of the objects in the given collection appear in the MultiPriorityQueue.

Specified by:
containsAll in interface java.util.Collection<T extends Prioritizable>
Specified by:
containsAll in interface java.util.Set<T extends Prioritizable>
Parameters:
c - The collection of objects to check for.
Returns:
Returns true if all of the objects were found in the queue, false otherwise.

add

public boolean add(T t)
Adds the given object to the MultiPriorityQueue.

Specified by:
add in interface java.util.Collection<T extends Prioritizable>
Specified by:
add in interface java.util.Set<T extends Prioritizable>
Parameters:
t - The object to add.
Returns:
Returns true if the MultiPriorityQueue was modified as a result of adding the object, false otherwise.

addAll

public boolean addAll(java.util.Collection<? extends T> t)
Adds all objects in the given collection to the MultiPriorityQueue.

Specified by:
addAll in interface java.util.Collection<T extends Prioritizable>
Specified by:
addAll in interface java.util.Set<T extends Prioritizable>
Parameters:
t - The collection containing objects to add.
Returns:
Returns true if the MultiPriorityQueue was modified as a result of adding the objects, false otherwise.

addAll

public boolean addAll(T[] tArray)
Adds all objects in the given array to the MultiPriorityQueue.

Specified by:
addAll in interface TransitionerQueue<T extends Prioritizable>
Parameters:
tArray - The array containing objects to add.
Returns:
Returns true if the MultiPriorityQueue was modified as a result of adding the objects, false otherwise.

remove

public boolean remove(java.lang.Object obj)
Removes the given object from the MultiPriorityQueue.

Specified by:
remove in interface java.util.Collection<T extends Prioritizable>
Specified by:
remove in interface java.util.Set<T extends Prioritizable>
Parameters:
obj - The object to remove.
Returns:
Returns true if the MultiPriorityQueue was modified as a result of removing the object, false otherwise.

removeFirst

public T removeFirst()
Removes the first (highest-priority) object in the MultiPriorityQueue and returns it.

Specified by:
removeFirst in interface TransitionerQueue<T extends Prioritizable>
Returns:
Returns the object that was removed.

removeAll

public boolean removeAll(java.util.Collection<?> c)
Removes all objects contained in the given collection from the MultiPriorityQueue.

Specified by:
removeAll in interface java.util.Collection<T extends Prioritizable>
Specified by:
removeAll in interface java.util.Set<T extends Prioritizable>
Parameters:
c - The collection of objects to remove.
Returns:
Returns true if the MultiPriorityQueue was modified as a result of removing the given objects, false otherwise.

retainAll

public boolean retainAll(java.util.Collection<?> c)
Removes all objects from the MultiPriorityQueue except the objects contained in the given collection.

Specified by:
retainAll in interface java.util.Collection<T extends Prioritizable>
Specified by:
retainAll in interface java.util.Set<T extends Prioritizable>
Parameters:
c - The collection of objects to retain.
Returns:
Returns true if the MultiPriorityQueue was modified as a result of retaining the given objects, false otherwise.

peek

public T peek()
Returns:
Returns the first (highest-priority) object in the MultiPriorityQueue, but does not remove it from the queue.

first

public T first()
Specified by:
first in interface TransitionerQueue<T extends Prioritizable>
Returns:
Returns the first (highest-priority) object in the MultiPriorityQueue, but does not remove it from the queue.

clear

public void clear()
Removes all objects from the MultiPriorityQueue.

Specified by:
clear in interface java.util.Collection<T extends Prioritizable>
Specified by:
clear in interface java.util.Set<T extends Prioritizable>

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection<T extends Prioritizable>
Specified by:
toArray in interface java.util.Set<T extends Prioritizable>
Returns:
Returns an array containing all objects from the MultiPriorityQueue, sorted according to the MultiPriorityQueue's iterator semantics.

toArray

public <P> P[] toArray(P[] arg0)
Specified by:
toArray in interface java.util.Collection<T extends Prioritizable>
Specified by:
toArray in interface java.util.Set<T extends Prioritizable>
Returns:
Returns an array containing all objects from the MultiPriorityQueue, sorted according to the MultiPriorityQueue's iterator semantics. The runtime type of the returned array is that of the specified array.