Fix operator associativity in SLI debug log
[sdnc/core.git] / sli / common / src / main / java / org / openecomp / sdnc / sli / SvcLogicAtom.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.io.Serializable;
25 import java.util.LinkedList;
26 import java.util.List;
27
28 public class SvcLogicAtom extends SvcLogicExpression {
29         
30         public enum AtomType {
31                 NUMBER,
32                 STRING,
33                 IDENTIFIER,
34                 CONTEXT_VAR
35
36         }
37         
38         private AtomType atomType;
39         private String atom;
40
41
42         public SvcLogicAtom(String atomType, String atom)
43         {
44                 this.atomType = AtomType.valueOf(atomType);
45                 this.atom = atom;
46                 
47         }
48         
49         public SvcLogicAtom(String atom)
50         {
51
52                 if (atom == null)
53                 {
54                         this.atomType = null;
55                         this.atom = null;
56                 }
57                 else
58                 {
59                         if (atom.startsWith("$"))
60                         {
61                                 this.atomType = AtomType.CONTEXT_VAR;
62                                 this.atom = atom.substring(1);
63                         }
64                         else
65                         {
66                                 if (Character.isDigit(atom.charAt(0)))
67                                 {
68                                         this.atomType = AtomType.NUMBER;
69                                         this.atom = atom;
70                                 }
71                                 else if (atom.charAt(0) == '\'')
72                                 {
73                                         this.atomType = AtomType.STRING;
74                                         this.atom = atom.substring(1, atom.length()-1);
75                                 }
76                                 else
77                                 {
78                                         this.atomType = AtomType.IDENTIFIER;
79                                         this.atom = atom;
80                                         
81                                 }
82                                         
83                         }
84                 }
85         }
86
87         public AtomType getAtomType() {
88                 return atomType;
89         }
90         
91         public void setAtomType(String newType)
92         {
93                 atomType = AtomType.valueOf(newType);
94         }
95
96         public String getAtom() {
97                 return atom;
98         }
99         
100         
101         
102         public void setAtomType(AtomType atomType) {
103                 this.atomType = atomType;
104         }
105
106         public void setAtom(String atom) {
107                 this.atom = atom;
108         }
109
110
111
112         public String toString()
113         {
114                 StringBuffer sbuff = new StringBuffer();
115                 switch(getAtomType())
116                 {
117                 case CONTEXT_VAR:
118                         sbuff.append("$");
119                 case IDENTIFIER:
120                         boolean needDot = false;
121                         for (SvcLogicExpression term: this.getOperands())
122                         {
123                                 if (needDot)
124                                 {
125                                         sbuff.append(".");
126                                 }
127                                 sbuff.append(term.toString());
128                                 needDot = true;
129                         }
130                         return(sbuff.toString());
131                 case STRING:
132                 case NUMBER:
133                 default:
134                         return(atom);
135                 }
136         }
137         
138         public String asParsedExpr()
139         {
140                 // simplify debugging output for NUMBER type
141                 if (atomType == AtomType.NUMBER) {
142                         return atom;
143                 }
144
145                 StringBuffer sbuff = new StringBuffer();
146                 
147                 sbuff.append("(atom");
148                 sbuff.append("<");
149                 sbuff.append(atomType.toString());
150                 sbuff.append(">");
151                 
152                 switch(atomType)
153                 {
154                 case IDENTIFIER:
155                 case CONTEXT_VAR:
156                         for (SvcLogicExpression term : getOperands())
157                         {
158                                 sbuff.append(" ");
159                                 sbuff.append(term.asParsedExpr());
160                                 
161                         }
162                         break;
163                 default:
164                         sbuff.append(" ");
165                         sbuff.append(atom);
166                 }
167                 
168                 sbuff.append(")");
169                 return(sbuff.toString());
170         }
171
172         
173         
174 }