|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.accountingenhancements.formula.FormulaFunction
public class FormulaFunction
A template for defined function. This class isn't used directly, all formula functions should extend this class.
For examples of how to define a function see FormulaFunction_Mid
and FormulaFunction_IIF
.
The usual constructor used is the FormulaFunction(FormulaVariable functionVariable) where functionVariable is a FormulaVariable of type TYPE_FUNCTION.
The (String) and (String, int) and (FormulaVariable) constructors use parseCommaDelimitedFunctionFields(String, int) to create the argument stack.
Arguments are handed to these functions as the contents of a stack. The stack can be created through the use of the static parseCommaDelimitedFunctionFields
method.
The following is a convenient cut-and-paste for the base code for creating function code. Replace all code
following your package statement with the following code. Perform a global replace on FormulaFunction_ with
FormulaFunction_YourNewClassName then fix the FormulaFunction_YourNewClassNameYourNewClassName.java in the comment
line at the top of this file.
import com.accountingenhancements.common.SupportParameters;
import com.accountingenhancements.formula.*;
import java.text.ParseException;
import java.util.HashMap;
/**
*
*
*@author ????
*/
public class FormulaFunction_ extends FormulaFunction{
/**
*Create a new FormulaFunction with an already defined functionArgumentStack.
*@param functionArgumentStack a list of FormulaVariable
s that are needed by the function.
*/
public FormulaFunction_(FormulaVariableStack functionArgumentStack){
super(functionArgumentStack);
}
/**
*Create a new FormulaFunction with a comma-delimited string representing the arguments needed by the function
*@param functionArgumentString the string representing the functions arguments. The outer parentheses must be removed.
*Example: If the function is IIF(A
*@throws ParseException if arguments can't be parsed using parseCommaDelimitedFunctionFields(String,int)
*/
public FormulaFunction_(String functionArgumentString) throws ParseException{
super(functionArgumentString);
}
/**
*Create a new FormulaFunction with a comma-delimited string representing the arguments needed by the function
*@param functionArgumentString the string representing the functions arguments. The outer parentheses must be removed.
*Example: If the function is IIF(A
*@param level the level applied to the creation of FormulaVariables for the arguments.
*@throws ParseException if arguments can't be parsed using parseCommaDelimitedFunctionFields(String,int)
*/
public FormulaFunction_(String functionArgumentString, int level) throws ParseException{
super(functionArgumentString,level);
}
/**
*Create a new FormulaFunction from the functionVariable (A FormulaVariable of TYPE_FUNCTION).
*This is the most common constructor used.
*@param functionVariable a FormulaVariable of TYPE_FUNCTION
*@throws ParseException if arguments can't be parsed using parseCommaDelimitedFunctionFields(String,int)
*/
public FormulaFunction_(FormulaVariable functionVariable) throws ParseException{
super(functionVariable);
}
/**
*@return the name of this function
*/
public static String getName(){
return "";
}
/**
*@return String[][] of needed arguments and their dataTypes which as {{"Test Argument ARG1","TYPE_BOOLEAN"},{"True Argument ARG2","TYPE_?"},{"False Argument ARG3","TYPE_?"}}
*If handing this routine a FormulaVariableStack functionArgumentStack then the FormulaVariable arguments should be named, ARG1, ARG2, etc...
*/
public static String[][] getRequiredArguments(){
String[][] result={};
return result;
}
/**
*@return String[] description of return value and it's data type. {"Upper case result","TYPE_STRING"}
*/
public static String[] getReturnValueDescription(){
String[] result={};
return result;
}
/**
*Portion of code that actually solves the function.
*/
protected FormulaVariable solve(FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel) throws java.text.ParseException, java.lang.ArithmeticException, ClassNotFoundException{
FormulaVariable solution=null;
String resultString="";
int highestLevelUsed=0;
boolean treatUnquotedTextAsFormula=true;
FormulaVariable Arg_1=null;
Arg_1=functionArgumentStack.get("ARG1");
if(Arg_1==null)throw new java.lang.ArithmeticException("ARG1 is missing from functionArgumentStack");
Arg_1=Arg_1.solve(variableList,iteration,supportParameters,functionList,resolveEverythingAboveLevel);
if(Arg_1==null||Arg_1.isNull()==true||Arg_1.getString().length()==0)throw new java.lang.ArithmeticException("ARG1 is invalid");
if(highestLevelUsed < Arg_1.getHighestLevel())highestLevelUsed=Arg_1.getHighestLevel();
solution=new FormulaVariable("",resultString,highestLevelUsed,treatUnquotedTextAsFormula);
return solution;
}
}
Field Summary | |
---|---|
protected FormulaVariableStack |
functionArgumentStack
The stack containing the function's arguments. |
Constructor Summary | |
---|---|
FormulaFunction(FormulaVariable functionVariable)
Create a new FormulaFunction from the functionVariable (A FormulaVariable of TYPE_FUNCTION). This is the most common constructor used. |
|
FormulaFunction(FormulaVariableStack functionArgumentStack)
Create a new FormulaFunction with an already defined functionArgumentStack. |
|
FormulaFunction(java.lang.String functionArgumentString)
Create a new FormulaFunction with a comma-delimited string representing the arguments needed by the function |
|
FormulaFunction(java.lang.String functionArgumentString,
int level)
Create a new FormulaFunction with a comma-delimited string representing the arguments needed by the function |
Method Summary | |
---|---|
FormulaVariable |
function(FormulaVariableList variableList,
int iteration,
SupportParameters supportParameters,
FormulaFunctionList functionList,
int resolveEverythingAboveLevel)
Programmer defined function. |
FormulaVariable |
function(FormulaVariableStack functionArgumentStack,
FormulaVariableList variableList,
int iteration,
SupportParameters supportParameters,
FormulaFunctionList functionList,
int resolveEverythingAboveLevel)
Programmer defined function. This function replaces the funcionArgumentStack and calls function(FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel) so you can leave this function alone, but the other function needs to be overriden. |
FormulaVariableStack |
getFunctionArgumentStack()
|
protected static FormulaVariableStack |
getFunctionArgumentStackFromFunctionVariable(FormulaVariable functionVariable)
|
static java.lang.String |
getName()
|
static java.lang.String[][] |
getRequiredArguments()
|
static java.lang.String[][] |
getRequiredFormulaVariables()
|
static java.lang.String[][] |
getRequiredSupportParameters()
|
static java.lang.String[] |
getReturnValueDescription()
|
protected static FormulaVariableStack |
parseCommaDelimitedFunctionFields(java.lang.String formulaSegment,
int level)
Break the contents of a formula that are between parentheses into their constituent fields. Note: Examples are as seen in code, so "\\\"how\\\"" creates the string \"how\" and Hello\\, creates the string Hello\, Ex: "IIF(A formulaSegment = "A returnArray.get(0) == "A returnArray.get(1) == "\"Hello there, \\\"how\\\" are you\"" returnArray.get(2) == "1.34" Escaping a comma will cause the comma to be ignored. Ex: "Hello\\, how are you,2" returnArray.get(0) == "Hello\\, how are you" returnArray.get(1) == "2" |
void |
setFunctionArgumentStack(FormulaVariableStack functionArgumentStack)
|
protected FormulaVariable |
solve(FormulaVariableList variableList,
int iteration,
SupportParameters supportParameters,
FormulaFunctionList functionList,
int resolveEverythingAboveLevel)
Actual method to be overridden. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected FormulaVariableStack functionArgumentStack
Constructor Detail |
---|
public FormulaFunction(FormulaVariableStack functionArgumentStack)
functionArgumentStack
- a list of FormulaVariable
s that are needed by the function.public FormulaFunction(java.lang.String functionArgumentString) throws java.text.ParseException
functionArgumentString
- the string representing the functions arguments. The outer parentheses must be removed.java.text.ParseException
- if arguments can't be parsed using parseCommaDelimitedFunctionFields(String,int)public FormulaFunction(java.lang.String functionArgumentString, int level) throws java.text.ParseException
functionArgumentString
- the string representing the functions arguments. The outer parentheses must be removed.level
- the level applied to the creation of FormulaVariables for the arguments.
java.text.ParseException
- if arguments can't be parsed using parseCommaDelimitedFunctionFields(String,int)public FormulaFunction(FormulaVariable functionVariable) throws java.text.ParseException
functionVariable
- a FormulaVariable of TYPE_FUNCTION
java.text.ParseException
- if arguments can't be parsed using parseCommaDelimitedFunctionFields(String,int)Method Detail |
---|
public FormulaVariable function(FormulaVariableStack functionArgumentStack, FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel) throws java.text.ParseException, java.lang.ArithmeticException, java.lang.ClassNotFoundException
functionArgumentStack
- list of variables needed by function to return result.variableList
- the variable list used when solving for various variables.iteration
- used to prevent infinite loops.supportParameters
- a list of objects that this method may need to solve itself. One example is if this were an SQL function, one of the objects could be an sql connection.functionList
- list of functions that may be necessary when solving FormulaVariables on the functionArgumentStack.resolveEverythingAboveLevel
- reflects level at which TYPE_VARIABLE, TYPE_FUNCTION, and TYPE_FORMULA variables need to be re-solved.
NoClassFoundException
- if solving the arguments requires access to an undefined function.
ArithmeticException,
- ParseException as handed through from FormulaVariableStack and if caused by various solutions in certain
functions. SQL functions and the like will have to throw ArithmeticExceptions since we are faily generic here.
If you add your own exceptions in your functions, note that it is up to you to support the exception if it fails when being
solved in the FormulaVariableStack because it will be thrown as an unexpected exception. You are better off using the Arithmetic Exception
in your defined function and setting the return message such that your external code can react appropriately to the error..
java.text.ParseException
java.lang.ArithmeticException
java.lang.ClassNotFoundException
public FormulaVariable function(FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel) throws java.text.ParseException, java.lang.ArithmeticException, java.lang.ClassNotFoundException
variableList
- the variable list used when solving for various variables.iteration
- used to prevent infinite loops.supportParameters
- a list of objects that this method may need to solve itself. One example is if this were an SQL function, one of the objects could be an sql connection.functionList
- list of functions that may be necessary when solving FormulaVariables on the functionArgumentStack.resolveEverythingAboveLevel
- reflects level at which TYPE_VARIABLE, TYPE_FUNCTION, and TYPE_FORMULA variables need to be re-solved.
java.lang.ArithmeticException
- or ParseException as handed through from FormulaVariableStack and if caused by various solutions in certain
functions. SQL functions and the like will have to throw ArithmeticExceptions since we are faily generic here.
If you add your own exceptions in your functions, note that it is up to you to support the exception if it fails when being
solved in the FormulaVariableStack because it will be thrown as an unexpected exception. You are better off using the Arithmetic Exception
in your defined function and setting the return message such that your external code can react appropriately to the error..
java.text.ParseException
java.lang.ClassNotFoundException
public static java.lang.String getName()
public static java.lang.String[][] getRequiredSupportParameters()
public static java.lang.String[][] getRequiredFormulaVariables()
public static java.lang.String[][] getRequiredArguments()
public FormulaVariableStack getFunctionArgumentStack()
public void setFunctionArgumentStack(FormulaVariableStack functionArgumentStack)
functionArgumentStack
- set the new functionArgumentStack. The functionArgumentStack is usually set once, then this class is created.public static java.lang.String[] getReturnValueDescription()
protected static FormulaVariableStack getFunctionArgumentStackFromFunctionVariable(FormulaVariable functionVariable) throws java.lang.ArithmeticException, java.text.ParseException
functionVariable
- create a functionArgumentStack from the data contained in a FormulaVariable of TYPE_FUNCTION.
ArithmeticException,
- ParseException
java.lang.ArithmeticException
java.text.ParseException
protected static FormulaVariableStack parseCommaDelimitedFunctionFields(java.lang.String formulaSegment, int level) throws java.text.ParseException, java.lang.ArithmeticException
formulaSegment
- the contents between parentheses in a formula.level
- the level at which the variables in the newly created stack should be stamped.
Usually level zero unless you have record specific information in the formulaSegment.
Ideally, variables should be used when referencing record specific data so that the function
only recalculates the result if the resolveEverythingAboveLevel value falls below the
highestLevelInSolution recorded in this new stack.java.lang.ArithmeticException
- or ParseException as handed through from FormulaVariableStack.
java.text.ParseException
protected FormulaVariable solve(FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel) throws java.text.ParseException, java.lang.ArithmeticException, java.lang.ClassNotFoundException
java.text.ParseException
java.lang.ArithmeticException
java.lang.ClassNotFoundException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |