Type-specific stack collection classes. The classes in this package implement type-specific stacks based on growable arrays. They support a subset of the normal collection methods, as well as appropriate stack methods. The standard public access methods in these classes are as follows:
Method Signature | From |
void clear() | {@link com.sosnoski.util.stack.StackBase} |
Object clone() | implementation class |
void ensureCapacity(int) | {@link com.sosnoski.util.GrowableBase} |
boolean isEmpty() | {@link com.sosnoski.util.stack.StackBase} |
type peek() | implementation class |
type peek(int) | implementation class |
type pop() | implementation class |
type pop(int) | implementation class |
void push(type) | implementation class |
int size() | {@link com.sosnoski.util.stack.StackBase} |
type[] toArray() | implementation class |
The access methods are unsynchronized for best performance. The user program must implement appropriate locking if multiple threads need to access an instance of these classes while that instance may be modified.
Collections of a primitive type and of a specific object type are both supported. All variations derive directly from the {@link com.sosnoski.util.stack.StackBase} base class. To define a stack of a new type, generally you can use one of the existing classes as a base and do a text substitution of the type names.
For instance, to define a stack of instances of
Thread
just copy StringStack.java
to a new file named ThreadStack.java
, then do a global
text substitution of "Thread" for "String" in the
new file.
Primitive types are only slightly more complicated. To implement a
stack of a primitive type other than the included int
,
you're best off basing it on StringStack.java
. If you're doing
this for double
, for instance, you'd need to first substitute
"DoubleStack" for "StringStack", then "double" for "String". The last step is deleting
the "m_baseArray[m_countPresent] = null;" line within the pop
method,which is not needed for primitive types.