create changelog entry
[debian/openrocket] / core / src / de / congrace / exp4j / PostfixExpression.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 de.congrace.exp4j;
18
19 import java.util.Set;
20 import java.util.Stack;
21
22 /**
23  * Class for calculating values from a RPN postfix expression.<br/>
24  * The default way to create a new instance of {@link PostfixExpression} is by
25  * using the static factory method fromInfix()
26  * 
27  * @author fas@congrace.de
28  */
29 public final class PostfixExpression extends AbstractExpression implements Calculable {
30         
31         private VariableSet variables = new VariableSet();
32         
33         /**
34          * Factory method for creating {@link PostfixExpression}s from human
35          * readable infix expressions
36          * 
37          * @param expression
38          *            the infix expression to be used
39          * @return an equivalent {@link PostfixExpression}
40          * @throws UnparsableExpressionException
41          *             if the expression was invalid
42          * @throws UnknownFunctionException
43          *             if an unknown function has been used
44          * @deprecated please use {@link ExpressionBuilder} API
45          */
46     @Deprecated
47         public static PostfixExpression fromInfix(String expression) throws UnparsableExpressionException, UnknownFunctionException {
48                 return fromInfix(expression, null);
49         }
50
51         /**
52          * Factory method for creating {@link PostfixExpression}s from human
53          * readable infix expressions
54          * 
55          * @param expression
56          *            the infix expression to be used
57          * @param customFunctions
58          *            the CustomFunction implementations used
59          * @return an equivalent {@link PostfixExpression}
60          * @throws UnparsableExpressionException
61          *             if the expression was invalid
62          * @throws UnknownFunctionException
63          *             if an unknown function has been used
64          * @deprecated please use {@link ExpressionBuilder}
65          */
66     @Deprecated
67         public static PostfixExpression fromInfix(String expression, Set<CustomFunction> customFunctions) throws UnparsableExpressionException,
68                         UnknownFunctionException {
69                 String[] variableStrings = null;
70                 int posStart, posEnd;
71                 if ((posStart = expression.indexOf('=')) > 0) {
72                         String functionDef = expression.substring(0, posStart);
73                         expression = expression.substring(posStart + 1);
74                         if ((posStart = functionDef.indexOf('(')) > 0 && (posEnd = functionDef.indexOf(')')) > 0) {
75                                 variableStrings = functionDef.substring(posStart + 1, posEnd).split(",");
76                         }
77                 }
78                 return new PostfixExpression(InfixTranslator.toPostfixExpression(expression, variableStrings, customFunctions), variableStrings, customFunctions);
79         }
80         
81         /**
82          * Construct a new simple {@link PostfixExpression}
83          * 
84          * @param expression
85          *            the postfix expression to be calculated
86          * @param variableNames
87          *            the variable names in the expression
88          * @param customFunctions
89          *            the CustomFunction implementations used
90          * @throws UnparsableExpressionException
91          *             when expression is invalid
92          * @throws UnknownFunctionException
93          *             when an unknown function has been used
94          */
95         private PostfixExpression(String expression, String[] variableStrings, Set<CustomFunction> customFunctions) throws UnparsableExpressionException,
96                         UnknownFunctionException {
97                 super(expression, new Tokenizer(variableStrings, customFunctions).tokenize(expression), variableStrings);
98         }
99
100         /**
101          * delegate the calculation of a simple expression 
102          */
103         public Variable calculate() throws IllegalArgumentException {
104
105                 final Stack<Variable> stack = new Stack<Variable>();
106                 for (final Token t : getTokens()) {
107                         ((CalculationToken) t).mutateStackForCalculation(stack, variables);
108                 }
109                 return stack.pop();
110
111         }
112
113         public void setVariable(Variable value) {
114                 variables.add(value);
115         }
116 }