2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdnc.util.str;
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;
32 import java.util.SortedSet;
33 import java.util.TreeSet;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class StrUtil {
40 private static final Logger log = LoggerFactory.getLogger(StrUtil.class);
42 public static final String INDENT_STR = " ";
44 public static void indent(StringBuilder ss, int ind) {
45 for (int i = 0; i < ind; i++)
46 ss.append(INDENT_STR);
49 public static void info(Logger log, Object o) {
50 if (log.isInfoEnabled()) {
51 StringBuilder ss = new StringBuilder();
53 log.info(ss.toString());
57 public static void debug(Logger log, Object o) {
58 if (log.isDebugEnabled()) {
59 StringBuilder ss = new StringBuilder();
61 log.debug(ss.toString());
65 public static void struct(StringBuilder ss, Object o) {
69 public static void struct(StringBuilder ss, Object o, int ind) {
80 Class<? extends Object> cls = o.getClass();
88 int n = Array.getLength(o);
94 Object o1 = Array.get(o, 0);
96 ss.append('[').append(o1);
97 for (int i = 1; i < n; i++) {
99 ss.append(", ").append(o1);
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);
119 if (o instanceof Collection<?>) {
120 Collection<?> ll = (Collection<?>) o;
128 Iterator<?> ii = ll.iterator();
129 Object o1 = ii.next();
131 ss.append('[').append(o1);
132 while (ii.hasNext()) {
134 ss.append(", ").append(o1);
143 struct(ss, o1, ind + 1);
144 while (ii.hasNext()) {
146 struct(ss, o1, ind + 1);
155 if (o instanceof Map<?, ?>) {
156 Map<?, ?> mm = (Map<?, ?>) o;
166 for (Object k : mm.keySet()) {
169 ss.append(k).append(": ");
171 Object o1 = mm.get(k);
172 struct(ss, o1, ind + 2);
182 Field[] fields = cls.getFields();
184 if (fields.length == 0) {
191 ss.append('<').append(cls.getSimpleName()).append("> {");
192 for (Field f : fields) {
195 ss.append(f.getName()).append(": ");
199 } catch (IllegalAccessException e) {
200 v = "*** Cannot obtain value *** : " + e.getMessage();
202 struct(ss, v, ind + 2);
209 public static SortedSet<Integer> listInt(String ss, String warning) {
210 if (ss == null || ss.length() == 0)
213 SortedSet<Integer> ll = new TreeSet<Integer>();
214 String[] str = ss.split(",");
215 for (String s : str) {
217 int i1 = s.indexOf('-');
220 String s1 = s.substring(0, i1);
221 String s2 = s.substring(i1 + 1);
222 start = Integer.parseInt(s1);
223 end = Integer.parseInt(s2);
225 start = end = Integer.parseInt(s);
226 for (int i = start; i <= end; i++)
228 } catch (NumberFormatException e) {
229 // Skip this - bad data in DB
230 log.warn(warning + " [" + s + "].", e);
236 public static String listInt(SortedSet<Integer> ll) {
237 if (ll == null || ll.size() == 0)
240 StringBuilder sb = new StringBuilder(2000);
241 Iterator<Integer> i = ll.iterator();
245 boolean first = true;
246 while (i.hasNext()) {
255 else if (start == end - 1)
256 sb.append(start).append(',').append(end);
258 sb.append(start).append('-').append(end);
270 else if (start == end - 1)
271 sb.append(start).append(',').append(end);
273 sb.append(start).append('-').append(end);
275 return sb.toString();
278 public static List<String> listStr(String s) {
279 if (s == null || s.length() == 0)
281 String[] ss = s.split(",");
282 return Arrays.asList(ss);
285 public static String listStr(Collection<String> ll) {
286 if (ll == null || ll.isEmpty())
288 StringBuilder ss = new StringBuilder(1000);
289 Iterator<String> i = ll.iterator();
292 ss.append(',').append(i.next());
293 return ss.toString();
296 private static boolean isSimple(Object o) {
300 if (o instanceof Number || o instanceof String || o instanceof Boolean || o instanceof Date)