FlexOr.container
Class Queue

java.lang.Object
  extended by FlexOr.container.Queue
All Implemented Interfaces:
Container, java.lang.Cloneable
Direct Known Subclasses:
PriorityQueue

public class Queue
extends java.lang.Object
implements Container

Queue implements container which has the basic add and remove methods that a queue needs. Storage can be implemented by any Container type with the property that add and remove maintain the FIFO discipline -- this holds for any Sequence; and Heap and Binary Tree if the comparison is on a serial number. The choice of storage model is made at queue creation time.

Note that using Vector (not possible until Vector implements Sequence) gives O(n) performance for remove() as the entire contents of the Vector is shifted. Swapping ends would give poor performance for add(). For an array implementation of queue storage it is best to stay away from Vector and use a CircularArray -- element[0] follows element[size-1] creating a ring-like structure.

Version:
1.0 1999 Jan 16
Author:
Gunnar Gotshalks
See Also:
Sequence, Container, SLList

Constructor Summary
Queue()
          The default constructor selects a singly linked list as the storage container.
Queue(Container container)
          The constructor permits the user to specify the type of storage container.
 
Method Summary
 void add(java.lang.Object obj)
          Add obj at the rear of the queue.
 void addWait(java.lang.Object obj)
          Add obj at the rear of the queue; wait if the queue is full.
 java.lang.Object clone()
          Return a shallow copy of the queue.
 boolean contains(java.lang.Object obj)
          Determines whether an element is in the queue.
 java.util.Enumeration<java.lang.Object> elements()
          Returns an enumerator over the contents of the queue.
 boolean equals(java.lang.Object obj)
          Check for equality of contents of two queues.
 boolean isEmpty()
          Check if the queue is empty.
 boolean isFull()
          Check if the queue is full.
 java.lang.Object remove()
          Return the first element in the queue.
 void removeAll()
          Empty the queue.
 java.lang.Object removeWait()
          Return the first element in the queue; wait if the queue is empty.
 int size()
          Returns the number of elements in the queue.
 java.lang.String toString()
          Return a string representation of the contents.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Queue

public Queue()
The default constructor selects a singly linked list as the storage container.


Queue

public Queue(Container container)
The constructor permits the user to specify the type of storage container.

The input container is cloned to minimize side effects of sharing.

Requires: container is empty
Ensures: the queue uses the input storage type.

Throws:
NotEmptyException - thrown when expecting an empty container but getting a non empty container.
Method Detail

contains

public boolean contains(java.lang.Object obj)
Determines whether an element is in the queue. Not really a queue operation but useful for testing and debugging.

Specified by:
contains in interface Container

elements

public java.util.Enumeration<java.lang.Object> elements()
Returns an enumerator over the contents of the queue.

Specified by:
elements in interface Container
Throws:
NoSuchElementException - when nextElement is called with an empty enumeration sequence.

isEmpty

public boolean isEmpty()
Check if the queue is empty.

Specified by:
isEmpty in interface Container

isFull

public boolean isFull()
Check if the queue is full.

Specified by:
isFull in interface Container

size

public int size()
Returns the number of elements in the queue.

Specified by:
size in interface Container

add

public void add(java.lang.Object obj)
Add obj at the rear of the queue.

Requires: True
Ensures: sequence = old sequence ^ obj

Specified by:
add in interface Container

addWait

public void addWait(java.lang.Object obj)
Add obj at the rear of the queue; wait if the queue is full.

Requires: True
Ensures: sequence = old sequence ^ obj


removeAll

public void removeAll()
Empty the queue.

Requires: True
Ensures: sequence = null // the empty sequence.

Specified by:
removeAll in interface Container

remove

public java.lang.Object remove()
Return the first element in the queue.

Requires: isEmpty = false
Ensures: old sequence = obj ^ sequence

Specified by:
remove in interface Container

removeWait

public java.lang.Object removeWait()
Return the first element in the queue; wait if the queue is empty.

Requires: isEmpty = false
Ensures: old sequence = obj ^ sequence


clone

public java.lang.Object clone()
Return a shallow copy of the queue.

Requires: True
Ensures: result = copy of queue

Specified by:
clone in interface Container
Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Return a string representation of the contents.

Requires: True
Ensures: result = string representation of contents

Specified by:
toString in interface Container
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Check for equality of contents of two queues. By equality we mean an equal sequence of elements as returned by the Enumerator for each queue.

Requires: True
Ensures: result = true => contents are equal
                result = false => contents are not equal

Specified by:
equals in interface Container
Overrides:
equals in class java.lang.Object