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