FuncMap.java

Go to the documentation of this file.
00001 package com.graphbuilder.math;
00002 
00003 import com.graphbuilder.math.func.*;
00004 
00081 public class FuncMap {
00082 
00083     private String[] name = new String[50];
00084     private Function[] func = new Function[50];
00085     private int numFunc = 0;
00086     private boolean caseSensitive = false;
00087 
00088     public FuncMap() {}
00089 
00090     public FuncMap(boolean caseSensitive) {
00091         this.caseSensitive = caseSensitive;
00092     }
00093 
00097     public void loadDefaultFunctions() {
00098         // >= 0 parameters
00099         setFunction("min", new MinFunction());
00100         setFunction("max", new MaxFunction());
00101 
00102         // > 0 parameters
00103         setFunction("sum", new SumFunction());
00104         setFunction("avg", new AvgFunction());
00105 
00106         // 0 parameters
00107         //setFunction("pi", new PiFunction());
00108         setFunction("e", new EFunction());
00109 //      setFunction("rand", new RandFunction());
00110 
00111         // 1 parameter
00112 //      setFunction("sin", new SinFunction());
00113 //      setFunction("cos", new CosFunction());
00114 //      setFunction("tan", new TanFunction());
00115         setFunction("sqrt", new SqrtFunction());
00116         setFunction("abs", new AbsFunction());
00117         setFunction("ceil", new CeilFunction());
00118         setFunction("floor", new FloorFunction());
00119         setFunction("exp", new ExpFunction());
00120         setFunction("lg", new LgFunction());
00121         setFunction("ln", new LnFunction());
00122         setFunction("sign", new SignFunction());
00123         setFunction("round", new RoundFunction());
00124         setFunction("fact", new FactFunction());
00125 //      setFunction("cosh", new CoshFunction());
00126 //      setFunction("sinh", new SinhFunction());
00127 //      setFunction("tanh", new TanhFunction());
00128 //      setFunction("acos", new AcosFunction());
00129 //      setFunction("asin", new AsinFunction());
00130 //      setFunction("atan", new AtanFunction());
00131 //      setFunction("acosh", new AcoshFunction());
00132 //      setFunction("asinh", new AsinhFunction());
00133 //      setFunction("atanh", new AtanhFunction());
00134 
00135         // 2 parameters
00136         setFunction("pow", new PowFunction());
00137         setFunction("mod", new ModFunction());
00138         setFunction("combin", new CombinFunction());
00139 
00140         // 1 or 2 parameters
00141         setFunction("log", new LogFunction());
00142         
00143         // 2 or 3 parameters
00144         setFunction("if", new IfFunction());
00145     }
00146 
00152     public Function getFunction(String funcName, int numParam) {
00153         for (int i = 0; i < numFunc; i++) {
00154             if (func[i].acceptNumParam(numParam) && (caseSensitive && name[i].equals(funcName) || !caseSensitive && name[i].equalsIgnoreCase(funcName)))
00155                 return func[i];
00156         }
00157 
00158         throw new RuntimeException("function not found: " + funcName + " " + numParam);
00159     }
00160 
00166     public void setFunction(String funcName, Function f) {
00167         if (funcName == null)
00168             throw new IllegalArgumentException("function name cannot be null");
00169 
00170         if (f == null)
00171             throw new IllegalArgumentException("function cannot be null");
00172 
00173         for (int i = 0; i < numFunc; i++) {
00174             if (caseSensitive && name[i].equals(funcName) || !caseSensitive && name[i].equalsIgnoreCase(funcName)) {
00175                 func[i] = f;
00176                 return;
00177             }
00178         }
00179 
00180         if (numFunc == name.length) {
00181             String[] tmp1 = new String[2 * numFunc];
00182             Function[] tmp2 = new Function[tmp1.length];
00183 
00184             for (int i = 0; i < numFunc; i++) {
00185                 tmp1[i] = name[i];
00186                 tmp2[i] = func[i];
00187             }
00188 
00189             name = tmp1;
00190             func = tmp2;
00191         }
00192 
00193         name[numFunc] = funcName;
00194         func[numFunc] = f;
00195         numFunc++;
00196     }
00197 
00201     public boolean isCaseSensitive() {
00202         return caseSensitive;
00203     }
00204 
00208     public String[] getFunctionNames() {
00209         String[] arr = new String[numFunc];
00210 
00211         for (int i = 0; i < arr.length; i++)
00212             arr[i] = name[i];
00213 
00214         return arr;
00215     }
00216 
00221     public Function[] getFunctions() {
00222         Function[] arr = new Function[numFunc];
00223 
00224         for (int i = 0; i < arr.length; i++)
00225             arr[i] = func[i];
00226 
00227         return arr;
00228     }
00229 
00234     public void remove(String funcName) {
00235         for (int i = 0; i < numFunc; i++) {
00236             if (caseSensitive && name[i].equals(funcName) || !caseSensitive && name[i].equalsIgnoreCase(funcName)) {
00237                 for (int j = i + 1; j < numFunc; j++) {
00238                     name[j - 1] = name[j];
00239                     func[j - 1] = func[j];
00240                 }
00241                 numFunc--;
00242                 name[numFunc] = null;
00243                 func[numFunc] = null;
00244                 break;
00245             }
00246         }
00247     }
00248 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1