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