com.sosnoski.util.stack
Class StackBase

java.lang.Object
  |
  +--com.sosnoski.util.GrowableBase
        |
        +--com.sosnoski.util.stack.StackBase
Direct Known Subclasses:
IntStack, ObjectStack, StringStack

public abstract class StackBase
extends GrowableBase

Base class for type-specific stack classes with any type of values (including primitive types). This class builds on the basic structure provided by GrowableBase, specializing it for usage as a stack. See the base class description for details of the implementation.

Stacks based on this class are unsynchronized in order to provide the best possible performance for typical usage scenarios, so explicit synchronization must be implemented by the subclass or the application in cases where they are to be modified in a multithreaded environment.

Subclasses need to implement the abstract methods defined by the base class for working with the data array, as well as the actual data access methods (at least the basic push(), pop(), and peek() methods).

Version:
1.0
Author:
Dennis M. Sosnoski

Field Summary
protected  int m_countPresent
          The number of values currently present in the stack.
 
Fields inherited from class com.sosnoski.util.GrowableBase
DEFAULT_SIZE, m_countLimit, m_maximumGrowth
 
Constructor Summary
StackBase(int size, java.lang.Class type)
          Constructor with partial specification.
StackBase(int size, int growth, java.lang.Class type)
          Constructor with full specification.
StackBase(StackBase base)
          Copy (clone) constructor.
 
Method Summary
protected  java.lang.Object buildArray(java.lang.Class type)
          Constructs and returns a simple array containing the same data as held in this stack.
 void clear()
          Set the stack to the empty state.
protected  int getAddIndex()
          Gets the array offset for appending a value to those in the stack.
 boolean isEmpty()
          Check if stack is empty.
 int size()
          Get the number of values currently present in the stack.
 
Methods inherited from class com.sosnoski.util.GrowableBase
buildArray, discardValues, ensureCapacity, getArray, growArray, resizeCopy, setArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_countPresent

protected int m_countPresent
The number of values currently present in the stack.
Constructor Detail

StackBase

public StackBase(int size,
                 int growth,
                 java.lang.Class type)
Constructor with full specification.
Parameters:
size - number of elements initially allowed in stack
growth - maximum size increment for growing stack
type - stack element type

StackBase

public StackBase(int size,
                 java.lang.Class type)
Constructor with partial specification.
Parameters:
size - number of elements initially allowed in stack
type - stack element type

StackBase

public StackBase(StackBase base)
Copy (clone) constructor.
Parameters:
base - instance being copied
Method Detail

getAddIndex

protected int getAddIndex()
Gets the array offset for appending a value to those in the stack. If the underlying array is full, it is grown by the appropriate size increment so that the index value returned is always valid for the array in use by the time of the return.
Returns:
index position for added element

size

public int size()
Get the number of values currently present in the stack.
Returns:
count of values present

isEmpty

public boolean isEmpty()
Check if stack is empty.
Returns:
true if stack empty, false if not

clear

public void clear()
Set the stack to the empty state.

buildArray

protected java.lang.Object buildArray(java.lang.Class type)
Constructs and returns a simple array containing the same data as held in this stack. Note that the items will be in reverse pop order, with the last item to be popped from the stack as the first item in the array.
Parameters:
type - element type for constructed array
Returns:
array containing a copy of the data


Company Web Site

XML Benchmark Home