Fix operator associativity in SLI debug log
[sdnc/core.git] / sli / common / src / main / java / org / openecomp / sdnc / sli / SvcLogicBinaryExpression.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.sli;
23
24 import java.util.LinkedList;
25 import java.util.List;
26
27 public class SvcLogicBinaryExpression extends SvcLogicExpression {
28         
29
30         public enum OperatorType {
31                 addOp("+"),
32                 subOp("-"),
33                 multOp("*"),
34                 divOp("/"),
35                 equalOp("=="),
36                 ltOp("<"),
37                 leOp("<="),
38                 gtOp(">"),
39                 geOp(">="),
40                 neOp("!="),
41                 andOp("and"),
42                 orOp("or");
43                 
44                 private String text;
45                 
46                 private OperatorType(String text)
47                 {
48                         this.text = text;
49                 }
50                 
51                 public String getText()
52                 {
53                         return(text);
54                 }
55                 
56                 public static OperatorType fromString(String text)
57                 {
58                         if (text != null)
59                         {
60                                 for (OperatorType t : OperatorType.values())
61                                 {
62                                         if (text.equalsIgnoreCase(t.getText())) {
63                                                 
64                                                 return(t);
65                                         }
66                                 }
67                         }
68                         return(null);
69                 }
70                 
71                 public String toString()
72                 {
73                         return(text);
74                 }
75         }
76         private List<OperatorType> operators;
77         
78         public List<OperatorType> getOperators() {
79                 return operators;
80         }
81
82         public SvcLogicBinaryExpression()
83         {
84                 operators = new LinkedList<OperatorType>();
85         }
86         
87         public void addOperator(String operator)
88         {
89                 operators.add(OperatorType.fromString(operator));
90         }
91
92         
93         public String toString()
94         {
95                 
96                 List<SvcLogicExpression>operands = getOperands();
97                 StringBuffer sbuff = new StringBuffer();
98
99                 sbuff.append(operands.get(0).toString());
100                 for (int i = 0 ; i < operators.size(); i++)
101                 {
102                         sbuff.append(" ");
103                         sbuff.append(operators.get(i));
104                         sbuff.append(" ");
105                         if (i + 1 < operands.size()) {
106                                 sbuff.append(operands.get(i + 1).toString());
107                         } else {
108                                 // expression incomplete; operand not bound yet
109                                 sbuff.append("?");
110                         }
111                 }
112                 
113                 return(sbuff.toString());
114
115         }
116         
117         public String asParsedExpr() {
118
119                 List<SvcLogicExpression> operands = getOperands();
120
121                 if (operators.isEmpty()) {
122                         return operands.get(0).asParsedExpr();
123                 } else {
124                         StringBuffer sbuff = new StringBuffer();
125                         // operators in reverse order for left associativity
126                         for (int i = operators.size() - 1; i >= 0; --i) {
127                                 sbuff.append("(");
128                                 sbuff.append(operators.get(i).getText());
129                                 sbuff.append(" ");
130                         }
131                         for (int i = 0; i < operators.size() + 1; ++i) {
132                                 if (i < operands.size()) {
133                                         sbuff.append(operands.get(i).asParsedExpr());
134                                 } else {
135                                         // expression incomplete; operand not bound yet
136                                         sbuff.append("?");
137                                 }
138                                 if (i != 0) {
139                                         sbuff.append(")");
140                                 }
141                                 if (i < operators.size()) {
142                                         sbuff.append(" ");
143                                 }
144                         }
145                         return sbuff.toString();
146                 }
147         }
148
149 }