added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / common / utility / Utility.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 package org.oran.otf.common.utility;\r
18 \r
19 import com.fasterxml.jackson.databind.ObjectMapper;\r
20 import com.google.common.base.Strings;\r
21 import java.util.ArrayList;\r
22 import java.util.Arrays;\r
23 import java.util.Collection;\r
24 import java.util.HashMap;\r
25 import java.util.List;\r
26 import java.util.Map;\r
27 import java.util.UUID;\r
28 \r
29 public class Utility {\r
30 \r
31   public static String getLoggerPrefix() {\r
32     return "[" + Thread.currentThread().getStackTrace()[2].getMethodName() + "]: ";\r
33   }\r
34 \r
35   public static Map<?, ?> toMap(Object obj) throws Exception {\r
36     ObjectMapper mapper = new ObjectMapper();\r
37     return mapper.convertValue(obj, HashMap.class);\r
38   }\r
39 \r
40   public static boolean isCollection(Object obj) {\r
41     return obj.getClass().isArray() || obj instanceof Collection;\r
42   }\r
43 \r
44   public static List<?> toList(Object obj) {\r
45     if (obj == null) {\r
46       throw new NullPointerException("Argument cannot be null.");\r
47     }\r
48 \r
49     List<?> list = new ArrayList<>();\r
50     if (obj.getClass().isArray()) {\r
51       list = Arrays.asList((Object[]) obj);\r
52     } else if (obj instanceof Collection) {\r
53       list = new ArrayList<>((Collection<?>) obj);\r
54     }\r
55 \r
56     return list;\r
57   }\r
58 \r
59   public static boolean isValidUuid(String str) {\r
60     if (Strings.isNullOrEmpty(str)) {\r
61       return false;\r
62     }\r
63     try {\r
64       UUID uuid = UUID.fromString(str);\r
65       return uuid.toString().equalsIgnoreCase(str);\r
66     } catch (IllegalArgumentException iae) {\r
67       return false;\r
68     }\r
69   }\r
70 }\r