[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / util / str / StrUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.util.str;
23
24 import java.lang.reflect.Array;
25 import java.lang.reflect.Field;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Date;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.SortedSet;
33 import java.util.TreeSet;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class StrUtil {
39
40         private static final Logger log = LoggerFactory.getLogger(StrUtil.class);
41
42         public static final String INDENT_STR = "    ";
43
44         public static void indent(StringBuilder ss, int ind) {
45                 for (int i = 0; i < ind; i++)
46                         ss.append(INDENT_STR);
47         }
48
49         public static void info(Logger log, Object o) {
50                 if (log.isInfoEnabled()) {
51                         StringBuilder ss = new StringBuilder();
52                         struct(ss, o);
53                         log.info(ss.toString());
54                 }
55         }
56
57         public static void debug(Logger log, Object o) {
58                 if (log.isDebugEnabled()) {
59                         StringBuilder ss = new StringBuilder();
60                         struct(ss, o);
61                         log.debug(ss.toString());
62                 }
63         }
64
65         public static void struct(StringBuilder ss, Object o) {
66                 struct(ss, o, 0);
67         }
68
69         public static void struct(StringBuilder ss, Object o, int ind) {
70                 if (o == null) {
71                         ss.append("null");
72                         return;
73                 }
74
75                 if (isSimple(o)) {
76                         ss.append(o);
77                         return;
78                 }
79
80                 Class<? extends Object> cls = o.getClass();
81
82                 if (cls.isEnum()) {
83                         ss.append(o);
84                         return;
85                 }
86
87                 if (cls.isArray()) {
88                         int n = Array.getLength(o);
89                         if (n == 0) {
90                                 ss.append("[]");
91                                 return;
92                         }
93
94                         Object o1 = Array.get(o, 0);
95                         if (isSimple(o1)) {
96                                 ss.append('[').append(o1);
97                                 for (int i = 1; i < n; i++) {
98                                         o1 = Array.get(o, i);
99                                         ss.append(", ").append(o1);
100                                 }
101                                 ss.append(']');
102                                 return;
103                         }
104
105                         ss.append('\n');
106                         indent(ss, ind + 1);
107                         ss.append('[');
108                         struct(ss, o1, ind + 1);
109                         for (int i = 1; i < n; i++) {
110                                 o1 = Array.get(o, i);
111                                 struct(ss, o1, ind + 1);
112                         }
113                         ss.append('\n');
114                         indent(ss, ind + 1);
115                         ss.append(']');
116                         return;
117                 }
118
119                 if (o instanceof Collection<?>) {
120                         Collection<?> ll = (Collection<?>) o;
121
122                         int n = ll.size();
123                         if (n == 0) {
124                                 ss.append("[]");
125                                 return;
126                         }
127
128                         Iterator<?> ii = ll.iterator();
129                         Object o1 = ii.next();
130                         if (isSimple(o1)) {
131                                 ss.append('[').append(o1);
132                                 while (ii.hasNext()) {
133                                         o1 = ii.next();
134                                         ss.append(", ").append(o1);
135                                 }
136                                 ss.append(']');
137                                 return;
138                         }
139
140                         ss.append('\n');
141                         indent(ss, ind + 1);
142                         ss.append('[');
143                         struct(ss, o1, ind + 1);
144                         while (ii.hasNext()) {
145                                 o1 = ii.next();
146                                 struct(ss, o1, ind + 1);
147                         }
148                         ss.append('\n');
149                         indent(ss, ind + 1);
150                         ss.append(']');
151                         return;
152
153                 }
154
155                 if (o instanceof Map<?, ?>) {
156                         Map<?, ?> mm = (Map<?, ?>) o;
157
158                         int n = mm.size();
159                         if (n == 0) {
160                                 ss.append("{}");
161                                 return;
162                         }
163
164                         ss.append('{');
165
166                         for (Object k : mm.keySet()) {
167                                 ss.append('\n');
168                                 indent(ss, ind + 1);
169                                 ss.append(k).append(": ");
170
171                                 Object o1 = mm.get(k);
172                                 struct(ss, o1, ind + 2);
173                         }
174
175                         ss.append('\n');
176                         indent(ss, ind);
177                         ss.append('}');
178
179                         return;
180                 }
181
182                 Field[] fields = cls.getFields();
183
184                 if (fields.length == 0) {
185                         ss.append(o);
186                         return;
187                 }
188
189                 ss.append('\n');
190                 indent(ss, ind + 1);
191                 ss.append('<').append(cls.getSimpleName()).append("> {");
192                 for (Field f : fields) {
193                         ss.append('\n');
194                         indent(ss, ind + 2);
195                         ss.append(f.getName()).append(": ");
196                         Object v = null;
197                         try {
198                                 v = f.get(o);
199                         } catch (IllegalAccessException e) {
200                                 v = "*** Cannot obtain value *** : " + e.getMessage();
201                         }
202                         struct(ss, v, ind + 2);
203                 }
204                 ss.append('\n');
205                 indent(ss, ind + 1);
206                 ss.append('}');
207         }
208
209         public static SortedSet<Integer> listInt(String ss, String warning) {
210                 if (ss == null || ss.length() == 0)
211                         return null;
212
213                 SortedSet<Integer> ll = new TreeSet<Integer>();
214                 String[] str = ss.split(",");
215                 for (String s : str) {
216                         try {
217                                 int i1 = s.indexOf('-');
218                                 int start, end;
219                                 if (i1 > 0) {
220                                         String s1 = s.substring(0, i1);
221                                         String s2 = s.substring(i1 + 1);
222                                         start = Integer.parseInt(s1);
223                                         end = Integer.parseInt(s2);
224                                 } else
225                                         start = end = Integer.parseInt(s);
226                                 for (int i = start; i <= end; i++)
227                                         ll.add(i);
228                         } catch (NumberFormatException e) {
229                                 // Skip this - bad data in DB
230                                 log.warn(warning + " [" + s + "].", e);
231                         }
232                 }
233                 return ll;
234         }
235
236         public static String listInt(SortedSet<Integer> ll) {
237                 if (ll == null || ll.size() == 0)
238                         return null;
239
240                 StringBuilder sb = new StringBuilder(2000);
241                 Iterator<Integer> i = ll.iterator();
242                 int n = i.next();
243                 int start = n;
244                 int end = n;
245                 boolean first = true;
246                 while (i.hasNext()) {
247                         n = i.next();
248                         if (n != end + 1) {
249                                 if (!first)
250                                         sb.append(',');
251                                 first = false;
252
253                                 if (start == end)
254                                         sb.append(start);
255                                 else if (start == end - 1)
256                                         sb.append(start).append(',').append(end);
257                                 else
258                                         sb.append(start).append('-').append(end);
259
260                                 start = n;
261                         }
262                         end = n;
263                 }
264
265                 if (!first)
266                         sb.append(',');
267
268                 if (start == end)
269                         sb.append(start);
270                 else if (start == end - 1)
271                         sb.append(start).append(',').append(end);
272                 else
273                         sb.append(start).append('-').append(end);
274
275                 return sb.toString();
276         }
277
278         public static List<String> listStr(String s) {
279                 if (s == null || s.length() == 0)
280                         return null;
281                 String[] ss = s.split(",");
282                 return Arrays.asList(ss);
283         }
284
285         public static String listStr(Collection<String> ll) {
286                 if (ll == null || ll.isEmpty())
287                         return null;
288                 StringBuilder ss = new StringBuilder(1000);
289                 Iterator<String> i = ll.iterator();
290                 ss.append(i.next());
291                 while (i.hasNext())
292                         ss.append(',').append(i.next());
293                 return ss.toString();
294         }
295
296         private static boolean isSimple(Object o) {
297                 if (o == null)
298                         return true;
299
300                 if (o instanceof Number || o instanceof String || o instanceof Boolean || o instanceof Date)
301                         return true;
302
303                 return false;
304         }
305 }