|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--EDU.bmrb.starlibj.VectorCheckType
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 |
protected java.util.Vector types
protected java.util.Vector data
protected boolean typesFrozen
Constructor Detail |
public VectorCheckType()
public VectorCheckType(int startCap)
public VectorCheckType(int startCap, int incr)
Method Detail |
public void addType(java.lang.Class typ) throws TypesAreFrozen
freezeTypes
public void freezeTypes()
public void setSize(int newSize)
java.util.Vector.setSize
public int capacity()
java.util.Vector.capacity
public int size()
java.util.Vector.size
public boolean isEmpty()
java.util.Vector.isEmpty
public java.util.Enumeration elements()
java.util.Vector.Enumeration
public boolean contains(java.lang.Object obj)
java.util.Vector.contains
public int indexOf(java.lang.Object obj)
java.util.Vector.indexOf
public int indexOf(java.lang.Object obj, int index)
java.util.Vector.indexOf
public int lastIndexOf(java.lang.Object obj)
java.util.Vector.lastIndexOf
public int lastIndexOf(java.lang.Object obj, int index)
java.util.Vector.lastIndexOf
public java.lang.Object elementAt(int index)
java.util.Vector.elementAt
public java.lang.Object firstElement()
java.util.Vector.firstElement
public java.lang.Object lastElement()
java.util.Vector.lastElement
public void setElementAt(java.lang.Object obj, int index) throws WrongElementType, TypesNotFrozenYet
java.util.Vector.setElementAt
public void removeElementAt(int index)
java.util.Vector.removeElementAt
public void insertElementAt(java.lang.Object obj, int index) throws WrongElementType, TypesNotFrozenYet
java.util.Vector.insertElementAt
public void addElement(java.lang.Object obj) throws WrongElementType, TypesNotFrozenYet
java.util.Vector.addElement
public boolean removeElement(java.lang.Object obj)
java.util.Vector.removeElement
public boolean isObjectAllowed(java.lang.Object o)
o
- the object to check for.addType()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |