1 package net.sf.openrocket.util.exp4j;
3 import java.util.Stack;
5 import net.sf.openrocket.util.exp4j.FunctionToken.Function;
8 * this classed is used to create custom functions for exp4j<br/>
12 * CustomFunction fooFunc = new CustomFunction("foo") {
13 * public double applyFunction(double value) {
14 * return value*Math.E;
18 * Calculable calc = new ExpressionBuilder("foo(x)").withCustomFunction(fooFunc).withVariable("x",varX).build();
19 * assertTrue(calc.calculate() == Math.E * varX);
25 public abstract class CustomFunction extends CalculationToken {
29 * create a new single value input CustomFunction with a set name
32 * the name of the function (e.g. foo)
34 protected CustomFunction(String value) throws InvalidCustomFunctionException{
36 for (Function f:Function.values()) {
37 if (value.equalsIgnoreCase(f.toString())){
38 throw new InvalidCustomFunctionException(value + " is already reserved as a function name");
44 * create a new single value input CustomFunction with a set name
47 * the name of the function (e.g. foo)
49 protected CustomFunction(String value,int argumentCount) throws InvalidCustomFunctionException{
51 this.argc=argumentCount;
52 for (Function f:Function.values()) {
53 if (value.equalsIgnoreCase(f.toString())){
54 throw new InvalidCustomFunctionException(value + " is already reserved as a function name");
60 * apply the function to a value
63 * the values to which the function should be applied.
64 * @return the function value
66 public abstract double applyFunction(double[] values);
69 void mutateStackForCalculation(Stack<Double> stack, Map<String, Double> variableValues) {
70 double[] args=new double[argc];
71 for (int i=0;i<argc;i++) {
74 stack.push(this.applyFunction(args));
78 void mutateStackForInfixTranslation(Stack<Token> operatorStack, StringBuilder output) {
79 operatorStack.push(this);
81 public int getArgumentCount() {