planet.util
Class Queue

java.lang.Object
  extended by planet.util.Queue
All Implemented Interfaces:
java.io.Serializable

public class Queue
extends java.lang.Object
implements java.io.Serializable

An implementation of queues based on arrays. The head of the queue starts out at the head of the array, allowing the queue to grow and shrink in constant time. This queue implementation is ideal for applications that require a queue with a known maximum size that expands in constant time.

Example usage:

To compute the sum of the unicode value of every character in the standard input we could use the following:

 public static void main(String[] arguments)
 {
     int charsInInput = QueueExample.countChars(argument);
     Queue q = new Queue(charsInInput);
     int unicodeSum = 0;

     if(arguments.length > 0){
         for(int i=0; i < arguments.length; i++){
               for(int j=0; j < arguments[i].length(); j++){
                   q.#remove(new Character(arguments[i].charAt(j)));
               }
           }
     }

     while(!q.isEmpty()){
          char c = ((Character)q.remove()).charValue();
          unicodeSum+=Character.getNumericValue(c);
     }

     System.out.println("Total Value: " + unicodeSum);
 }
 

Version:
$Id: Queue.java,v 1.1 2004/03/11 11:22:31 jordi Exp $
Author:
2001 duane a. bailey
See Also:
Serialized Form

Field Summary
protected  int count
          current size of queue
protected  java.lang.Object[] data
          The references to values stored within the queue.
protected  int head
          index of the head of queue.
 
Constructor Summary
Queue(int size)
          Construct a queue holding at most size elements.
 
Method Summary
 void add(java.lang.Object value)
          Add a value to the tail of the queue.
 void clear()
          Remove all the values from the queue.
 java.lang.Object get()
          Fetch the value at the head of the queue.
 boolean isEmpty()
          Determine if the queue is empty.
 boolean isFull()
          Determines if the queue is not able to accept any new values.
 java.lang.Object remove()
          Remove a value from the head of the queue.
 int size()
          Determine the number of elements within the queue Postconditions: returns the number of elements in the queue
 java.lang.String toString()
          Construct a string representation of the queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

data

protected java.lang.Object[] data
The references to values stored within the queue.


head

protected int head
index of the head of queue.


count

protected int count
current size of queue

Constructor Detail

Queue

public Queue(int size)
Construct a queue holding at most size elements. Postconditions: create a queue capable of holding at most size values

Parameters:
size - The maximum size of the queue.
Method Detail

add

public void add(java.lang.Object value)
         throws QueueFull
Add a value to the tail of the queue. Preconditions: the queue is not full Postconditions: the value is added to the tail of the structure

Parameters:
value - The value added.
Throws:
QueueFull

remove

public java.lang.Object remove()
Remove a value from the head of the queue. Preconditions: the queue is not empty Postconditions: the head of the queue is removed and returned

Returns:
The value actually removed.

get

public java.lang.Object get()
Fetch the value at the head of the queue. Preconditions: the queue is not empty Postconditions: the element at the head of the queue is returned

Returns:
Reference to the first value of the queue.

size

public int size()
Determine the number of elements within the queue Postconditions: returns the number of elements in the queue

Returns:
The number of elements within the queue.

clear

public void clear()
Remove all the values from the queue. Postconditions: removes all elements from the queue


isFull

public boolean isFull()
Determines if the queue is not able to accept any new values. Postconditions: returns true if the queue is at its capacity

Returns:
True iff the queue is full.

isEmpty

public boolean isEmpty()
Determine if the queue is empty. Postconditions: returns true iff the queue is empty

Returns:
True iff the queue is empty.

toString

public java.lang.String toString()
Construct a string representation of the queue. Postconditions: returns string representation of queue

Overrides:
toString in class java.lang.Object
Returns:
String representing the queue.