Sharkysoft home

lava.util
Class PriorityQueue

java.lang.Object
  |
  +--lava.util.PriorityQueue
All Implemented Interfaces:
java.util.Enumeration

public class PriorityQueue
extends java.lang.Object
implements java.util.Enumeration

Vector of prioritized objects.

A PriorityQueue is vector of prioritized Objects.

Since:
1999.04.05
Author:
Sharky

Constructor Summary
PriorityQueue()
          Initializes an empty PriorityQueue, using a default initial capacity.
PriorityQueue(int cap)
          Prepares an empty PriorityQueue with the specified initial capacity.
 
Method Summary
 void addElement(java.lang.Object object)
          Adds an element to the end of this queue, using the priority of the last Object in the queue.
 int capacity()
          Returns the current capacity of this queue.
 java.lang.Object clone()
          Clones this queue.
 boolean contains(java.lang.Object obj)
          Tells if this queue contains the specified object.
 void copyInto(java.lang.Object[] dest)
          Copies elements into Object array.
 java.lang.Object elementAt(int index)
          Returns the element at the specified index in the queue.
 void ensureCapacity(int capacity)
          Guarantees that this queue will be able to buffer at least the indicated number of elements.
 java.lang.Object firstElement()
          Returns the next element in the queue without removing it.
 boolean hasMoreElements()
          Determines whether this queue contains more elements.
 int indexOf(java.lang.Object obj)
          See indexOf(Object,int).
 int indexOf(java.lang.Object obj, int index)
          Searches this queue for an object equal to the given object.
 void insertElementAt(java.lang.Object obj, int index)
          Inserts an object into the queue.
 void insertPrioritizedElement(java.lang.Object obj, int priority)
          Inserts an object into the queue at the end of other objects with the same or higher priority.
 boolean isEmpty()
          Determines if this queue is empty.
 java.lang.Object lastElement()
          Returns the last element in this queue without removing it.
 int lastIndexOf(java.lang.Object obj)
          See lastIndexOf(Object,int).
 int lastIndexOf(java.lang.Object obj, int index)
          Searches this queue for an object equal to the given object.
 java.lang.Object nextElement()
          Removes the element at the head of the queue and returns it.
 int priorityAt(int index)
          Copies elements into Object array.
 void removeAllElements()
          Empties the queue.
 boolean removeElement(java.lang.Object obj)
          Removes the specified object from the queue.
 void removeElementAt(int index)
          Deletes the element at the specified index.
 void setElementAt(java.lang.Object obj, int index)
          Replaces the element at the specified index with the given object.
 void setSize(int new_size)
          Sets the number of elements contained in this queue.
 int size()
          Returns the number of elements currently buffered in this queue.
 java.lang.String toString()
          Generates a string representation of this queue.
 void trimToSize()
          Minimizes the amount of memory used to store this queue.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PriorityQueue

public PriorityQueue(int cap)
Prepares an empty PriorityQueue with the specified initial capacity.
Parameters:
dc - the initial capacity

PriorityQueue

public PriorityQueue()
Initializes an empty PriorityQueue, using a default initial capacity.
Method Detail

isEmpty

public final boolean isEmpty()
Determines if this queue is empty.
Returns:
true if this queue is empty; false otherwise

hasMoreElements

public final boolean hasMoreElements()
Determines whether this queue contains more elements.
Specified by:
hasMoreElements in interface java.util.Enumeration
Returns:
true if there are more elements, false otherwise

nextElement

public java.lang.Object nextElement()
Removes the element at the head of the queue and returns it.
Specified by:
nextElement in interface java.util.Enumeration
Returns:
the element at the head of the queue, or null if the queue is empty

addElement

public void addElement(java.lang.Object object)
Adds an element to the end of this queue, using the priority of the last Object in the queue. If the queue is empty, then the Object will be assigned the lowest priority.
Parameters:
object - the element to add

capacity

public final int capacity()
Returns the current capacity of this queue.
Returns:
the current capacity of this queue

size

public final int size()
Returns the number of elements currently buffered in this queue.
Returns:
the current capacity of this queue

indexOf

public int indexOf(java.lang.Object obj,
                   int index)
