com.accountingenhancements.formula
Class FormulaVariableStack

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<FormulaVariable>
              extended by com.accountingenhancements.formula.FormulaVariableStack
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<FormulaVariable>, java.util.Collection<FormulaVariable>, java.util.List<FormulaVariable>, java.util.RandomAccess

public class FormulaVariableStack
extends java.util.ArrayList<FormulaVariable>
implements java.lang.Cloneable

An ordered list of FormulaVariables that represents a parsed Formula.
The formula is broken down into components separated by operators. The resulting stack is then processed first moving right until operator a leading operator has a greater precedence than the previous operator. Then the values are resolved and the math is solved working back toward the beginning of the stack until either the beginning of the stack or an operator of qreater precedence is encountered. Then the stack is scanned forward again. The stack should alternate between Value type FormulaVariables and Operator type FormulaVariables.

See Also:
Serialized Form

Field Summary
protected  java.lang.String formula
          The formula that was handed into this class.
protected  int highestLevelInSolution
          If this stack has been resolved then this is the highest level variable used in the solution.
protected  int lowestScale
           
protected  FormulaVariable result
          This is the result when the stack resolved.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
FormulaVariableStack()
          Creates a new instance of FormulaVariableStack
FormulaVariableStack(java.lang.String formula)
          Creates a new instance of FormulaVariableStack
 
Method Summary
 FormulaVariableStack clone()
           
 FormulaVariable get(java.lang.String formulaVariableName)
           
 void parseFormula(java.lang.String formula)
           
 void purgeResultsInStackGreaterThanSpecifiedLevel(int specifiedLevel)
          Go through entries in the stack and purge the results of any functions, formulas, or variable references, that used variables with a level higher than the specified level.
 void reparseFormula()
          Reparse the formula that was last processed.
 FormulaVariable solve(FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList)
          This solves the supplied formula and returns the result.
 FormulaVariable solve(FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel)
           
static FormulaVariable solveRangeOfValuesSeparatedBySameOperator(java.util.ArrayList<FormulaVariable> stack, int startPointer, int endPointer, FormulaVariableList variableList, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel)
           
 
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, remove, removeRange, set, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
 

Field Detail

formula

protected java.lang.String formula
The formula that was handed into this class. reParse() reprocesses this formula


result

protected FormulaVariable result
This is the result when the stack resolved.


highestLevelInSolution

protected int highestLevelInSolution
If this stack has been resolved then this is the highest level variable used in the solution. This is used when resolving, to evaluate whether the solution should be recalculated.


lowestScale

protected int lowestScale
Constructor Detail

FormulaVariableStack

public FormulaVariableStack()
Creates a new instance of FormulaVariableStack


FormulaVariableStack

public FormulaVariableStack(java.lang.String formula)
                     throws java.text.ParseException
Creates a new instance of FormulaVariableStack

Parameters:
formula - the formula string that needs to be solved. This will be handed to FormulaVariableStack.parseFormula().
Throws:
java.text.ParseException
Method Detail

parseFormula

public void parseFormula(java.lang.String formula)
                  throws java.text.ParseException
Parameters:
formula - the formula to be parsed. The old formula (if one exists) and old stack are cleared out.
Throws:
java.text.ParseException

reparseFormula

public void reparseFormula()
                    throws java.text.ParseException
Reparse the formula that was last processed.

Throws:
java.text.ParseException

clone

public FormulaVariableStack clone()
Overrides:
clone in class java.util.ArrayList<FormulaVariable>
Returns:
clone of object. All variables are cloned as well. I suggest processing clone of original if a formula is going to be processed over and over to avoid reparsing.

solve

public FormulaVariable solve(FormulaVariableList variableList,
                             int iteration,
                             SupportParameters supportParameters,
                             FormulaFunctionList functionList)
                      throws java.text.ParseException,
                             java.lang.ClassNotFoundException
This solves the supplied formula and returns the result. The starting level is -1 so the calculation should start from scratch without using any old values unless you have used levels in a FormulaVariable that is below zero.

Returns:
solution to stack.
Throws:
java.lang.ClassNotFoundException - if part of solution requires solving a function that hasn't been added to the functionList
java.text.ParseException

solve

public FormulaVariable solve(FormulaVariableList variableList,
                             int iteration,
                             SupportParameters supportParameters,
                             FormulaFunctionList functionList,
                             int resolveEverythingAboveLevel)
                      throws java.lang.ArithmeticException,
                             java.text.ParseException,
                             java.lang.ClassNotFoundException
Parameters:
resolveEverythingAboveLevel - resolves any solutions that incorporates variables with levels above this level. This saves time if a stack doesn't need to be resolved.
Returns:
solution to stack.
Throws:
java.lang.ClassNotFoundException - if part of solution requires solving a function that hasn't been added to the functionList
java.lang.ArithmeticException
java.text.ParseException

solveRangeOfValuesSeparatedBySameOperator

public static FormulaVariable solveRangeOfValuesSeparatedBySameOperator(java.util.ArrayList<FormulaVariable> stack,
                                                                        int startPointer,
                                                                        int endPointer,
                                                                        FormulaVariableList variableList,
                                                                        SupportParameters supportParameters,
                                                                        FormulaFunctionList functionList,
                                                                        int resolveEverythingAboveLevel)
                                                                 throws java.lang.ArithmeticException,
                                                                        java.text.ParseException,
                                                                        java.lang.ClassNotFoundException
Throws:
java.lang.ArithmeticException
java.text.ParseException
java.lang.ClassNotFoundException

get

public FormulaVariable get(java.lang.String formulaVariableName)

purgeResultsInStackGreaterThanSpecifiedLevel

public void purgeResultsInStackGreaterThanSpecifiedLevel(int specifiedLevel)
Go through entries in the stack and purge the results of any functions, formulas, or variable references, that used variables with a level higher than the specified level. go down into the stacks of variables that have their own stacks.
Deletes formulas whose levels are above the specified level and, in the existing formula's whose highestLevelInSolution is greater than the specified level, forces the variables to be re-solved during the next solve regardless of the solve parameter's resolveEverythingAboveLevel value. It is important to use this when a lower level value has changed and you wouldn't have run across these re-calculations during the lower level process. An example would be, while processing deduction codes, and employee number changed. The deduction code formulas are performed at a level 2 and the employee data is performed at a level 1. Once the level 1 stuff was processed, the level two routines wouldn't know that a level 1 value has changed and would continue to use old values.

Parameters:
specifiedLevel - the level above which all results are to be purged.