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 de.congrace.exp4j;
20 import java.util.Stack;
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()
27 * @author fas@congrace.de
29 public final class PostfixExpression extends AbstractExpression implements Calculable {
31 private VariableSet variables = new VariableSet();
34 * Factory method for creating {@link PostfixExpression}s from human
35 * readable infix expressions
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
47 public static PostfixExpression fromInfix(String expression) throws UnparsableExpressionException, UnknownFunctionException {
48 return fromInfix(expression, null);
52 * Factory method for creating {@link PostfixExpression}s from human
53 * readable infix expressions
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}
67 public static PostfixExpression fromInfix(String expression, Set<CustomFunction> customFunctions) throws UnparsableExpressionException,
68 UnknownFunctionException {
69 String[] variableStrings = null;
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(",");
78 return new PostfixExpression(InfixTranslator.toPostfixExpression(expression, variableStrings, customFunctions), variableStrings, customFunctions);
82 * Construct a new simple {@link PostfixExpression}
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
95 private PostfixExpression(String expression, String[] variableStrings, Set<CustomFunction> customFunctions) throws UnparsableExpressionException,
96 UnknownFunctionException {
97 super(expression, new Tokenizer(variableStrings, customFunctions).tokenize(expression), variableStrings);
101 * delegate the calculation of a simple expression
103 public Variable calculate() throws IllegalArgumentException {
105 final Stack<Variable> stack = new Stack<Variable>();
106 for (final Token t : getTokens()) {
107 ((CalculationToken) t).mutateStackForCalculation(stack, variables);
113 public void setVariable(Variable value) {
114 variables.add(value);