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