EDU.bmrb.starlibj
Class VectorCheckType

java.lang.Object
  |
  +--EDU.bmrb.starlibj.VectorCheckType
Direct Known Subclasses:
BlockListVector, DataValuesVector, LoopRowsVector, NameListVector, SaveListVector, StarListVector, TagsVector

public class VectorCheckType
extends java.lang.Object

VectorCheckType is essentially the exact same thing as the standard Java class java.util.vector, but with the additional provisions to ensure that only objects of a specific type will be allowed to be put into the vector. Anything else is deemed an error and generates an exception. Essentially, what you do is you create an object of type VectorCheckType, then add the types you want it to be able to hold using addType(). Then prevent any future types from being added with freezeTypes(). Until you have done this, you cannot insert anything into the vector. Once you have called freezeTypes(), you cannot call addType() again. The idea is to provide a generic way to implement something like the C++ concept of template classes - we want to have a vector that only allows some types of object, not all types of object. Typically, addType() and freezeTypes() will have already been called internally in the library before the user programmer gets to use the vector. For example, StarFileNode will use a VectorCheckType that has been set up to only hold BlockNodes.

A typical piece of code using VectorCheckType might look like this:
Right
             VectorCheckType aVect =
                 new VectorCheckType();
             [...snip...]

             // Make the vector accept
             // only items and loops:
             aVect.addType(
                 Class.forName( StarValidity.clsNameDataItemNode) );
             aVect.addType( 
                 Class.forName( StarValidity.clsNameDataLoopNode) );
             aVect.freezeTypes();

             aVect.addElement( 
                 new DataItemNode([...snip...]);
         
Wrong Wrong
             VectorCheckType aVect =
                 new VectorCheckType();
             [...snip...]

             // Make the vector accept
             // only items and loops:
             aVect.addType(
                 Class.forName( StarValidity.clsNameDataItemNode) );
             aVect.addType( 
                 Class.forName( StarValidity.clsNameDataLoopNode) );

             // This attempt to add an element
             // before the list was frozen
             // produces and exception.
             aVect.addElement( 
                 new DataItemNode([...snip...]);

             aVect.freezeTypes();
         
             VectorCheckType aVect =
                 new VectorCheckType();
             [...snip...]

             // Make the vector accept
             // only items and loops:
             aVect.addType(
                 Class.forName( StarValidity.clsNameDataItemNode) );
             aVect.addType( 
                 Class.forName( StarValidity.clsNameDataLoopNode) );
             aVect.freezeTypes();

             // This attempt to add an
             // element of the wrong type
             // produces an exception.
             aVect.addElement( SomeOtherType );

             // This is also an exception:
             //   Attempting to add more
             //   types after freezeTypes()
             //   has been called.
             aVect.addType( SomeOtherType);
         


Field Summary
protected  java.util.Vector data
           
protected  java.util.Vector types
           
protected  boolean typesFrozen
           
 
Constructor Summary
VectorCheckType()
          makes an empty vector
VectorCheckType(int startCap)
          makes an empty vector with a starting capacity
VectorCheckType(int startCap, int incr)
          Constructs an empty vector with starting capacity and amount to increment it by when it is overflown.
 
Method Summary
 void addElement(java.lang.Object obj)
          Just like the Vector method of the same name.
 void addType(java.lang.Class typ)
          Adds another type to the list of types that the class will allow to be inserted.
 int capacity()
          Just like the Vector method of the same name.
 boolean contains(java.lang.Object obj)
          Just like the Vector method of the same name.
 java.lang.Object elementAt(int index)
          Just like the Vector method of the same name.
 java.util.Enumeration elements()
          Just like the Vector method of the same name.
 java.lang.Object firstElement()
          Just like the Vector method of the same name.
 void freezeTypes()
          Freezes the class like it is such that no more types can be added to the list of acceptable types for this vector to hold.
 int indexOf(java.lang.Object obj)
          Just like the Vector method of the same name.
 int indexOf(java.lang.Object obj, int index)
          Just like the Vector method of the same name.
 void insertElementAt(java.lang.Object obj, int index)
          Just like the Vector method of the same name.
 boolean isEmpty()
          Just like the Vector method of the same name.
 boolean isObjectAllowed(java.lang.Object o)
          Used to ask "is this object allowed in this class?" (In other words, "Was there a previous call to addType() that allowed it to handle this kind of class?")
 java.lang.Object lastElement()
          Just like the Vector method of the same name.
 int lastIndexOf(java.lang.Object obj)
          Just like the Vector method of the same name.
 int lastIndexOf(java.lang.Object obj, int index)
          Just like the Vector method of the same name.
 boolean removeElement(java.lang.Object obj)
          Just like the Vector method of the same name.
 void removeElementAt(int index)
          Similar to the Vector method of the same name.
 void setElementAt(java.lang.Object obj, int index)
          Just like the Vector method of the same name.
 void setSize(int newSize)
          Just like the Vector method of the same name.
 int size()
          Just like the Vector method of the same name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

types

protected java.util.Vector types

data

protected java.util.Vector data

typesFrozen

protected boolean typesFrozen
Constructor Detail

VectorCheckType

public VectorCheckType()
makes an empty vector

VectorCheckType

public VectorCheckType(int startCap)
makes an empty vector with a starting capacity

VectorCheckType

public VectorCheckType(int startCap,
                       int incr)
Constructs an empty vector with starting capacity and amount to increment it by when it is overflown.
Method Detail

addType

public void addType(java.lang.Class typ)
             throws TypesAreFrozen
Adds another type to the list of types that the class will allow to be inserted. This must be done before the vector can have any values inserted into it.
See Also:
freezeTypes

freezeTypes

public void freezeTypes()
Freezes the class like it is such that no more types can be added to the list of acceptable types for this vector to hold. Until this is done, none of the insertion functions for this vector will be allowed.

setSize

public void setSize(int newSize)
Just like the Vector method of the same name.
See Also:
java.util.Vector.setSize

capacity

public int capacity()
Just like the Vector method of the same name.
See Also:
java.util.Vector.capacity

size

public int size()
Just like the Vector method of the same name.
See Also:
java.util.Vector.size

isEmpty

public boolean isEmpty()
Just like the Vector method of the same name.
See Also:
java.util.Vector.isEmpty

elements

public java.util.Enumeration elements()
Just like the Vector method of the same name.
See Also:
java.util.Vector.Enumeration

contains

public boolean contains(java.lang.Object obj)
Just like the Vector method of the same name.
See Also:
java.util.Vector.contains

indexOf

public int indexOf(java.lang.Object obj)
Just like the Vector method of the same name.
See Also:
java.util.Vector.indexOf

indexOf

public int indexOf(java.lang.Object obj,
                   int index)
Just like the Vector method of the same name.
See Also:
java.util.Vector.indexOf

lastIndexOf

public int lastIndexOf(java.lang.Object obj)
Just like the Vector method of the same name.
See Also:
java.util.Vector.lastIndexOf

lastIndexOf

public int lastIndexOf(java.lang.Object obj,
                       int index)
Just like the Vector method of the same name.
See Also:
java.util.Vector.lastIndexOf

elementAt

public java.lang.Object elementAt(int index)
Just like the Vector method of the same name.
See Also:
java.util.Vector.elementAt

firstElement

public java.lang.Object firstElement()
Just like the Vector method of the same name.
See Also:
java.util.Vector.firstElement

lastElement

public java.lang.Object lastElement()
Just like the Vector method of the same name.
See Also:
java.util.Vector.lastElement

setElementAt

public void setElementAt(java.lang.Object obj,
                         int index)
                  throws WrongElementType,
                         TypesNotFrozenYet
Just like the Vector method of the same name.
See Also:
java.util.Vector.setElementAt

removeElementAt

public void removeElementAt(int index)
Similar to the Vector method of the same name.
See Also:
java.util.Vector.removeElementAt

insertElementAt

public void insertElementAt(java.lang.Object obj,
                            int index)
                     throws WrongElementType,
                            TypesNotFrozenYet
Just like the Vector method of the same name.
See Also:
java.util.Vector.insertElementAt

addElement

public void addElement(java.lang.Object obj)
                throws WrongElementType,
                       TypesNotFrozenYet
Just like the Vector method of the same name.
See Also:
java.util.Vector.addElement

removeElement

public boolean removeElement(java.lang.Object obj)
Just like the Vector method of the same name.
See Also:
java.util.Vector.removeElement

isObjectAllowed

public boolean isObjectAllowed(java.lang.Object o)
Used to ask "is this object allowed in this class?" (In other words, "Was there a previous call to addType() that allowed it to handle this kind of class?")
Parameters:
o - the object to check for.
See Also:
addType()