ESC/Java2
© 2003,2004,2005 David Cok and Joseph Kiniry
© 2005 UCD Dublin
© 2003,2004 Radboud University Nijmegen
© 1999,2000 Compaq Computer Corporation
© 1997,1998,1999 Digital Equipment Corporation
All Rights Reserved

javafe.util
Class StackVector

java.lang.Object
  extended byjavafe.util.StackVector

public final class StackVector
extends java.lang.Object

A stack of Vector objects.

Contains 1 or more Vectors arranged in a stack. Direct access is allowed only to the elements of the top Vector via a subset of the usual java.util.Vector operations. These Vectors may not contain null elements.

push() pushs a zero-length Vector on the stack, pop() discards the top Vector, and merge() combines the top two Vectors.

The caller is responsible for ensuring that pop() and merge() are called only when the stack has at least 2 Vectors on it.

All the elements in the Vectors must be of type elementType.

Note: Although this class as a whole is thread safe, individual instances are not. Thus, different threads can safely access different instances of StackVector without any synchronization, but different threads accessing the same instance will have to lock and synchronize.


Field Summary
private  int currentStackBottom
           
private  int elementCount
           
private  java.lang.Object[] elements
          Our data representation is as follows: elements[0], ..., elements[elementCount-1] contain the Vectors on our stack, stored as sequences of elements from the bottom of the stack (pushed longest ago) to the top of the stack.
private  int vectorCount
           
 
Constructor Summary
StackVector()
          Create a StackVector that contains only 1 zero-length Vector.
 
Method Summary
 void addElement(java.lang.Object x)
          Add an element at the end of the top Vector.
private  void addElementInternal(java.lang.Object x)
          Add x to the end of the top Vector.
private  void checkBounds(int index)
           
 void clear()
          Reset us to the state where we contain only 1 Vector, which has zero-length.
 boolean contains(java.lang.Object o)
          Return true iff the top Vector contains o.
 void copyInto(java.lang.Object[] dst)
           
 java.lang.Object elementAt(int i)
          Return the element in the top Vector at the given index.
 void merge()
          Merge the top Vector with the Vector just under it by appending the former to the latter.
 void pop()
          Pop off the current top Vector.
 void push()
          Push a zero-length Vector.
 void removeAllElements()
          Zero the top Vector.
 void setElementAt(java.lang.Object o, int i)
           
 int size()
          Return the size of the top Vector.
 java.util.Vector stackContents()
          Returns the contents of all the vectors on the stack as a single vector.
 java.lang.String toString()
           
 int vectors()
          Return the number of Vectors on our stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

elements

private java.lang.Object[] elements
Our data representation is as follows:

elements[0], ..., elements[elementCount-1] contain the Vectors on our stack, stored as sequences of elements from the bottom of the stack (pushed longest ago) to the top of the stack. Each sequence is seperated by a null element.

Example: [1, 2, null, 3, 4, null, 5, 6] represents the stack [1, 2], [3, 4], [5, 6] where the Vector [5, 6] is on top so elementAt(1) will return 6.

To speed access to the top Vector, currentStackBottom points to the first element of the top Vector (5 in the example). Note that this may point just beyond the last element of elements if the top Vector is zero-length.

vectorCount holds the # of Vectors; it exists so that clients can make sure they've left a StackVector the way they found it and so that preconditions can be written for pop(), etc.


elementCount

private int elementCount

currentStackBottom

private int currentStackBottom

vectorCount

private int vectorCount
Constructor Detail

StackVector

public StackVector()
Create a StackVector that contains only 1 zero-length Vector.

Method Detail

size

public final int size()
Return the size of the top Vector.

Indices into the top Vector range from 0 to size()-1.


checkBounds

private void checkBounds(int index)

elementAt

public java.lang.Object elementAt(int i)
Return the element in the top Vector at the given index.

Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is out of range.

setElementAt

public void setElementAt(java.lang.Object o,
                         int i)

addElementInternal

private void addElementInternal(java.lang.Object x)
Add x to the end of the top Vector.

x may be null, in which case the caller needs to cleanup to ensure that a null element does not stay in the top Vector.


addElement

public final void addElement(java.lang.Object x)
Add an element at the end of the top Vector.


removeAllElements

public final void removeAllElements()
Zero the top Vector.


copyInto

public final void copyInto(java.lang.Object[] dst)

contains

public final boolean contains(java.lang.Object o)
Return true iff the top Vector contains o.


clear

public void clear()
Reset us to the state where we contain only 1 Vector, which has zero-length.


vectors

public final int vectors()
Return the number of Vectors on our stack.


push

public void push()
Push a zero-length Vector.


pop

public void pop()
Pop off the current top Vector.

Precondition: at least 2 Vectors are on our stack.


merge

public void merge()
Merge the top Vector with the Vector just under it by appending the former to the latter.

Example: ..., A, TOP -> ..., A^TOP

Precondition: there are at least two vectors on our stack.


stackContents

public java.util.Vector stackContents()
Returns the contents of all the vectors on the stack as a single vector.


toString

public java.lang.String toString()

ESC/Java2
© 2003,2004,2005 David Cok and Joseph Kiniry
© 2005 UCD Dublin
© 2003,2004 Radboud University Nijmegen
© 1999,2000 Compaq Computer Corporation
© 1997,1998,1999 Digital Equipment Corporation
All Rights Reserved

The ESC/Java2 Project Homepage