com.rubecula.spreadsheet
Class QuantityCell

java.lang.Object
  |
  +--com.rubecula.spreadsheet.QuantityCell
All Implemented Interfaces:
Auditable, Dependent, Identifiable, Presentable

public class QuantityCell
extends java.lang.Object
implements Presentable, Dependent, Auditable

Objects of this class have various component fields:

Version:
$Revision: 1.22 $
Author:
Robin Hillyard

Constructor Summary
  QuantityCell(java.lang.String name, Presentable quantity)
          Constructor to create a new QuantityCell with the given name and quantity.
protected QuantityCell(java.lang.String name, Presentable quantity, java.lang.String expression, PresentationFactoryInterface presentationFactory, CellTable cells)
          Primary constructor to create a new QuantityCell with the given name and quantity.
  QuantityCell(java.lang.String name, Presentation presentation)
          Constructor to create a new QuantityCell with the given name and presentation.
  QuantityCell(java.lang.String name, java.lang.String formula, PresentationFactoryInterface presentationFactory, CellTable cells)
          Constructor to create a new QuantityCell with the given name and formula.
 
Method Summary
 void addDependent(Presentable dependent)
          Add a dependent of this cell.
 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 arithmetical object for debugging purposes.
 void calculate()
           
 java.util.Iterator dependents()
          Get an iterator on the dependents.
 AttrMap getAttributes()
          Method to get the attributes for this cell.
 java.lang.String getExpression()
          Method to get the Expression which defines this cell, if any.
 java.lang.String getIdentifier()
          Method to get the specific identifier for an object.
 java.lang.String getName()
          Method to get the name of this cell.
static ExpressionInterface GetParser()
           
(package private)  Presentation getPresentation()
           
 Presentable getQuantity()
           
 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.
 java.lang.Object getValue()
          This method yields the true definition of this cell so that it can be edited in the spreadsheet editor.
 boolean isDirty()
          Method to determine if this cell is in its "dirty" state.
 Presentable makePresentableInstance(Presentation presentation)
          Instance method to convert a Presentation into a Presentable.
 Presentation present(AttrMap attributes)
          Present the cell with the given attributes merged into the cell's own attributes.
 void removeAttribute(java.lang.String attr)
          Method to unset an attribute for this cell.
 void removeDependent(Presentable dependent)
          Remove a dependent from this cell.
 void setAttribute(java.lang.String attr, java.lang.String value)
          Method to set/reset an attribute for this cell.
static void SetDebug(boolean debug)
          Class method to set the debug status for QuantityCell calculations.
 void setDirty()
          Method to set this cell in its "dirty" state.
static void SetJEPParser()
          Method to set up JEP as the expression parser for QuantityCell calculations.
 void setName(java.lang.String name)
          Method to set the name of this cell.
static void SetParser(ExpressionInterface parser)
          Class-mutating method to set the ExpressionParser to be used when encountering an expression which cannot be handled with related package classes.
 void setValue(java.lang.Object value)
          This method sets the value of this cell according to the class of value.
 void setValue(Presentable value)
          Method to set the value of this cell.
 void setValue(Presentation value)
          Method to set the value of this cell.
 void setValue(java.lang.String value)
          Method to set the value (and possibly the attributes) of this cell.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QuantityCell

protected QuantityCell(java.lang.String name,
                       Presentable quantity,
                       java.lang.String expression,
                       PresentationFactoryInterface presentationFactory,
                       CellTable cells)
Primary constructor to create a new QuantityCell with the given name and quantity.
Note however that this constructor should not be called without following up with a setValue() call.

Parameters:
name - the name of this new QuantityCell (of the form A1, B1, etc.)
quantity - the quantity represented by this cell (or null if expression is valid).
expression - the formula for recalculating this quantity (or null).
presentationFactory - the factory from which Presentations will be created from Strings (or null).
cells - a table of QuantityCells used when expression is valid (otherwise may be null).

QuantityCell

public QuantityCell(java.lang.String name,
                    Presentable quantity)
Constructor to create a new QuantityCell with the given name and quantity. The presentation field is set to null.

