com.sosnoski.util.stack
Class StringStack

java.lang.Object
  |
  +--com.sosnoski.util.GrowableBase
        |
        +--com.sosnoski.util.stack.StackBase
              |
              +--com.sosnoski.util.stack.StringStack

public class StringStack
extends StackBase

Growable String stack with type specific access methods. This implementation is unsynchronized in order to provide the best possible performance for typical usage scenarios, so explicit synchronization must be implemented by a wrapper class or directly by the application in cases where instances are modified in a multithreaded environment. See the base classes for other details of the implementation.

Version:
1.1
Author:
Dennis M. Sosnoski

Field Summary
protected  java.lang.String[] m_baseArray
          The underlying array used for storing the data.
 
Fields inherited from class com.sosnoski.util.stack.StackBase
m_countPresent
 
Fields inherited from class com.sosnoski.util.GrowableBase
DEFAULT_SIZE, m_countLimit, m_maximumGrowth
 
Constructor Summary
StringStack()
          Default constructor.
StringStack(int size)
          Constructor with initial size specified.
StringStack(int size, int growth)
          Constructor with full specification.
StringStack(StringStack base)
          Copy (clone) constructor.
 
Method Summary
 java.lang.Object clone()
          Duplicates the object with the generic call.
protected  java.lang.Object getArray()
          Get the backing array.
 java.util.Iterator iterator()
          Return an iterator for the Strings in this stack.
 java.lang.String peek()
          Copy top value from the stack.
 java.lang.String peek(int depth)
          Copy a value from the stack.
 java.lang.String pop()
          Pop a value from the stack.
 java.lang.String pop(int count)
          Pop multiple values from the stack.
 void push(java.lang.String value)
          Push a value on the stack.
protected  void setArray(java.lang.Object array)
          Set the backing array.
 java.lang.String[] toArray()
          Constructs and returns a simple array containing the same data as held in this stack.
 
Methods inherited from class com.sosnoski.util.stack.StackBase
buildArray, clear, getAddIndex, isEmpty, size
 
Methods inherited from class com.sosnoski.util.GrowableBase
buildArray, discardValues, ensureCapacity, growArray, resizeCopy
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_baseArray

protected java.lang.String[] m_baseArray
The underlying array used for storing the data.
Constructor Detail

StringStack

public StringStack(int size,
                   int growth)
Constructor with full specification.
Parameters:
size - number of String values initially allowed in stack
growth - maximum size increment for growing stack

StringStack

public StringStack(int size)
Constructor with initial size specified.
Parameters:
size - number of String values initially allowed in stack

StringStack

public StringStack()
Default constructor.

StringStack

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

getArray

protected java.lang.Object getArray()
Get the backing array. This method is used by the type-agnostic base class code to access the array used for type-specific storage.
Overrides:
getArray in class GrowableBase
Returns:
backing array object

setArray

protected void setArray(java.lang.Object array)
Set the backing array. This method is used by the type-agnostic base class code to set the array used for type-specific storage.
Overrides:
setArray in class GrowableBase
Parameters:
backing - array object

push

public void push(java.lang.String value)
Push a value on the stack.
Parameters:
value - value to be added

pop

public java.lang.String pop()
Pop a value from the stack.
Returns:
value from top of stack
Throws:
java.lang.ArrayIndexOutOfBoundsException - on attempt to pop empty stack

pop

public java.lang.String pop(int count)
Pop multiple values from the stack. The last value popped is the one returned.
Parameters:
count - number of values to pop from stack (must be strictly positive)
Returns:
value from top of stack
Throws:
java.lang.ArrayIndexOutOfBoundsException - on attempt to pop past end of stack

peek

public java.lang.String peek(int depth)
Copy a value from the stack. This returns a value from within the stack without modifying the stack.
Parameters:
depth - depth of value to be returned
Returns:
value from stack
Throws:
java.lang.ArrayIndexOutOfBoundsException - on attempt to peek past end of stack

peek

public java.lang.String peek()
Copy top value from the stack. This returns the top value without removing it from the stack.
Returns:
value at top of stack
Throws:
java.lang.ArrayIndexOutOfBoundsException - on attempt to peek empty stack

iterator

public final java.util.Iterator iterator()
Return an iterator for the Strings in this stack. The iterator returns all values in order the order they were added, but is not "live". Values added to the stack during iteration will not be returned by the iteration, and any other changes to the stack while the iteration is in progress will give indeterminant results.
Returns:
iterator for values in stack

toArray

public java.lang.String[] toArray()
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.
Returns:
array containing a copy of the data

clone

public java.lang.Object clone()
Duplicates the object with the generic call.
Overrides:
clone in class java.lang.Object
Returns:
a copy of the object


Company Web Site

XML Benchmark Home