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 * {@link Token} for Operations like +,-,*,/,% and ^
25 * @author fas@congrace.de
27 class OperatorToken extends CalculationToken {
30 * the valid {@link Operation}s for the {@link OperatorToken}
32 * @author fas@congrace.de
35 ADDITION(1, true), SUBTRACTION(1, true), MULTIPLICATION(2, true), DIVISION(2, true), MODULO(2, true), EXPONENTIATION(3, false), UNARY_MINUS(4, false), UNARY_PLUS(
37 private final int precedence;
38 private final boolean leftAssociative;
40 private Operation(int precedence, boolean leftAssociative) {
41 this.precedence = precedence;
42 this.leftAssociative = leftAssociative;
47 * return a corresponding {@link Operation} for a symbol
50 * the symbol of the operation
51 * @return the corresponding {@link Operation}
53 static Operation getOperation(char c) {
56 return Operation.ADDITION;
58 return Operation.SUBTRACTION;
60 return Operation.MULTIPLICATION;
62 return Operation.DIVISION;
64 return Operation.EXPONENTIATION;
66 return Operation.UNARY_MINUS;
68 return Operation.MODULO;
74 static boolean isOperator(char c) {
75 return getOperation(c) != null;
78 private final Operation operation;
81 * construct a new {@link OperatorToken}
84 * the symbol (e.g.: '+')
86 * the {@link Operation} of this {@link Token}
88 OperatorToken(String value, Operation operation) {
90 this.operation = operation;
94 * apply the {@link Operation}
97 * the doubles to operate on
98 * @return the result of the {@link Operation}
100 double applyOperation(double... values) {
103 return values[0] + values[1];
105 return values[0] - values[1];
107 return values[0] * values[1];
109 return Math.pow(values[0], values[1]);
111 return values[0] / values[1];
117 return values[0] % values[1];
124 public boolean equals(Object obj) {
125 if (obj instanceof OperatorToken) {
126 final OperatorToken t = (OperatorToken) obj;
127 return t.getValue().equals(this.getValue());
132 int getOperandCount() {
150 * get the {@link Operation} of this {@link Token}
152 * @return the {@link Operation}
154 Operation getOperation() {
158 int getPrecedence() {
159 return operation.precedence;
163 public int hashCode() {
164 return getValue().hashCode();
168 * check if the operation is left associative
170 * @return true if left associative, otherwise false
172 boolean isLeftAssociative() {
173 return operation.leftAssociative;
177 void mutateStackForCalculation(Stack<Double> stack, Map<String, Double> variableValues) {
178 if (this.getOperandCount() == 2) {
179 final double n2 = stack.pop();
180 final double n1 = stack.pop();
181 stack.push(this.applyOperation(n1, n2));
182 } else if (this.getOperandCount() == 1) {
183 final double n1 = stack.pop();
184 stack.push(this.applyOperation(n1));
189 void mutateStackForInfixTranslation(Stack<Token> operatorStack, StringBuilder output) {
191 while (!operatorStack.isEmpty() && (before = operatorStack.peek()) != null && (before instanceof OperatorToken || before instanceof FunctionToken)) {
192 if (before instanceof FunctionToken) {
194 output.append(before.getValue()).append(" ");
196 final OperatorToken stackOperator = (OperatorToken) before;
197 if (this.isLeftAssociative() && this.getPrecedence() <= stackOperator.getPrecedence()) {
198 output.append(operatorStack.pop().getValue()).append(" ");
199 } else if (!this.isLeftAssociative() && this.getPrecedence() < stackOperator.getPrecedence()) {
200 output.append(operatorStack.pop().getValue()).append(" ");
206 operatorStack.push(this);