com.rubecula.jquantity
Class Expression

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractList
          extended byjava.util.Vector
              extended bycom.rubecula.jquantity.Expression
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, java.util.List, Presentable, java.util.RandomAccess, Replaceable, java.io.Serializable

public class Expression
extends java.util.Vector
implements Replaceable

Class to manage a mathematical expression. It is the Presentable counterpart to SymbolicPresentation. Elements, the order of which is important, are either of:


Note that although this class is defined in terms of Replaceable (which extends Presentableand java.util.Vector only), nevertheless it uses numerical classes such as BaseNumber and the abstract class of Item. This doesn't seem quite right. // TODO The calculations which use operators on Items and BaseNumbers ought to be defined somewhere else so that this class could be more "pure".

Since:
V_0_3
Version:
$Revision: 1.14 $
Author:
Robin Hillyard
See Also:
Serialized Form

Nested Class Summary
(package private) static class Expression.ExpressionException
          Static class to define an exception encountered when parsing a formula.
 
Nested classes inherited from class java.util.AbstractList
 
Field Summary
private static Lookupable $CONSTANTS
          This Lookupable object is where "constants" that may be required by any expression can be stored.
 
Fields inherited from class java.util.Vector
capacityIncrement, elementCount, elementData
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
Expression()
          Constructor for an empty formula.
Expression(Presentable presentable)
          Constructor for an "anadic" formula of all strings.
 
Method Summary
static Lookupable GetConstants()
           
 java.lang.String getType()
          This method is primarily for development purposes: it yields the name of the class of the presentable object or, in some cases, the class of the presentable object indirectly referenced.
 Presentable getValue()
          Method to evaluate this expression.
 Presentable getValue(java.lang.Object ignored)
          This method takes this Expression and returns its logical equivalent, i.e. it evaluates the expression.
private static Presentable LookupConstant(java.lang.String s)
           
static void main(java.lang.String[] args)
          Main method for testing this class.
 Presentable makePresentableInstance(Presentation presentation)
          Instance method to convert a Presentation into a Presentable.
private static Item OpAnadic(Operator op)
           
private static Item OpDyadicComplex(Item current, Operator currentOp, Item number)
           
private static Item OpDyadicNonComplex(Item current, Operator currentOp, BaseNumber number)
          Note that the order of the operands is the reverse of the true order.
private static Item OpMonadic(Operator op, Item number)
           
static Presentable Parse(Presentation presentation)
          Create a Rational from a Presentation and return it as a Presentable.
static Expression Parse(SymbolicPresentation presentation, java.lang.String identifier)
          Translates the SymbolicPresentation representation of an expression into an instance of Expression.
 Presentation present()
          Convert this Presentable object into a Presentation with default attributes.
 Presentation present(AttrMap attributes)
          Convert this Presentable object into a Presentation, according to the set of attributes provided.
static void SetConstants(Lookupable constants)
           
protected static void testExpression(java.io.PrintStream out, SymbolicPresentation sp, java.lang.String label)
           
 
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 java.util.List
iterator, listIterator, listIterator
 

Field Detail

$CONSTANTS

private static Lookupable $CONSTANTS
This Lookupable object is where "constants" that may be required by any expression can be stored.

Constructor Detail

Expression

public Expression()
Constructor for an empty formula.


Expression

public Expression(Presentable presentable)
Constructor for an "anadic" formula of all strings.

Parameters:
presentable - the presentable to form this singleton expression.
Method Detail

Parse

public static Expression Parse(SymbolicPresentation presentation,
                               java.lang.String identifier)
                        throws PresentationException
Translates the SymbolicPresentation representation of an expression into an instance of Expression.

Parameters:
presentation - SymbolicPresentation representation to interpret.
identifier - A string with which to identify this Rational.
Returns:
Expression : a new object corresponding to presentation.
Throws:
PresentationException - if the presentation cannot be parsed as an Expression.

LookupConstant

