create changelog entry
[debian/openrocket] / core / src / de / congrace / exp4j / ParenthesisToken.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.Stack;
20
21 /**
22  * Token for parenthesis
23  * 
24  * @author fas@congrace.de
25  */
26 class ParenthesisToken extends Token {
27
28         ParenthesisToken(String value) {
29                 super(value);
30         }
31
32         @Override
33         public boolean equals(Object obj) {
34                 if (obj instanceof ParenthesisToken) {
35                         final ParenthesisToken t = (ParenthesisToken) obj;
36                         return t.getValue().equals(this.getValue());
37                 }
38                 return false;
39         }
40
41         @Override
42         public int hashCode() {
43                 return getValue().hashCode();
44         }
45
46         /**
47          * check the direction of the parenthesis
48          * 
49          * @return true if it's a left parenthesis (open) false if it is a right
50          *         parenthesis (closed)
51          */
52         boolean isOpen() {
53                 return getValue().equals("(") || getValue().equals("[") || getValue().equals("{");
54         }
55
56         @Override
57         void mutateStackForInfixTranslation(Stack<Token> operatorStack, StringBuilder output) {
58                 if (this.isOpen()) {
59                         operatorStack.push(this);
60                 } else {
61                         Token next;
62                         while ((next = operatorStack.peek()) instanceof OperatorToken || next instanceof FunctionToken || next instanceof CustomFunction
63                                         || (next instanceof ParenthesisToken && !((ParenthesisToken) next).isOpen())) {
64                                 output.append(operatorStack.pop().getValue()).append(" ");
65                         }
66                         operatorStack.pop();
67                 }
68         }
69 }