com.rubecula.jquantity
Class Mill

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractList
              |
              +--java.util.Vector
                    |
                    +--java.util.Stack
                          |
                          +--com.rubecula.jquantity.Mill
All Implemented Interfaces:
Auditable, java.lang.Cloneable, java.util.Collection, java.util.List, Lookupable, Millable, java.util.RandomAccess, java.io.Serializable
Direct Known Subclasses:
Mill

public class Mill
extends java.util.Stack
implements Millable

This class is responsible for evaluating operations on Presentable objects. It is named in honor of Charles Babbage, and not forgetting the early days of computing, especially in the United Kingdom, and paricularly for Maurice Wilkes.

Version:
$Revision: 1.12 $
Author:
Robin Hillyard
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.util.AbstractList
 
Field Summary
protected static java.lang.String OP_2ATAN
           
protected static java.lang.String OP_AUDIT
           
protected static java.lang.String OP_CHS
           
protected static java.lang.String OP_COPY
           
protected static java.lang.String OP_DIFFERENCE
           
protected static java.lang.String OP_E
           
protected static java.lang.String OP_EXP
           
protected static java.lang.String OP_INV
           
protected static java.lang.String OP_LOG
           
protected static java.lang.String OP_NOOP
           
protected static java.lang.String OP_PI
           
protected static java.lang.String OP_POWER
           
protected static java.lang.String OP_PRODUCT
           
protected static java.lang.String OP_QUOTIENT
           
protected static java.lang.String OP_SUM
           
protected static java.lang.String OP_SWAP
           
protected static java.lang.String OP_TANHALF
           
 
Fields inherited from class java.util.Vector
capacityIncrement, elementCount, elementData
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
Mill(java.lang.String name)
          Principal constructor for a new Mill.
 
Method Summary
 java.lang.String audit()
          Method to return a detailed (unlabeled) string from an object for debugging purposes.
 void audit(java.io.PrintStream out, java.lang.String label)
          Method to output detailed string from an object for debugging purposes.
 java.lang.String audit(java.lang.String label)
          Method to return a detailed string from an object for debugging purposes.
 int getAdy(java.lang.String operator)
          This method returns the number of presentables needed to popped off the stack for an operation.
 java.lang.String getIdentifier()
          Method to get the specific identifier for an object.
 AlphabeticalMap getOperations()
          Method to get a map which represents the supported functions of this Mill.
 Presentable getValue(java.lang.String key)
          Method to get a memory presentable defined by key.
 void help(java.io.PrintStream output)
          This method outputs help information listing and describing the various operations supported by the mill.
static void main(java.lang.String[] args)
          Main method for testing this class.
 void operate(java.lang.String operator)
          Mutating method to apply the operation operator to the stack.
 void push(Presentable operand)
          Mutating method to push operand onto the top of the Mill stack.
 void setValue(java.lang.String key, Presentable value)
          Method to set a value which can later be retrieved using the name given (using the method defined by the Lookupable interface).
 
Methods inherited from class java.util.Stack
empty, peek, pop, push, search
 
Methods inherited from class java.util.Vector
add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toArray, toString, trimToSize
 
Methods inherited from class java.util.AbstractList
iterator, listIterator, listIterator
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.rubecula.util.Millable
pop, size
 
Methods inherited from interface java.util.List
iterator, listIterator, listIterator
 

Field Detail

OP_SUM

protected static final java.lang.String OP_SUM
See Also:
Constant Field Values

OP_DIFFERENCE

protected static final java.lang.String OP_DIFFERENCE
See Also:
Constant Field Values

OP_PRODUCT

protected static final java.lang.String OP_PRODUCT
See Also:
Constant Field Values

OP_QUOTIENT

protected static final java.lang.String OP_QUOTIENT
See Also:
Constant Field Values

OP_POWER

protected static final java.lang.String OP_POWER
See Also:
Constant Field Values

OP_2ATAN

protected static final java.lang.String OP_2ATAN
See Also:
Constant Field Values

OP_TANHALF

protected static final java.lang.String OP_TANHALF
See Also:
Constant Field Values

OP_EXP

protected static final java.lang.String OP_EXP
See Also:
Constant Field Values

OP_LOG

protected static final java.lang.String OP_LOG
See Also:
Constant Field Values

OP_CHS

protected static final java.lang.String OP_CHS
See Also:
Constant Field Values

OP_INV

protected static final java.lang.String OP_INV
See Also:
Constant Field Values

OP_PI

protected static final java.lang.String OP_PI
See Also:
Constant Field Values

OP_E

protected static final java.lang.String OP_E
See Also:
Constant Field Values

OP_COPY

protected static final java.lang.String OP_COPY
See Also:
Constant Field Values

OP_AUDIT

protected static final java.lang.String OP_AUDIT
See Also:
Constant Field Values

