added svcapi ui and camunda code
[it/otf.git] / otf-service-api / 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 \r
22 import java.lang.reflect.Field;\r
23 import java.util.ArrayList;\r
24 import java.util.Arrays;\r
25 import java.util.Collection;\r
26 import java.util.HashMap;\r
27 import java.util.List;\r
28 import java.util.Map;\r
29 import java.util.UUID;\r
30 \r
31 public class Utility {\r
32 \r
33   public static String getLoggerPrefix() {\r
34     return "[" + Thread.currentThread().getStackTrace()[2].getMethodName() + "]: ";\r
35   }\r
36 \r
37   public static Map<?, ?> toMap(Object obj) throws Exception {\r
38     ObjectMapper mapper = new ObjectMapper();\r
39     return mapper.convertValue(obj, HashMap.class);\r
40   }\r
41 \r
42   public static boolean isCollection(Object obj) {\r
43     return obj.getClass().isArray() || obj instanceof Collection;\r
44   }\r
45 \r
46   public static List<?> toList(Object obj) {\r
47     if (obj == null) {\r
48       throw new NullPointerException("Argument cannot be null.");\r
49     }\r
50 \r
51     List<?> list = new ArrayList<>();\r
52     if (obj.getClass().isArray()) {\r
53       list = Arrays.asList((Object[]) obj);\r
54     } else if (obj instanceof Collection) {\r
55       list = new ArrayList<>((Collection<?>) obj);\r
56     }\r
57 \r
58     return list;\r
59   }\r
60 \r
61   public static boolean isValidUuid(String str) {\r
62     if (Strings.isNullOrEmpty(str)) {\r
63       return false;\r
64     }\r
65     try {\r
66       UUID uuid = UUID.fromString(str);\r
67       return uuid.toString().equalsIgnoreCase(str);\r
68     } catch (IllegalArgumentException iae) {\r
69       return false;\r
70     }\r
71   }\r
72 \r
73   // check a name type pair to see if it matches field in class\r
74   public static boolean isTypeVariablePairInClass(String variableName, Object variableValue, Class javaClass){\r
75     List<Field> testHeadFields = Arrays.asList(javaClass.getFields());\r
76     for(int i = 0; i < testHeadFields.size(); i++){\r
77       Field field = testHeadFields.get(i);\r
78       if(field.getName().equals(variableName) && field.getType().isInstance(variableValue)){\r
79         return true;\r
80       }\r
81     }\r
82     return false;\r
83   }\r
84 }\r