2 Copyright 2011 frank asseg
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 package net.sf.openrocket.util.exp4j;
20 import java.util.Stack;
23 * A {@link Token} for functions
25 * @author fas@congrace.de
28 class FunctionToken extends CalculationToken {
30 * the functionNames that can be used in an expression
36 ABS, ACOS, ASIN, ATAN, CBRT, CEIL, COS, COSH, EXP, EXPM1, FLOOR, LOG, SIN, SINH, SQRT, TAN, TANH
39 private Function function;
42 * construct a new {@link FunctionToken}
45 * the name of the function
46 * @throws UnknownFunctionException
47 * if an unknown function name is encountered
49 FunctionToken(String value) throws UnknownFunctionException {
52 function = Function.valueOf(value.toUpperCase());
53 } catch (IllegalArgumentException e) {
54 throw new UnknownFunctionException(value);
56 if (function == null) {
57 throw new UnknownFunctionException(value);
62 * apply a function to a value x
65 * the value the function should be applied to
66 * @return the result of the function
68 double applyFunction(double x) {
105 return Double.NaN; // should not happen ;)
110 * get the {@link Function}
112 * @return the correspoding {@link Function}
114 Function getFunction() {
119 void mutateStackForCalculation(Stack<Double> stack, Map<String, Double> variableValues) {
120 stack.push(this.applyFunction(stack.pop()));
124 void mutateStackForInfixTranslation(Stack<Token> operatorStack, StringBuilder output) {
125 operatorStack.push(this);