/* * FormulaFunction_IIF.java * * Created on April 29, 2006, 10:42 PM * * 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. */ package com.accountingenhancements.formula; import com.accountingenhancements.common.SupportParameters; import java.util.*; import java.text.*; /** * * @author Lee lofgren lofgren_opensource@accountingenhancements.com * @version 0.1009102006 */ public class FormulaFunction_IIF extends FormulaFunction{ protected static String[][] requiredArguments = {{"ARG1: Test Argument","TYPE_BOOLEAN"},{"ARG2: True Argument","TYPE_?"},{"ARG3: False Argument","TYPE_?"}}; protected static String[] returnValueDescription = {"Either ARG2 or ARG3 depending on boolean result of ARG1.","TYPE_?"}; /** *Create a new FormulaFunction with an already defined functionArgumentStack. *@param functionArgumentStack a list of FormulaVariables that are needed by the function. */ public FormulaFunction_IIF(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 *Example: If the function is IIF(A *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_IIF(FormulaVariable functionVariable) throws ParseException{ super(functionVariable); } /** * IIF Function * Based on the truth of a test, a FormulaVariable is returned by following the result of the true branch or the false branch. Ex: IIF(testVariable!=0,"Not Zero","Zero"); * This class expects ARG1 to be the test, ARG2 to be the true result, and ARG3 to be the false result. * @param variableList the variable list used when solving for various variables. * @param iteration used to prevent infinite loops. * @param 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. * @param functionList list of functions that may be necessary when solving FormulaVariables on the functionArgumentStack. * @param resolveEverythingAboveLevel reflects level at which TYPE_VARIABLE, TYPE_FUNCTION, and TYPE_FORMULA variables need to be re-solved. * @return FormulaVariable containing the result * @throws 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.. */ public FormulaVariable function(FormulaVariableList variableList, int iteration, SupportParameters supportParameters, FormulaFunctionList functionList, int resolveEverythingAboveLevel) throws java.text.ParseException, java.lang.ArithmeticException, ClassNotFoundException { FormulaVariable returnValue=null; if(functionArgumentStack==null)throw new java.lang.ArithmeticException("functionArgumentStack is null"); FormulaVariable testValue = functionArgumentStack.get("ARG1"); FormulaVariable trueValue = functionArgumentStack.get("ARG2"); FormulaVariable falseValue = functionArgumentStack.get("ARG3"); if(testValue==null)throw new java.lang.ArithmeticException("Missing the test argument for this IIF(testArg, trueArg, falseArg) function"); if (testValue.solve(variableList, iteration, supportParameters, functionList, resolveEverythingAboveLevel).getBoolean()==false){ if(falseValue==null)throw new java.lang.ArithmeticException("Missing false argument for this IIF(testArg,trueArg,falseArg) function"); returnValue=falseValue.solve(variableList, iteration, supportParameters, functionList, resolveEverythingAboveLevel); } else { if(trueValue==null)throw new java.lang.ArithmeticException("Missing false argument for this IIF(testArg,trueArg,falseArg) function"); returnValue=trueValue.solve(variableList, iteration, supportParameters, functionList, resolveEverythingAboveLevel); } return returnValue; } /** *@return the name of this function */ public static final String getName() { return "IIF"; } /** *@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(){ return requiredArguments; } /** *@return String[] description of the return value as well as it's data type */ public static String[] getReturnValueDescription(){ return returnValueDescription; } }