codesimian
Class NumberStack
java.lang.Object
java.lang.Number
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) |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
stack
protected java.util.List<java.lang.Number> stack
- Default: new java.util.ArrayList(4)
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)
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