private static Presentable LookupConstant(java.lang.String s)
                                   throws PresentationException,
                                          PresentableException
Throws:
PresentationException
PresentableException

getValue

public Presentable getValue()
                     throws PresentableException
Method to evaluate this expression.

Returns:
a Presentable value corresponding to the current value of this expression.
Throws:
PresentableException - name not be found.

getValue

public Presentable getValue(java.lang.Object ignored)
                     throws PresentableException
This method takes this Expression and returns its logical equivalent, i.e. it evaluates the expression. Note that binding is entirely position, and that the implicit operator is times.

Specified by:
getValue in interface Replaceable
Parameters:
ignored - Not used.
Returns:
a Presentable value corresponding to the value of this expression.
Throws:
PresentableException

present

public Presentation present(AttrMap attributes)
                     throws PresentationException
Convert this Presentable object into a Presentation, according to the set of attributes provided.

Specified by:
present in interface Presentable
Parameters:
attributes - a map of desired attributes for this presentation.
Returns:
an appropriate instance of Presentation.
Throws:
PresentationException - if formula cannot be presented with given attributes.

present

public Presentation present()
                     throws PresentationException
Convert this Presentable object into a Presentation with default attributes.

Returns:
an appropriate instance of Presentation.
Throws:
PresentationException - if formula cannot be presented with given attributes.

getType

public java.lang.String getType()
This method is primarily for development purposes: it yields the name of the class of the presentable object or, in some cases, the class of the presentable object indirectly referenced. The text returned is canonical class name (subclasses may choose to override this).

Specified by:
getType in interface Presentable
Returns:
a representation of the effective class of this object.

makePresentableInstance

public Presentable makePresentableInstance(Presentation presentation)
                                    throws PresentationException
Instance method to convert a Presentation into a Presentable. Ideally, this would be a class method, but such a method cannot be defined in an interface. Nor can it be defined in an abstract class with the intention of overriding it in a subclass. Polymorphism just doesn't work that way.
Therefore, the present value (as in fields) of the instance is essentially ignored - only the methods are important.

Specified by:
makePresentableInstance in interface Presentable
Parameters:
presentation - of a number to be converted into a Presentable number.
Returns:
a number in the form of a Presentable.
Throws:
PresentationException - if presentation cannot be parsed appropriately.

Parse

public static Presentable Parse(Presentation presentation)
                         throws PresentationException
Create a Rational from a Presentation and return it as a Presentable.

Parameters:
presentation - A presentation which should be parsed to form a Presentable.
Returns:
A Rational which corresponds to the presentation.
Throws:
PresentationException - if the presentation cannot be parsed as a Presentable object.

OpAnadic

private static Item OpAnadic(Operator op)
                      throws PresentableException
Throws:
PresentableException

OpMonadic

private static Item OpMonadic(Operator op,
                              Item number)
                       throws PresentableException
Throws:
PresentableException

OpDyadicNonComplex

private static Item OpDyadicNonComplex(Item current,
                                       Operator currentOp,
                                       BaseNumber number)
                                throws PresentableException
Note that the order of the operands is the reverse of the true order.

Parameters:
current - the "current" value - generally the accumulated result of the expression thus far.
currentOp - the "current" operator.
number - the new number to be combined with the current value
Returns:
the result of operating on current and number
Throws:
PresentableException

OpDyadicComplex

private static Item OpDyadicComplex(Item current,
                                    Operator currentOp,
                                    Item number)
                             throws PresentableException
Throws:
PresentableException

testExpression

protected static void testExpression(java.io.PrintStream out,
                                     SymbolicPresentation sp,
                                     java.lang.String label)

main

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

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

GetConstants

public static Lookupable GetConstants()
Returns:
Returns the $CONSTANTS.

SetConstants

public static void SetConstants(Lookupable constants)
Parameters:
constants - The Lookupable constants to be used as our "constants" definition to set.
See Also:
$CONSTANTS