m865.datastructures
Class AbstractStack

java.lang.Object
  extended bym865.datastructures.AbstractStack
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection
Direct Known Subclasses:
StackAL, StackLL

public abstract class AbstractStack
extends java.lang.Object
implements java.lang.Cloneable, java.util.Collection

Abstract class for the stack data structure. It implements the push, pop and convenience methods. The optional methods of the Collections interface, which would allow the stack integrity to be violated are not implemented and throw an UnsupportedOperation exception.

Version:
2.1 09/09/05
Author:
Daniel D. Warner

Field Summary
protected  int hash
          The cached value of the hash code for the stack.
 
Constructor Summary
AbstractStack()
           
 
Method Summary
 boolean add(java.lang.Object obj)
          Pushes an object onto the top of the stack.
 boolean addAll(java.util.Collection c)
          Pushes each object of the specified collection onto the stack.
 boolean contains(java.lang.Object obj)
          Determines if the specified object is in the stack.
 boolean containsAll(java.util.Collection c)
          Determines if the specified collection is contained in the stack.
protected  void downdateHashCode(java.lang.Object obj)
          Downdates the hash code for this stack
 boolean equals(java.lang.Object obj)
          The test for equality.
 int hashCode()
          Returns the value of the hash code for this stack.
 boolean isEmpty()
          Determines whether the stack is empty.
abstract  java.lang.Object peek()
          Returns the object on the top of the stack.
abstract  java.lang.Object pop()
          Removes and returns the object on the top of the stack.
abstract  void push(java.lang.Object obj)
          Pushes an object onto the top of the stack.
 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 stack.
 java.lang.Object[] toArray(java.lang.Object[] a)
          Creates an array containing the objects in this stack.
 java.lang.String toString()
          List the objects in the stack
protected  void updateHashCode(java.lang.Object obj)
          Updates the hash code for this stack
 
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 stack. 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 stack while the iterator is being used.

Constructor Detail

AbstractStack

public AbstractStack()
Method Detail

push

public abstract void push(java.lang.Object obj)
Pushes an object onto the top of the stack.

Parameters:
obj - the object to be placed on the top of the stack.

pop

public abstract java.lang.Object pop()
Removes and returns the object on the top of the stack.

Returns:
the object on the top of the stack or null if the stack is empty.

peek

public abstract java.lang.Object peek()
Returns the object on the top of the stack. The stack remains unchanged.

Returns:
the object on the top of the stack or null if the stack is empty.

add

public boolean add(java.lang.Object obj)
Pushes an object onto the top of the stack. 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
Parameters:
obj - the object to be placed on the top of the stack.
Returns:
true because the stack has been changed.

addAll

public boolean addAll(java.util.Collection c)
Pushes each object of the specified collection onto the stack.

Specified by:
addAll in interface java.util.Collection
Parameters:
c - the collection of objects to be placed on the stack.
Returns:
true if the collection has any elements, because the stack has been changed.

contains

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

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

containsAll

public boolean containsAll(java.util.Collection c)
Determines if the specified collection is contained in the stack.

Specified by:
containsAll in interface java.util.Collection
Parameters:
c - the collection of objects to be found in the stack.
Returns:
true - if the stack 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
Parameters:
obj - the object which may be equal to this stack.
Returns:
true - if the specified object is equal to this stack.

hashCode

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

Specified by:
hashCode in interface java.util.Collection
Returns:
hash - the hashcode for this stack

updateHashCode

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

Parameters:
obj - the object being added to the stack

downdateHashCode

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

Parameters:
obj - the object being removed from the stack

isEmpty

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

Specified by:
isEmpty in interface java.util.Collection
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 stack. Since this would violate the integrity of the abstract stack data structure, it is not implemented.

Specified by:
remove in interface java.util.Collection
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 stack. Since this would violate the integrity of the abstract stack data structure, it is not implemented.

Specified by:
removeAll in interface java.util.Collection
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 stack except for the objects in the specified collection. Since this would violate the integrity of the abstract stack data structure, it is not implemented.

Specified by:
retainAll in interface java.util.Collection
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 stack.

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

toArray

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

Specified by:
toArray in interface java.util.Collection
Parameters:
a - an array of objects whose run type is compatible with all the objects in the stack.
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 stack 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 stack

Returns:
a formatted string listing the objects in this stack