package com.accountingenhancements.formula;
import java.text.ParseException;
import java.util.*;
/*
* FormulaVariableList.java
*
* Created on April 27, 2006, 10:49 AM
*
* Copyright 2006 Lee Lofgern and Accounting Enhancements Inc Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
/**
* Manage a list of FormulaVariables.
* Variables with the highest levels replace variables with lower levels.
* When higher levels are removed, the lower levels by the same name return in scope.
* This list treats all variable names as ignore-case.
*
*
* @author Lee lofgren lofgren_opensource@accountingenhancements.com
* @version 0.1009102006
*/
public class FormulaVariableList {
protected HashMap variableList=new HashMap();
protected ArrayListvariableArray=new ArrayList();
protected int highestLevel=0;
/**
* Creates a new instance of FormulaVariableList
*/
public FormulaVariableList() {
variableList=new HashMap();
}
/**
*
*
* @return the FormulaVariable class that is stored under assigned name, null if not found
*/
public FormulaVariable getVariable(String variableName){
return variableList.get(variableName.toUpperCase());
}
/**
*Add a semi-colon separated list of variables in the order, variable name, equals sign, value, semi-colon, etc...
*@param variables a semi-colon. Example: addVariables("Var1=A+B;Var2=\"Hi There\"");
*@return a stack containing the variables that were added to the variable list.
*@throws ParseException
*/
public FormulaVariableStack addVariables(String variables)throws ParseException{
return addVariables(variables,0);
}
/**
*Add a semi-colon separated list of variables in the order, variable name, equals sign, value, semi-colon, etc...
*@param variables a semi-colon. Example: addVariables("Var1=A+B;Var2=\"Hi There\"",2);
*@param level the volatility of the variable.
*@return a stack containing the variables that were added to the variable list.
*@throws ParseException
*/
public FormulaVariableStack addVariables(String variables, int level)throws ParseException{
int semicolon;
int start;
FormulaVariableStack stack=new FormulaVariableStack();
start=0;
semicolon=0;
if (variables!=null&&variables.length()>0){
while(starthighestLevel)highestLevel=variable.getLevel();
return variable;
} else return null;
}
/**
*Add a value to the variable list.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable. FormulaVariable will automatically try to determine whether this data represents a TYPE_STRING, TYPE_DOUBLE, TYPE_LONG, or TYPE_DATE.
* @param level the level of the variable. When variables share the same name (not case sensitive), the highest level is returned.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, String value, int level){FormulaVariable variable = new FormulaVariable(variableName, value, level);addVariable(variable);return variable;}
/**
*Add a value to the variable list.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable. FormulaVariable will automatically try to determine whether this data represents a TYPE_STRING, TYPE_DOUBLE, TYPE_LONG, or TYPE_DATE.
* @param level the level of the variable. When variables share the same name (not case sensitive), the highest level is returned.
* @param treatUnquotedTextAsFormulas If true then if the variable value has beginning and ending quotes, they will be removed and the variable will be of TYPE_STRING, If unquoted only made up of letters, numbers, and underscores with no spaces, then the variable type will be TYPE_VARIABLE, else the variable type will be TYPE_FORMULA, or TYPE_FUNCTION.
* If treatUnquotedTextAsFormula is false then the variable type will be TYPE_STRING if it is determined not to be TYPE_LONG, TYPE_BOOLEAN, or TYPE_DOUBLE and the value will be left as-is (no quotes removed).
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, String value, int level, boolean treatUnquotedTextAsFormulas){FormulaVariable variable = new FormulaVariable(variableName, value, level, treatUnquotedTextAsFormulas);addVariable(variable);return variable;}
/**
*Add a value to the variable list.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable. FormulaVariable will automatically try to determine whether this data represents a TYPE_STRING, TYPE_DOUBLE, TYPE_LONG, or TYPE_DATE.
* @param scaleIfDouble the scale of a number
* @param level the level of the variable. When variables share the same name (not case sensitive), the highest level is returned.
* @param treatUnquotedTextAsFormulas If true then if the variable value has beginning and ending quotes, they will be removed and the variable will be of TYPE_STRING, If unquoted only made up of letters, numbers, and underscores with no spaces, then the variable type will be TYPE_VARIABLE, else the variable type will be TYPE_FORMULA, or TYPE_FUNCTION.
* If treatUnquotedTextAsFormula is false then the variable type will be TYPE_STRING if it is determined not to be TYPE_LONG, TYPE_BOOLEAN, or TYPE_DOUBLE and the value will be left as-is (no quotes removed).
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, String value, int scaleIfDouble, int level, boolean treatUnquotedTextAsFormulas){FormulaVariable variable = new FormulaVariable(variableName, value, scaleIfDouble, level, treatUnquotedTextAsFormulas);addVariable(variable);return variable;}
/**
*Add a value to the variable list.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param level the level of the variable. When variables share the same name (not case sensitive), the highest level is returned.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, long value, int level){FormulaVariable variable = new FormulaVariable(variableName,value, level);addVariable(variable);return variable;}
/**
*Add a value to the variable list.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param level the level of the variable. When variables share the same name (not case sensitive), the highest level is returned.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, java.util.Date value, int level){FormulaVariable variable = new FormulaVariable(variableName,value, level);addVariable(variable);return variable;}
/**
*Add a value to the variable list.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param scale the scale of a number
* @param level the level of the variable. When variables share the same name (not case sensitive), the highest level is returned.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, double value, int scale, int level){FormulaVariable variable = new FormulaVariable(variableName,value, scale, level); addVariable(variable);return variable;}
/**
*Add a value to the variable list.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param scale the scale of a number
* @param level the level of the variable. When variables share the same name (not case sensitive), the highest level is returned.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, java.math.BigDecimal value, int scale, int level){FormulaVariable variable = new FormulaVariable(variableName,value, scale, level); addVariable(variable);return variable;}
/**
*Add a value to the variable list.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param scale the scale of a number if TYPE_DOUBLE or the sensitivity of a true/false test against a double if TYPE_BOOLEAN.
* @param level the level of the variable. When variables share the same name (not case sensitive), the highest level is returned.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, boolean value, int scale, int level){FormulaVariable variable = new FormulaVariable(variableName,value,scale,level); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, String value){FormulaVariable variable = new FormulaVariable(variableName,value,highestLevel); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param treatUnquotedTextAsFormulas If true then if the variable value has beginning and ending quotes, they will be removed and the variable will be of TYPE_STRING, If unquoted only made up of letters, numbers, and underscores with no spaces, then the variable type will be TYPE_VARIABLE, else the variable type will be TYPE_FORMULA or TYPE_FUNCTION.
* If treatUnquotedTextAsFormula is false then the variable type will be TYPE_STRING if it is determined not to be TYPE_LONG, TYPE_BOOLEAN, or TYPE_DOUBLE and the value will be left as-is (no quotes removed).
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, String value, boolean treatUnquotedTextAsFormulas){FormulaVariable variable = new FormulaVariable(variableName,value,highestLevel, treatUnquotedTextAsFormulas); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, long value){FormulaVariable variable = new FormulaVariable(variableName,value,highestLevel); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, java.util.Date value){FormulaVariable variable = new FormulaVariable(variableName,value,highestLevel); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, double value){FormulaVariable variable = new FormulaVariable(variableName,value,highestLevel); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param scale the scale of a number
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, double value, int scale){FormulaVariable variable = new FormulaVariable(variableName,value,scale, highestLevel); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, java.math.BigDecimal value){FormulaVariable variable = new FormulaVariable(variableName,value,highestLevel); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param scale the scale of a number
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, java.math.BigDecimal value, int scale){FormulaVariable variable = new FormulaVariable(variableName,value,scale, highestLevel); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, boolean value){FormulaVariable variable = new FormulaVariable(variableName,value,highestLevel); addVariable(variable);return variable;}
/**
*Add a value to the variable list. The level assigned to the newly created variable will be the highest active level.
* @param variableName the name to be assigned to the variable that is to be created.
* @param value the value that is assigned to the new variable.
* @param scale the scale of a number if TYPE_DOUBLE or the sensitivity of a true/false test against a double if TYPE_BOOLEAN.
*@return variable the variable for chaining purposes.
*/
public FormulaVariable addValue(String variableName, boolean value, int scale){FormulaVariable variable = new FormulaVariable(variableName,value,scale, highestLevel); addVariable(variable);return variable;}
/**
* @param variableName the name of the variable whose value is to be returned. Note: The name is not case sensitive.
* @return the value assigned to the variable who's name matches the requested name.
*/
public String getString(String variableName){
String value=null;
FormulaVariable variable;
variable=variableList.get(variableName.toUpperCase());
if(variable!=null){value=variable.getString();}
return value;
}
/**
* @param variableName the name of the variable whose value is to be returned. Note: The name is not case sensitive.
* @return the value assigned to the variable who's name matches the requested name.
*/
public Long getLong(String variableName){
Long value=null;
FormulaVariable variable;
variable=variableList.get(variableName.toUpperCase());
if(variable!=null){value=variable.getLong();}
return value;
}
/**
* @param variableName the name of the variable whose value is to be returned. Note: The name is not case sensitive.
* @return the value assigned to the variable who's name matches the requested name.
*/
public Double getDouble(String variableName){
Double value=null;
FormulaVariable variable;
variable=variableList.get(variableName.toUpperCase());
if(variable!=null){value=variable.getDouble();}
return value;
}
/**
* @param variableName the name of the variable whose value is to be returned. Note: The name is not case sensitive.
* @return the value assigned to the variable who's name matches the requested name.
*/
public Boolean getBoolean(String variableName){
Boolean value=null;
FormulaVariable variable;
variable=variableList.get(variableName.toUpperCase());
if(variable!=null){value=variable.getBoolean();}
return value;
}
/**
* @param variableName the name of the variable whose value is to be returned. Note: The name is not case sensitive.
* @return the value assigned to the variable who's name matches the requested name.
*/
public java.math.BigDecimal getBigDecimal(String variableName){
java.math.BigDecimal value=null;
FormulaVariable variable;
variable=variableList.get(variableName.toUpperCase());
if(variable!=null){value=variable.getBigDecimal();}
return value;
}
/**
* @param variableName the name of the variable whose value is to be returned. Note: The name is not case sensitive.
* @return the value assigned to the variable who's name matches the requested name.
* @throws ParseException if variable value can't be converted to a Date.
*/
public java.util.Date getDate(String variableName) throws ParseException{
java.util.Date value=null;
FormulaVariable variable;
variable=variableList.get(variableName.toUpperCase());
if(variable!=null){value=variable.getDate();}
return value;
}
/**
* @param variableName the name of the variable whose value is to be returned. Note: The name is not case sensitive.
* @return the variable type, String, Integer, Double, or Boolean.
*/
public int getVariableType(String variableName){
int value=-1;
FormulaVariable variable;
variable=variableList.get(variableName.toUpperCase());
if(variable!=null){value=variable.getVariableType();}
return value;
}
/**
*@param variable remove the specified variable.
*/
public void removeVariable(FormulaVariable variable){
variableArray.remove(variable);
rebuildVariableList();
}
/**
*@param variableName the name of the variable to be removed. Not case sensitive.
* If more than one variable by the same name, then the one with the highest level is remove.
*/
public void removeVariable(String variableName){
FormulaVariable variable;
variable=variableList.get(variableName.toUpperCase());
if(variable!=null)removeVariable(variable);
}
/**
*@param level the level to be removed from the variable list. Note: All higher levels are removed as well.
* Removing level 0 clears everything unless you've been using negative numbers.
*/
public void removeLevel(int level){
int index;
index=variableArray.size()-1;
while(index>=0){
if (variableArray.get(index).getLevel()>=level){variableArray.remove(index);}
index--;
}
rebuildVariableList();
}
/**
*Rebuild the HashMap to make sure that the variables returned have the highest levels when names are shared.
* This method is executed by the various remove methods.
*/
protected void rebuildVariableList(){
FormulaVariable workVar;
variableList.clear();
highestLevel=0;
for(int i=0;i