Searches this queue for an object equal to the given object. If obj is null, the search is for an element whose value is also null; otherwise the object's equals method is used to determine equality. Searching begins with the object specified by index, and precedes forwards.
Parameters:
obj - the object to search for
index - the index to begin searching from (Default value: 0)
Returns:
the index of the matching object in the queue, or -1 if no match is found

indexOf

public final int indexOf(java.lang.Object obj)
See indexOf(Object,int).

lastIndexOf

public int lastIndexOf(java.lang.Object obj,
                       int index)
Searches this queue for an object equal to the given object. If obj is null, the search is for an element whose value is also null; otherwise the object's equals method is used to determine equality. Searching begins with the object specified by index, and precedes backwards.
Parameters:
obj - the object to search for
index - the index to begin searching from (Default value: tail, where tail is the index of the last element)
Returns:
the index of the matching object in the queue, or -1 if no match is found

lastIndexOf

public final int lastIndexOf(java.lang.Object obj)
See lastIndexOf(Object,int).

elementAt

public java.lang.Object elementAt(int index)
Returns the element at the specified index in the queue.
Parameters:
index - the index of the element
Returns:
the element at the specified index

firstElement

public final java.lang.Object firstElement()
Returns the next element in the queue without removing it.
Returns:
the next element in the queue
Throws:
NoSuchElementException - if the queue is empty

clone

public java.lang.Object clone()
Clones this queue. The cloned queue is completely independent of the original queue, except that the pointers in the new queue still point to the same Objects.
Overrides:
clone in class java.lang.Object
Returns:
the clone

contains

public final boolean contains(java.lang.Object obj)
Tells if this queue contains the specified object. Uses the equals method to determine a match.
Parameters:
obj - the object to search for
Returns:
true if the object is in this queue, false otherwise

insertElementAt

public void insertElementAt(java.lang.Object obj,
                            int index)
Inserts an object into the queue. It will have the same priority as the former object of the same index, or the lowest priority if the queue was empty.
Parameters:
obj - the object to insert
priority - the priority

insertPrioritizedElement

public void insertPrioritizedElement(java.lang.Object obj,
                                     int priority)
Inserts an object into the queue at the end of other objects with the same or higher priority.
Parameters:
obj - the object to insert
priority - the priority

lastElement

public final java.lang.Object lastElement()
Returns the last element in this queue without removing it.
Returns:
the last element in this queue
Throws:
NoSuchElementException - if this queue is empty

removeAllElements

public final void removeAllElements()
Empties the queue.

removeElementAt

public final void removeElementAt(int index)
Deletes the element at the specified index.
Parameters:
index - the index

removeElement

public final boolean removeElement(java.lang.Object obj)
Removes the specified object from the queue. This is done by searching for the object, using the indexOf(Object) method, and then deleting the match if one is found. If no match is found, the queue is unchanged.

Overrideable methods employed:

Parameters:
obj - the object
Returns:
true if the object was found and removed, false otherwise

setElementAt

public final void setElementAt(java.lang.Object obj,
                               int index)
Replaces the element at the specified index with the given object. The new object is assigned the same priority as the old object.
Parameters:
obj - the new object
index - the index

setSize

public final void setSize(int new_size)
Sets the number of elements contained in this queue. If the new size is smaller than the current number of elements in the queue, then the elements at the end of the queue will be discarded. If the new size is larger, then the queue will be padded at the end with null elements of the lowest possible priority.
Parameters:
new_size - the new size

toString

public final java.lang.String toString()
Generates a string representation of this queue. The form is "{(s1, p1), (s2, p2), ...}", where s1, s2, ... are the results of the toString method returned by each queued object, or "null" for elements that are null, and p1, p2, ... are the objects' priorities.
Overrides:
toString in class java.lang.Object
Returns:
the string representation

trimToSize

public final void trimToSize()
Minimizes the amount of memory used to store this queue.

ensureCapacity

public final void ensureCapacity(int capacity)
Guarantees that this queue will be able to buffer at least the indicated number of elements.
Parameters:
capacity - the minimum capacity of this queue

copyInto

public void copyInto(java.lang.Object[] dest)
Copies elements into Object array. Copies the elements of this queue into a destination array. Of course, the array must be sufficiently large and of the proper type.
Parameters:
dest - the destination array

priorityAt

public int priorityAt(int index)
Copies elements into Object array.

Details: priorityAt returns the priority associated with the element at the specified index.

Parameters:
index - the index
Returns:
the priority

Sharkysoft home