Parameters:
name - the name of this new QuantityCell (of the form A1, B1, etc.
quantity - the quantity represented by this cell.

QuantityCell

public QuantityCell(java.lang.String name,
                    Presentation presentation)
Constructor to create a new QuantityCell with the given name and presentation.

Parameters:
name - the name of this new QuantityCell (of the form A1, B1, etc.).
presentation - the presentation for this cell.

QuantityCell

public QuantityCell(java.lang.String name,
                    java.lang.String formula,
                    PresentationFactoryInterface presentationFactory,
                    CellTable cells)
Constructor to create a new QuantityCell with the given name and formula.

Parameters:
name - the name of this new QuantityCell (of the form A1, B1, etc.).
formula - the formula represented by this cell.
presentationFactory - the factory from which to create Presentations from strings.
cells - the table of QuantityCells relevant to this formula.
Method Detail

SetDebug

public static void SetDebug(boolean debug)
Class method to set the debug status for QuantityCell calculations.

Parameters:
debug - true or false.

SetParser

public static void SetParser(ExpressionInterface parser)
Class-mutating method to set the ExpressionParser to be used when encountering an expression which cannot be handled with related package classes.

Parameters:
parser - an implementing instance of ExpressionInterface with which to parse expressions in cells.

GetParser

public static ExpressionInterface GetParser()
Returns:
the implementer of ExpressionInterface, else null.

SetJEPParser

public static void SetJEPParser()
Method to set up JEP as the expression parser for QuantityCell calculations. Set debug status of expression parser according to current debug status.


getName

public java.lang.String getName()
Method to get the name of this cell.

Returns:
the name of this cell.

setName

public void setName(java.lang.String name)
Method to set the name of this cell.

Parameters:
name - the name to give this cell.

getType

public java.lang.String getType()
Description copied from interface: Presentable
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 not necessarily the canonical class name.

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

getAttributes

public AttrMap getAttributes()
Method to get the attributes for this cell.

Returns:
#_Attributes

setValue

public void setValue(Presentation value)
Method to set the value of this cell.

Parameters:
value - a presentation representing the value for this cell.

setValue

public void setValue(Presentable value)
Method to set the value of this cell.

Parameters:
value - a presentable instance representing the value for this cell.

setValue

public void setValue(java.lang.String value)
Method to set the value (and possibly the attributes) of this cell.

Parameters:
value - a string representing the value for this cell, optionally followed by a set of comma-separated attributes enclosed in {}. Each of the attributes is of the form name[=value] where the [=value] part is optional.

setValue

public void setValue(java.lang.Object value)
This method sets the value of this cell according to the class of value. Invokes wither setValue(String), setValue(Presentable) or setValue(Presentation), as appropriate.

Parameters:
value - an object which will determine the cell's new value.

getExpression

public java.lang.String getExpression()
Method to get the Expression which defines this cell, if any.

Returns:
the defining expression or null.

getValue

public java.lang.Object getValue()
This method yields the true definition of this cell so that it can be edited in the spreadsheet editor.

Returns:
the expression (if non-null) else the presentable (if non-null) else...
Throws:
java.lang.RuntimeException - if expression and presentable both null.

getQuantity

public Presentable getQuantity()
                        throws PresentableException
Returns:
a presentable which is the value for this cell.
Throws:
PresentableException - if there is a problem calculating the value of this cell.

calculate

public void calculate()
               throws PresentableException
Throws:
PresentableException - if there is a problem calculating the value of this cell.

present

public Presentation present(AttrMap attributes)
Present the cell with the given attributes merged into the cell's own attributes. If "dirty" (presentation is null) or if presentation is currently an error, then update the quantity and present it. The result is stored for future use, and returned.

Specified by:
present in interface Presentable
Parameters:
attributes - an AttrMap list of attributes.
Returns:
an appropriate instance of Presentation.
Throws:
java.lang.UnsupportedOperationException - not implemented.

getPresentation

Presentation getPresentation()
Returns:
the current value of Presentation (may be null).

makePresentableInstance

public Presentable makePresentableInstance(Presentation presentation)
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 - a presentation of a number which you want to convert into a number.
Returns:
a new QuantityCell derived from presentation.

setDirty

public void setDirty()
Method to set this cell in its "dirty" state. That is to say to the presentation is set to null and, if there is a formula, the quantity is set to null too.


isDirty

public boolean isDirty()
Method to determine if this cell is in its "dirty" state. That is to say the presentation is set to null and, if there is a formula, the quantity is set to null too.

Returns:
true if the presentation is null or if both: the expression is non-null and the presentable is null.

addDependent

public void addDependent(Presentable dependent)
Add a dependent of this cell.

Specified by:
addDependent in interface Dependent
Parameters:
dependent - a Presentable object which is dependent on this cell.

removeDependent

public void removeDependent(Presentable dependent)
Remove a dependent from this cell.

Specified by:
removeDependent in interface Dependent
Parameters:
dependent - a Presentable object which is not dependent on this cell.

dependents

public java.util.Iterator dependents()
Get an iterator on the dependents.

Specified by:
dependents in interface Dependent
Returns:
a list of the current dependents in the form of an Iterator.

getIdentifier

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

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

audit

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

Specified by:
audit in interface Auditable
Parameters:
label - String: the label to associate with the data
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).

setAttribute

public void setAttribute(java.lang.String attr,
                         java.lang.String value)
Method to set/reset an attribute for this cell.

Parameters:
attr - the attribute name.
value - the attribute value.

removeAttribute

public void removeAttribute(java.lang.String attr)
Method to unset an attribute for this cell.

Parameters:
attr - the attribute name.