package gve.calc.formula;

import java.util.Hashtable;

When redefining a variable, the old contents are lost forever.
public class SingleFallbackEvaluator implements Evaluator { private Hashtable vars; private Evaluator fbEvaluator; private Hashtable functions = new Hashtable();
If this evaluator does not contain a variable, ask it the fallback evaluator. You cannot undefine variables in the fallback evaluator; this Evaluator keeps the fallback unchanged.
public SingleFallbackEvaluator(Evaluator fallback) { fbEvaluator = fallback; vars = new Hashtable(); }
Note that this copy is shallow with respect to fbEvaluator
public Object clone() { SingleFallbackEvaluator result = new SingleFallbackEvaluator(fbEvaluator); if (vars != null) result.vars = (Hashtable)vars.clone(); result.functions = (Hashtable)functions.clone(); return result; } public void defineVariable(String name,Part value) { vars.put(name,value); } public java.util.Enumeration variables() { return new FallbackEnumeration(vars.keys(),fbEvaluator.variables()); } public void undefVariable(String name) { vars.remove(name); } public Part getValue(String identifier) { Part o = (Part)vars.get(identifier); if (o == null) return fbEvaluator.getValue(identifier); return o; } public void setValue(String name,Part value) { vars.put(name,value); } public String toString() { return vars.toString(); } public void dump() { java.util.Enumeration keys = vars.keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); System.out.println("---key "+key); Object val = vars.get(key); if (val instanceof Part) ((Part)val).dump(1); else System.out.println(val); } System.out.println("Fallback evaluator contains:"); fbEvaluator.dump(); } public Part call(String funcname,Part args) { Function f = (Function)functions.get(funcname); if (f == null) return fbEvaluator.call(funcname,args); return f.call(funcname,args,this); } public void setFunction(String funcname,Function f) { functions.put(funcname,f); } public boolean isFunction(String funcname) { if (functions.get(funcname) != null) return true; return fbEvaluator.isFunction(funcname); } }