m865.datastructures
Class AbstractQueue<T>

java.lang.Object
  extended by m865.datastructures.AbstractQueue<T>
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<T>, java.util.Collection<T>
Direct Known Subclasses:
QueueCL, QueueLL

public abstract class AbstractQueue<T>
extends java.lang.Object
implements java.lang.Cloneable, java.util.Collection<T>

Abstract class for the queue data structure. It implements the enqueue, dequeue and convenience methods. The optional methods of the Collections interface, which would allow the queue integrity to be violated are not implemented and throw an UnsupportedOperation exception. Version 3 incorporates generics


Field Summary
protected  int hash
          The cached value of the hash code for the queue.
 
Constructor Summary
AbstractQueue()
           
 
Method Summary
 boolean add(T x)
          Enqueues an object onto the end of the queue.
 boolean addAll(java.util.Collection<? extends T> c)
          Enqueues each object of the specified collection onto the queue.
 boolean contains(java.lang.Object obj)
          Determines if the specified object is in the queue.
 boolean containsAll(java.util.Collection<?> c)
          Determines if the specified collection is contained in the queue.
abstract  T dequeue()
          Removes and returns the type T object at the beginning of the queue.
protected  void downdateHashCode(java.lang.Object obj)
          Downdates the hash code for this queue
abstract  void enqueue(T x)
          Appends a T object onto the end of the queue.
 boolean equals(java.lang.Object obj)
          The test for equality.
 int hashCode()
          Returns the value of the hash code for this queue.
 boolean isEmpty()
          Determines whether the queue is empty.
abstract  T peek()
          Returns the type T object on the beginning of the queue.
 boolean remove(java.lang.Object obj)
          This is an optional method of the Collection Interface.
 boolean removeAll(java.util.Collection<?> c)
          This is an optional method of the Collection Interface.
 boolean retainAll(java.util.Collection<?> c)
          This is an optional method of the Collection Interface.
 java.lang.Object[] toArray()
          Creates an array containing the objects in this queue.
<T> T[]
toArray(T[] a)
          Creates an array containing the objects in this queue.
 java.lang.String toString()
          List the objects in the queue
protected  void updateHashCode(java.lang.Object obj)
          Updates the hash code for this queue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
clear, iterator, size
 

Field Detail

hash

protected int hash
The cached value of the hash code for the queue. It is assumed that push, pop, clear, and clone methods will properly update this hash code. The cached value of the code should be used by iterators to check for any changes in the queue while the iterator is being used.

Constructor Detail

AbstractQueue

public AbstractQueue()
Method Detail

enqueue

public abstract void enqueue(T x)
Appends a T object onto the end of the queue.

Parameters:
x - the type T object to be appended to the end of the queue.

dequeue

public abstract T dequeue()
Removes and returns the type T object at the beginning of the queue.

Returns:
the object at the beginning of the queue or null if the queue is empty.

peek

public abstract T peek()
Returns the type T object on the beginning of the queue. The queue remains unchanged.

Returns:
the object at the beginning of the queue or null if the queue is empty.

add

public boolean add(T x)
Enqueues an object onto the end of the queue. Consistent with the Collection interface requirement that this method should ensure that this collection contains the specified object.

Specified by:
add in interface java.util.Collection<T>
Parameters:
x - the type T object to be placed at the end of the queue.
Returns:
true if the object is not null, because the queue has been changed.

addAll

public boolean addAll(java.util.Collection<? extends T> c)
Enqueues each object of the specified collection onto the queue.

Specified by:
addAll in interface java.util.Collection<T>
Parameters:
c - the collection of objects to be added to the queue.
Returns:
true if the collection has any elements, because the queue has been changed.

contains

public boolean contains(java.lang.Object obj)
Determines if the specified object is in the queue.

Specified by:
contains in interface java.util.Collection<T>
Parameters:
obj - the object to be found in the queue.
Returns:
true - if the specified object is in the queue.

containsAll

public boolean containsAll(java.util.Collection<?> c)
Determines if the specified collection is contained in the queue.

Specified by:
containsAll in interface java.util.Collection<T>
Parameters:
c - the collection of objects to be found in the queue.
Returns:
true - if the queue contains all of the objects in the specified collection.

equals

public boolean equals(java.lang.Object obj)
The test for equality.

Specified by:
equals in interface java.util.Collection<T>
Overrides:
equals in class java.lang.Object
Parameters:
obj - the object which may be equal to this queue.
Returns:
true - if the specified object is equal to this queue.

hashCode

public int hashCode()
Returns the value of the hash code for this queue. This method should return the same hash code for any two queues in the mthsc865.datastructures package that have the same objects stored in the same order.

Specified by:
hashCode in interface java.util.Collection<T>
Overrides:
hashCode in class java.lang.Object
Returns:
hash - the hashcode for this queue

updateHashCode

protected void updateHashCode(java.lang.Object obj)
Updates the hash code for this queue

Parameters:
obj - the object being added to the queue

downdateHashCode

protected void downdateHashCode(java.lang.Object obj)
Downdates the hash code for this queue

Parameters:
obj - the object being removeed from the queue

isEmpty

public boolean isEmpty()
Determines whether the queue is empty.

Specified by:
isEmpty in interface java.util.Collection<T>
Returns:
true - if this collection contains no elements.

remove

public boolean remove(java.lang.Object obj)
This is an optional method of the Collection Interface. If implemented, it would remove a single instance of the specified object from the queue. Since this would violate the integrity of the abstract queue data structure, it is not implemented.

Specified by:
remove in interface java.util.Collection<T>
Parameters:
obj - the object to be removed.
Returns:
throws an UnsupportedOperationException

removeAll

public boolean removeAll(java.util.Collection<?> c)
This is an optional method of the Collection Interface. If implemented, it would remove a single instance of each item in the specified collection from the queue. Since this would violate the integrity of the abstract queue data structure, it is not implemented.

Specified by:
removeAll in interface java.util.Collection<T>
Parameters:
c - the collection of objects to be removed.
Returns:
throws an UnsupportedOperationException

retainAll

public boolean retainAll(java.util.Collection<?> c)
This is an optional method of the Collection Interface. If implemented, it would remove all of the objects in the queue except for the objects in the specified collection. Since this would violate the integrity of the abstract queue data structure, it is not implemented.

Specified by:
retainAll in interface java.util.Collection<T>
Parameters:
c - the collection of objects to be retained.
Returns:
throws an UnsupportedOperationException

toArray

public java.lang.Object[] toArray()
Creates an array containing the objects in this queue.

Specified by:
toArray in interface java.util.Collection<T>
Returns:
an array of objects.

toArray

public <T> T[] toArray(T[] a)
Creates an array containing the objects in this queue.

Specified by:
toArray in interface java.util.Collection<T>
Parameters:
a - an array of objects whose run type is compatible with all the objects in the queue.
Returns:
an array of objects of the same type as the elements of a. If a is large enough, then all the objects in the queue are copied into a, with nulls stored in any extra empty entries of a. If a is not large enough, then the method returns a new array whose objects have the same type as a.

toString

public java.lang.String toString()
List the objects in the queue

Overrides:
toString in class java.lang.Object
Returns:
a formatted string listing the objects in this queue