OP_SWAP

protected static final java.lang.String OP_SWAP
See Also:
Constant Field Values

OP_NOOP

protected static final java.lang.String OP_NOOP
See Also:
Constant Field Values
Constructor Detail

Mill

public Mill(java.lang.String name)
Principal constructor for a new Mill.

Parameters:
name - by which this Mill will be referred to.
Method Detail

push

public void push(Presentable operand)
          throws MillException
Mutating method to push operand onto the top of the Mill stack.

Specified by:
push in interface Millable
Parameters:
operand - a presentable object which will displace the former top of the stack to the second position and itself become the top object.
Throws:
MillException - operand not a BaseNumber.

operate

public void operate(java.lang.String operator)
             throws java.util.EmptyStackException,
                    MillException

Mutating method to apply the operation operator to the stack. The number of items required on the stack prior to the operation and the number of items left on the stack afterwards depends upon the operation.

Specified by:
operate in interface Millable
Parameters:
operator - the name for the operation. The following operators ONLY are supported by this Mill:
$name
Retrieve the value indicated by name and push it on the stack. Note that such values are first looked up in the constants list and then, if nothing is found, in the instance list.
<blank>
Pop one value from the stack and push it back.
swap
Reverse the order of the two top stack items.
copy
Pop the top item off the stack and push it back twice.
+
Pop two values from the stack and push their sum.
-
Pop two values from the stack and push their difference.
*
Pop two values from the stack and push their product.
/
Pop two values from the stack and push their quotient.
inv
Pop one value from the stack, take its reciprocal and push the result.
chs
Pop one value from the stack, change its sign and push the result.
log
Pop one value from the stack, take its natural logarithm and push the result.
exp
Pop one value from the stack, raise e to its power and push the result.
2atan
Pop one value from the stack, evaluate twice the arctangent and push the result.
tanhalf
Pop one value from the stack, evaluate the tangent of half the value and push the result.
Throws:
java.util.EmptyStackException
MillException

getValue

public Presentable getValue(java.lang.String key)
Method to get a memory presentable defined by key. The lookup is first made in the class constants and then, if that fails, in this instance's memory. In this way, constants cannot be replaced either deliberately or inadvertently.

Specified by:
getValue in interface Lookupable
Parameters:
key - the name of the memory object, without any preceding "$".
Returns:
the presentable value specified or null.

setValue

public void setValue(java.lang.String key,
                     Presentable value)
              throws MillException
Method to set a value which can later be retrieved using the name given (using the method defined by the Lookupable interface). A memory value can be unset by setting to the null value. Names which appear in the constants list cannot be reused.

Specified by:
setValue in interface Millable
Parameters:
key - the name or key by which this value can be retrieved (without any preceding "$").
value - a Presentable value which is lookupable.
Throws:
MillException - if memory slot name cannot be set to value, because the name is already in use (or an attempt to remove a constant).

getOperations

public AlphabeticalMap getOperations()
Method to get a map which represents the supported functions of this Mill. The keys are the operators (typically abbreviations, all lower case) and the entries are the full mathematical function names (all lower case).

Specified by:
getOperations in interface Millable
Returns:
a map of the abbreviation-function mappings for the supported operations of this mill.

help

public void help(java.io.PrintStream output)
This method outputs help information listing and describing the various operations supported by the mill.

Specified by:
help in interface Millable
Parameters:
output - an output stream.

getIdentifier

public java.lang.String getIdentifier()
Method to get the specific identifier for an object.

Specified by:
getIdentifier in interface Auditable
Returns:
the identifier.

audit

public java.lang.String audit(java.lang.String label)
Method to return a detailed string from an object for debugging purposes.

Specified by:
audit in interface Auditable
Parameters:
label - the label to associate with the data. If label is non-null, a more detailed audit will be returned (this normally occurs only at the top level).
Returns:
the detailed string.

audit

public java.lang.String audit()
Method to return a detailed (unlabeled) string from an object for debugging purposes.

Specified by:
audit in interface Auditable
Returns:
the detailed string.

audit

public void audit(java.io.PrintStream out,
                  java.lang.String label)
Method to output detailed string from an object for debugging purposes.

Specified by:
audit in interface Auditable
Parameters:
out - the output stream.
label - the label to attach to the output (may be null).

getAdy

public int getAdy(java.lang.String operator)
           throws MillException
This method returns the number of presentables needed to popped off the stack for an operation.

Specified by:
getAdy in interface Millable
Parameters:
operator - the name (abbreviation) of the operator.
Returns:
0, 1 or 2 for an-, mon- or dy-adic operators; special operators that don't make a net change in the stack at all return -1.
Throws:
MillException - operator is not supported.

main

public static void main(java.lang.String[] args)
Main method for testing this class.

Parameters:
args - String[] : the command line options.