codesimian
Class NumberStack

java.lang.Object
  extended by java.lang.Number
      extended by codesimian.NumberStack
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
NumberStack.NeverEmpty

public class NumberStack
extends java.lang.Number

This class became almost obsolete when CS stopped being a subclass of Number, although CS still can be cast to Number, but often the dynamic properties are lost.

A stack of Numbers. Number functions return the same call on the top value.

WARNING: Be careful not to put this NumberStack inside itsself, or some other cycle. It does not have infinite-loop-protection (CSs do), but if it contains only CSs (CS extends Number) and they have that protection option on, then they can stop the infinite-loops.

NumberStack makes the syntax for specifying complex OPTIONS easy. Just use a Number variable and set its first value in my constructor. NumberStack is often used as a stack of default values for a certain option. Others can push and pop their own options without damaging anyone else's options.

If you do this: Number n = new NumberStack(...),
instead of: NumberStack n = new NumberStack(...),
then: users of your class might not know it is a NumberStack,
and they can simply replace it with new Integer(5) for example.
If they suspect its a NumberStack, they can check its type and push/pop.
It has almost the simplicity of a primitive type, but supports complex behavior.

Example:
Number defaultSize = new NumberStack(7.3);
int size = defaultSize.intValue(); //7, not 7.3
((NumberStack)defaultSize) .push((byte)3);
. //defaultSize returns 3
. CS complexValue = CS.compile("*(?xSize ?ySize ?zSize)"); . ((NumberStack)defaultSize) .push(complexValue);
. . //defaultSize returns complexValue.doubleValue() or .intValue() etc
. ((NumberStack)defaultSize) .pop();
. //defaultSize returns 3
((NumberStack)defaultSize) .pop();
//defaultSize returns 7.3

See Also:
Serialized Form

Nested Class Summary
static class NumberStack.NeverEmpty
          replaces pop() with peek() if pop() would empty the stack.
 
Field Summary
protected  java.util.List<java.lang.Number> stack
          Default: new java.util.ArrayList(4)
 
Constructor Summary
NumberStack()
           
NumberStack(byte pushMe)
           
NumberStack(double pushMe)
           
NumberStack(float pushMe)
           
NumberStack(int pushMe)
           
NumberStack(long pushMe)
           
NumberStack(java.lang.Number pushMe)
           
NumberStack(short pushMe)
           
 
Method Summary
 byte byteValue()
          returns the byte value of the top Number on this stack
 double doubleValue()
           
 float floatValue()
           
 int intValue()
           
 long longValue()
           
 java.lang.Number peek()
           
 java.lang.Number pop()
           
 void push(byte pushMe)
           
 void push(double pushMe)
           
 void push(float pushMe)
           
 void push(int pushMe)
           
 void push(long pushMe)
           
 void push(java.lang.Number pushMe)
           
 void push(short pushMe)
           
 short shortValue()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stack

protected java.util.List<java.lang.Number> stack
Default: new java.util.ArrayList(4)

Constructor Detail

NumberStack

public NumberStack()

NumberStack

public NumberStack(java.lang.Number pushMe)

NumberStack

public NumberStack(byte pushMe)

NumberStack

public NumberStack(short pushMe)

NumberStack

public NumberStack(int pushMe)

NumberStack

public NumberStack(long pushMe)

NumberStack

public NumberStack(float pushMe)

NumberStack

public NumberStack(double pushMe)
Method Detail

push

public void push(java.lang.Number pushMe)

push

public void push(byte pushMe)

push

public void push(short pushMe)

push

public void push(int pushMe)

push

public void push(long pushMe)

push

public void push(float pushMe)

push

public void push(double pushMe)

peek

public java.lang.Number peek()

pop

public java.lang.Number pop()

byteValue

public byte byteValue()
returns the byte value of the top Number on this stack

Overrides:
byteValue in class java.lang.Number

shortValue

public short shortValue()
Overrides:
shortValue in class java.lang.Number

intValue

public int intValue()
Specified by:
intValue in class java.lang.Number

longValue

public long longValue()
Specified by:
longValue in class java.lang.Number

floatValue

public float floatValue()
Specified by:
floatValue in class java.lang.Number

doubleValue

public double doubleValue()
Specified by:
doubleValue in class java.lang.Number