Fix to streamer loading of materials; missing material in Giant Leaps file.
[debian/openrocket] / core / src / net / sf / openrocket / util / exp4j / FunctionToken.java
1 /*
2 Copyright 2011 frank asseg
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15
16  */
17 package net.sf.openrocket.util.exp4j;
18
19 import java.util.Map;
20 import java.util.Stack;
21
22 /**
23  * A {@link Token} for functions
24  * 
25  * @author fas@congrace.de
26  * 
27  */
28 class FunctionToken extends CalculationToken {
29         /**
30          * the functionNames that can be used in an expression
31          * 
32          * @author ruckus
33          * 
34          */
35         enum Function {
36                 ABS, ACOS, ASIN, ATAN, CBRT, CEIL, COS, COSH, EXP, EXPM1, FLOOR, LOG, SIN, SINH, SQRT, TAN, TANH
37         }
38
39         private Function function;
40
41         /**
42          * construct a new {@link FunctionToken}
43          * 
44          * @param value
45          *            the name of the function
46          * @throws UnknownFunctionException
47          *             if an unknown function name is encountered
48          */
49         FunctionToken(String value) throws UnknownFunctionException {
50                 super(value);
51                 try {
52                         function = Function.valueOf(value.toUpperCase());
53                 } catch (IllegalArgumentException e) {
54                         throw new UnknownFunctionException(value);
55                 }
56                 if (function == null) {
57                         throw new UnknownFunctionException(value);
58                 }
59         }
60
61         /**
62          * apply a function to a value x
63          * 
64          * @param x
65          *            the value the function should be applied to
66          * @return the result of the function
67          */
68         double applyFunction(double x) {
69                 switch (function) {
70                 case ABS:
71                         return Math.abs(x);
72                 case ACOS:
73                         return Math.acos(x);
74                 case ASIN:
75                         return Math.asin(x);
76                 case ATAN:
77                         return Math.atan(x);
78                 case CBRT:
79                         return Math.cbrt(x);
80                 case CEIL:
81                         return Math.ceil(x);
82                 case COS:
83                         return Math.cos(x);
84                 case COSH:
85                         return Math.cosh(x);
86                 case EXP:
87                         return Math.exp(x);
88                 case EXPM1:
89                         return Math.expm1(x);
90                 case FLOOR:
91                         return Math.floor(x);
92                 case LOG:
93                         return Math.log(x);
94                 case SIN:
95                         return Math.sin(x);
96                 case SINH:
97                         return Math.sinh(x);
98                 case SQRT:
99                         return Math.sqrt(x);
100                 case TAN:
101                         return Math.tan(x);
102                 case TANH:
103                         return Math.tanh(x);
104                 default:
105                         return Double.NaN; // should not happen ;)
106                 }
107         }
108
109         /**
110          * get the {@link Function}
111          * 
112          * @return the correspoding {@link Function}
113          */
114         Function getFunction() {
115                 return function;
116         }
117
118         @Override
119         void mutateStackForCalculation(Stack<Double> stack, Map<String, Double> variableValues) {
120                 stack.push(this.applyFunction(stack.pop()));
121         }
122
123         @Override
124         void mutateStackForInfixTranslation(Stack<Token> operatorStack, StringBuilder output) {
125                 operatorStack.push(this);
126         }
127 }