9b35006ce789cd4f17e8aaa3923973604bf4870b
[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                         sbuff.append(operands.get(i+1).toString());
106                 }
107                 
108                 return(sbuff.toString());
109
110         }
111         
112         public String asParsedExpr()
113         {
114                 
115                 List<SvcLogicExpression>operands = getOperands();
116                 StringBuffer sbuff = new StringBuffer();
117                 StringBuffer closeParens = new StringBuffer();
118                 int i = 0;
119                 for (OperatorType operator : operators)
120                 {
121                         sbuff.append("(");
122                         sbuff.append(operator.getText());
123                         sbuff.append(" ");
124                         sbuff.append(operands.get(i++).asParsedExpr());
125                         closeParens.append(")");
126                 }
127                 sbuff.append(" ");
128                 if (i < operands.size())
129                 {
130                         sbuff.append(operands.get(i).asParsedExpr());
131                 }
132                 else
133                 {
134                         sbuff.append("__MISSING_OPERAND__");
135                 }
136                 sbuff.append(closeParens.toString());
137                 return(sbuff.toString());
138                 
139         }
140
141 }