Remove redundant type cast
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / json / JsonUtils.java
index 54a5732..bfad9ba 100644 (file)
@@ -117,12 +117,12 @@ public class JsonUtils {
         * to convert a JSONObject to an XML Doc. The intent of this is to\r
         * correctly generate XML from JSON including TAGs for JSONArrays\r
         *\r
-        * @param  jsonObj      org.json.JSON object to be converted to XML\r
+        * @param  obj object to be converted to XML\r
         * @param  tagName      optional XML tagname supplied primarily during recursive calls\r
         * @return String containing the XML translation\r
         */\r
        public static String toXMLString(Object obj, String tagName) throws JSONException {\r
-               StringBuffer strBuf = new StringBuffer();\r
+               StringBuilder strBuf = new StringBuilder();\r
                int i;\r
                JSONArray jsonArr;\r
                JSONObject jsonObj;\r
@@ -144,7 +144,7 @@ public class JsonUtils {
                        jsonObj = (JSONObject) obj;\r
                        keys = jsonObj.keys();\r
                        while (keys.hasNext()) {\r
-                               key = keys.next().toString();\r
+                               key = keys.next();\r
                                // msoLogger.debug("toXMLString(): key is " + k);\r
                                curObj = jsonObj.opt(key);\r
                                if (curObj == null) {\r
@@ -156,7 +156,7 @@ public class JsonUtils {
                                        str = null;\r
                                }\r
                                // append the content to the XML output\r
-                               if (key.equals("content")) {\r
+                               if ("content".equals(key)) {\r
                                        if (curObj instanceof JSONArray) {\r
                                                jsonArr = (JSONArray) curObj;\r
                                                len = jsonArr.length();\r
@@ -327,7 +327,7 @@ public class JsonUtils {
                                        return null;\r
                                } else {\r
                                        if (rawValue instanceof String) {\r
-                                               msoLogger.debug("getJsonValue(): the raw value is a String Object=" + ((String) rawValue).toString());\r
+                                               msoLogger.debug("getJsonValue(): the raw value is a String Object=" + rawValue);\r
                                                return (String) rawValue;\r
                                        } else {\r
                                                msoLogger.debug("getJsonValue(): the raw value is NOT a String Object=" + rawValue.toString());\r
@@ -356,7 +356,7 @@ public class JsonUtils {
                                        return null;\r
                                } else {\r
                                        if (rawValue instanceof String) {\r
-                                               msoLogger.debug("getJsonNodeValue(): the raw value is a String Object=" + ((String) rawValue).toString());\r
+                                               msoLogger.debug("getJsonNodeValue(): the raw value is a String Object=" + rawValue);\r
                                                return (String) rawValue;\r
                                        } else {\r
                                                msoLogger.debug("getJsonNodeValue(): the raw value is NOT a String Object=" + rawValue.toString());\r
@@ -388,7 +388,7 @@ public class JsonUtils {
                                        return 0;\r
                                } else {\r
                                        if (rawValue instanceof Integer) {\r
-                                               msoLogger.debug("getJsonValue(): the raw value is an Integer Object=" + ((String) rawValue).toString());\r
+                                               msoLogger.debug("getJsonValue(): the raw value is an Integer Object=" + rawValue);\r
                                                return (Integer) rawValue;\r
                                        } else {\r
                                                msoLogger.debug("getJsonValue(): the raw value is NOT an Integer Object=" + rawValue.toString());\r
@@ -410,14 +410,13 @@ public class JsonUtils {
         * @return boolean field value associated with keys - default is false\r
         */\r
        public static boolean getJsonBooleanValue(String jsonStr, String keys) {\r
-               String isDebugLogEnabled = "true";\r
                try {\r
                                Object rawValue = getJsonRawValue(jsonStr, keys);\r
                                if (rawValue == null) {\r
                                        return false;\r
                                } else {\r
                                        if (rawValue instanceof Boolean) {\r
-                                               msoLogger.debug("getJsonValue(): the raw value is a Boolean Object=" + ((String) rawValue).toString());\r
+                                               msoLogger.debug("getJsonValue(): the raw value is a Boolean Object=" + rawValue);\r
                                                return (Boolean) rawValue;\r
                                        } else {\r
                                                msoLogger.debug("getJsonValue(): the raw value is NOT an Boolean Object=" + rawValue.toString());\r
@@ -463,7 +462,7 @@ public class JsonUtils {
                                return null;\r
                        } else {\r
                                if (rawValue instanceof JSONArray) {\r
-                                       msoLogger.debug("getJsonParamValue(): keys=" + keys + " points to JSONArray: " + ((JSONArray) rawValue).toString());\r
+                                       msoLogger.debug("getJsonParamValue(): keys=" + keys + " points to JSONArray: " + rawValue.toString());\r
                                        int arrayLen = ((JSONArray) rawValue).length();\r
                                        if (index < 0 || arrayLen < index+1) {\r
                                                msoLogger.debug("getJsonParamValue(): index: " + index + " is out of bounds for array size of " + arrayLen);\r
@@ -520,9 +519,7 @@ public class JsonUtils {
 //             String isDebugLogEnabled = "true";\r
                try {\r
                        JSONObject jsonObj = new JSONObject(jsonStr);\r
-                       if (jsonObj != null) {\r
-                               return getJsonValueForKey(jsonObj, key);\r
-                       }\r
+                       return getJsonValueForKey(jsonObj, key);\r
                } catch (Exception e) {\r
                                msoLogger.debug("getJsonValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString(), e);\r
                }\r
@@ -552,7 +549,7 @@ public class JsonUtils {
                                msoLogger.debug("getJsonValueForKey(): iterating over the keys");\r
                                Iterator <String> itr = jsonObj.keys();\r
                                while (itr.hasNext()) {\r
-                                       String nextKey = (String) itr.next();\r
+                                       String nextKey = itr.next();\r
                                        Object obj = jsonObj.get(nextKey);\r
                                        if (obj instanceof JSONObject) {\r
                                                msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
@@ -590,12 +587,12 @@ public class JsonUtils {
                try {\r
                        if (jsonObj.has(key)) {\r
                                msoLogger.debug("getJsonValueForKey(): found value for key=" + key);\r
-                               return ((Integer) jsonObj.get(key));\r
+                               return (Integer) jsonObj.get(key);\r
                        } else {\r
                                msoLogger.debug("getJsonValueForKey(): iterating over the keys");\r
                                Iterator <String> itr = jsonObj.keys();\r
                                while (itr.hasNext()) {\r
-                                       String nextKey = (String) itr.next();\r
+                                       String nextKey = itr.next();\r
                                        Object obj = jsonObj.get(nextKey);\r
                                        if (obj instanceof JSONObject) {\r
                                                msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
@@ -628,17 +625,16 @@ public class JsonUtils {
         * @return String field value associated with key\r
         */\r
        public static Boolean getJsonBooleanValueForKey(JSONObject jsonObj, String key) {\r
-               String isDebugLogEnabled = "true";\r
                Boolean keyValue = false;\r
                try {\r
                        if (jsonObj.has(key)) {\r
                                msoLogger.debug("getJsonBooleanValueForKey(): found value for key=" + key);\r
-                               return ((Boolean) jsonObj.get(key));\r
+                               return (Boolean) jsonObj.get(key);\r
                        } else {\r
                                msoLogger.debug("getJsonBooleanValueForKey(): iterating over the keys");\r
                                Iterator <String> itr = jsonObj.keys();\r
                                while (itr.hasNext()) {\r
-                                       String nextKey = (String) itr.next();\r
+                                       String nextKey = itr.next();\r
                                        Object obj = jsonObj.get(nextKey);\r
                                        if (obj instanceof JSONObject) {\r
                                                msoLogger.debug("getJsonBooleanValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
@@ -694,7 +690,7 @@ public class JsonUtils {
                if (!jsonValueExists(jsonStr, keys)) {\r
                        return putJsonValue(jsonStr, keys, value);\r
                } else {\r
-                       msoLogger.debug("addJsonValue(): JSON add failed, key=" + keys + "/value=" + (String) value + " already exists");\r
+                       msoLogger.debug("addJsonValue(): JSON add failed, key=" + keys + "/value=" + value + " already exists");\r
                        return jsonStr;\r
                }\r
        }\r
@@ -830,7 +826,7 @@ public class JsonUtils {
                                                msoLogger.debug("putJsonValue(): key=" + keyStr + " points to json object");\r
                                                jsonObj = (JSONObject) keyValue;\r
                                        } else {\r
-                                               msoLogger.debug("putJsonValue(): key=" + keyStr + " not the last key but points to non-json object: " + (String) keyValue);\r
+                                               msoLogger.debug("putJsonValue(): key=" + keyStr + " not the last key but points to non-json object: " + keyValue);\r
                                                return null;\r
                                        }\r
                                } else { // at the last/new key value\r
@@ -863,7 +859,7 @@ public class JsonUtils {
        public Map<String, String> entryArrayToMap(Execution execution, String entryArray) {\r
                msoLogger.debug("Started Entry Array To Map Util Method");\r
 \r
-               Map<String, String> map = new HashMap<String, String>();\r
+               Map<String, String> map = new HashMap<>();\r
 \r
                //Populate Map\r
                String entryListJson = "{ \"entry\":" + entryArray + "}";\r
@@ -895,7 +891,7 @@ public class JsonUtils {
        public Map<String, String> entryArrayToMap(Execution execution, String entryArray, String keyNode, String valueNode) {\r
                msoLogger.debug("Started Entry Array To Map Util Method");\r
 \r
-               Map<String, String> map = new HashMap<String, String>();\r
+               Map<String, String> map = new HashMap<>();\r
                //Populate Map\r
                String entryListJson = "{ \"entry\":" + entryArray + "}";\r
                JSONObject obj = new JSONObject(entryListJson);\r
@@ -917,7 +913,7 @@ public class JsonUtils {
         * and puts it in a Java List<String>.\r
         *\r
         * @param execution\r
-        * @param stringArray - the getJsonValue of a json array of strings\r
+        * @param jsonArrayOfStrings - the getJsonValue of a json array of strings\r
         *\r
         * @return List - a java list containing the strings\r
         *\r
@@ -926,13 +922,13 @@ public class JsonUtils {
        public List<String> StringArrayToList(Execution execution, String jsonArrayOfStrings) {\r
                msoLogger.debug("Started  String Array To List Util Method");\r
 \r
-               List<String> list = new ArrayList<String>();\r
+               List<String> list = new ArrayList<>();\r
                //Populate List\r
                String stringListJson = "{ \"strings\":" + jsonArrayOfStrings + "}";\r
                JSONObject obj = new JSONObject(stringListJson);\r
                JSONArray arr = obj.getJSONArray("strings");\r
                for (int i = 0; i < arr.length(); i++){\r
-                       String s = arr.getString(i);\r
+                       String s = arr.get(i).toString();\r
                        list.add(s);\r
                }\r
                msoLogger.debug("Outgoing List is: " + list);\r
@@ -956,11 +952,9 @@ public class JsonUtils {
 \r
                try {\r
                        Object rawValue = getJsonRawValue(jsonStr, keys);\r
-                       if (rawValue == null) {\r
-                               return false;\r
-                       } else {\r
-                               return true;\r
-                       }\r
+\r
+                       return !(rawValue == null);\r
+\r
                } catch (Exception e) {\r
                                msoLogger.debug("jsonElementExist(): unable to determine if json element exist. Exception is: " + e.toString(),e);\r
                }\r