/* * JMEP - Java Mathematical Expression Parser. * Copyright (C) 1999 Jo Desmet * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * You can contact the Original submitter of this library by * email at: Jo_Desmet@yahoo.com. * */ package com.iabcinc.jmep; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.iabcinc.jmep.hooks.Constant; import com.iabcinc.jmep.hooks.DefaultsCB; import com.iabcinc.jmep.hooks.Function; import com.iabcinc.jmep.hooks.Unit; import com.iabcinc.jmep.hooks.Variable; /** * The container for user defined functions, variables and units. Every * class that needs to use the Expression class should either contain or * subclass the Environment class. Note that this class is not thread * safe. * @author Jo Desmet * @version 0.1.1.release * @see com.iabcinc.jmep.Expression */ public class Environment { private HashMap<String,Variable> oVariables; private HashMap<String,Function> oFunctions; private HashMap<String,Unit> oUnits; private ArrayList oDefaults; /** * Allocates the Expression Environment. */ public Environment() { oVariables = new HashMap(); oFunctions = new HashMap(); oUnits = new HashMap(); oDefaults = new ArrayList(); } /** * Adds a labeled String constant to the environment. * @param sName the label attached to the constant. * @param sValue the string value of the labeled constant. */ public void addConstant(String sName,final String sValue) { oVariables.put(sName,new Constant(sValue)); } /** * Adds a labeled Double constant to the environment. * @param sName the label attached to the constant. * @param fValue the double value of the labeled constant. */ public void addConstant(String sName,final double fValue) { oVariables.put(sName,new Constant(new Float(fValue))); } /** * Adds a labeled int constant to the environment. * @param sName the label attached to the constant. * @param dValue the int value of the labeled constant. */ public void addConstant(String sName,final int dValue) { oVariables.put(sName,new Constant(new Integer(dValue))); } /** * Adds a labeled variable to the environment. This is done by * using an adapter class. You can either use an inner class or * an anonymous class for this purpose. * @param sName the label attached to the variable. * @param oVariable the variable call-back instance. * @see com.iabcinc.jmep.Variable */ public void addVariable(String sName,Variable oVariable) { oVariables.put(sName,oVariable); } /** * Returns a map containing all the variables and constants. The * contents will be of type: String, Double, Integer or Variable. * Note that you can interact directly with this map. * @see com.iabcinc.jmep.Environment#addConstant * @see com.iabcinc.jmep.Environment#addVariable */ public Map<String,Variable> getVariables() { return oVariables; } /** * Returns a map containing all the units. The contents will be * only of type Unit. * Note that you can interact directly with this map. * @see com.iabcinc.jmep.Environment#addUnit */ public Map<String,Unit> getUnits() { return oUnits; } /** * Returns a map containing all the functions. The contents will be * only of type Function. * Note that you can interact directly with this map. * @see com.iabcinc.jmep.Environment#addFunction */ public Map<String,Function> getFunctions() { return oFunctions; } /** * Returns the labeled variable. Depending how the variable was * added, this could be: String, Double, Integer or Variable. * @see com.iabcinc.jmep.Environment#addConstant * @see com.iabcinc.jmep.Environment#addVariable */ public Variable getVariable(String name) { return oVariables.get(name); } /** * Returns the labeled unit. * @see com.iabcinc.jmep.Environment#addUnit */ public Unit getUnit(String name) { return (Unit)oUnits.get(name); } /** * Returns the labeled function. * @see com.iabcinc.jmep.Environment#addFunction */ public Function getFunction(String name) { return (Function)oFunctions.get(name); } /** * Adds a Function to the environment. This is done by using an * adapter class. You can either us an inner class or an anonymous * class for this purpose. * @param sName the label attached to the added function. * @param oFunction the function call-back instance. * @see com.iabcinc.jmep.Function */ public void addFunction(String sName,Function oFunction) { oFunctions.put(sName,oFunction); } /** * Adds a Unit to the environment. This is done by using an * adapter class. You can either us an inner class or an anonymous * class for this purpose. * @param sName the label attached to the added unit. * @param oUnit the unit call-back instance. * @see com.iabcinc.jmep.Unit */ public void addUnit(String sName,Unit oUnit) { oUnits.put(sName,oUnit); } /** * Adds a default call back to the environment. This is for late * binding and its use is depriciated because of performance issues. * Whenever a variable, function or unit was not resolved at Expersion * allocation, it will call a DefaultsCB at evaluation time (evaluate()). * @param oDefaultsCB an instance the adaptor containing default call-backs. * @see com.iabcinc.jmep.DefaultsCB * @deprecated use the more performant <tt>Variable</tt>, * <tt>Function</tt> or <tt>Variable</tt>. */ public void addDefaultCB(DefaultsCB oDefaultsCB) { oDefaults.add(oDefaultsCB); } }