All Packages Class Hierarchy This Package Previous Next Index
Class EDU.bmrb.starlibj.VectorCheckType
java.lang.Object
|
+----EDU.bmrb.starlibj.VectorCheckType
- public class VectorCheckType
- extends 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);
|
-
data
-
-
types
-
-
typesFrozen
-
-
VectorCheckType()
- makes an empty vector
-
VectorCheckType(int)
- makes an empty vector with a starting capacity
-
VectorCheckType(int, int)
- Constructs an empty vector with starting capacity and amount to
increment it by when it is overflown.
-
addElement(Object)
- Just like the Vector method of the same name.
-
addType(Class)
- Adds another type to the list of types that the class
will allow to be inserted.
-
capacity()
- Just like the Vector method of the same name.
-
contains(Object)
- Just like the Vector method of the same name.
-
elementAt(int)
- Just like the Vector method of the same name.
-
elements()
- Just like the Vector method of the same name.
-
firstElement()
- Just like the Vector method of the same name.
-
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.
-
indexOf(Object)
- Just like the Vector method of the same name.
-
indexOf(Object, int)
- Just like the Vector method of the same name.
-
insertElementAt(Object, int)
- Just like the Vector method of the same name.
-
isEmpty()
- Just like the Vector method of the same name.
-
isObjectAllowed(Object)
- 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?")
-
lastElement()
- Just like the Vector method of the same name.
-
lastIndexOf(Object)
- Just like the Vector method of the same name.
-
lastIndexOf(Object, int)
- Just like the Vector method of the same name.
-
removeElement(Object)
- Just like the Vector method of the same name.
-
removeElementAt(int)
- Similar to the Vector method of the same name.
-
setElementAt(Object, int)
- Just like the Vector method of the same name.
-
setSize(int)
- Just like the Vector method of the same name.
-
size()
- Just like the Vector method of the same name.
types
protected Vector types
data
protected Vector data
typesFrozen
protected boolean typesFrozen
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.
addType
public void addType(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:
- setSize
capacity
public int capacity()
- Just like the Vector method of the same name.
- See Also:
- capacity
size
public int size()
- Just like the Vector method of the same name.
- See Also:
- size
isEmpty
public boolean isEmpty()
- Just like the Vector method of the same name.
- See Also:
- isEmpty
elements
public Enumeration elements()
- Just like the Vector method of the same name.
- See Also:
- Enumeration
contains
public boolean contains(Object obj)
- Just like the Vector method of the same name.
- See Also:
- contains
indexOf
public int indexOf(Object obj)
- Just like the Vector method of the same name.
- See Also:
- indexOf
indexOf
public int indexOf(Object obj,
int index)
- Just like the Vector method of the same name.
- See Also:
- indexOf
lastIndexOf
public int lastIndexOf(Object obj)
- Just like the Vector method of the same name.
- See Also:
- lastIndexOf
lastIndexOf
public int lastIndexOf(Object obj,
int index)
- Just like the Vector method of the same name.
- See Also:
- lastIndexOf
elementAt
public Object elementAt(int index)
- Just like the Vector method of the same name.
- See Also:
- elementAt
firstElement
public Object firstElement()
- Just like the Vector method of the same name.
- See Also:
- firstElement
lastElement
public Object lastElement()
- Just like the Vector method of the same name.
- See Also:
- lastElement
setElementAt
public void setElementAt(Object obj,
int index) throws WrongElementType, TypesNotFrozenYet
- Just like the Vector method of the same name.
- See Also:
- setElementAt
removeElementAt
public void removeElementAt(int index)
- Similar to the Vector method of the same name.
- See Also:
- removeElementAt
insertElementAt
public void insertElementAt(Object obj,
int index) throws WrongElementType, TypesNotFrozenYet
- Just like the Vector method of the same name.
- See Also:
- insertElementAt
addElement
public void addElement(Object obj) throws WrongElementType, TypesNotFrozenYet
- Just like the Vector method of the same name.
- See Also:
- addElement
removeElement
public boolean removeElement(Object obj)
- Just like the Vector method of the same name.
- See Also:
- removeElement
isObjectAllowed
public boolean isObjectAllowed(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
All Packages Class Hierarchy This Package Previous Next Index