Remove critical code smells for utils classes
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / LOGJSONObject.java
index afb0de2..f7e0874 100644 (file)
@@ -23,20 +23,15 @@ package org.onap.dmaap.datarouter.provisioning.utils;
  * *
  ******************************************************************************/
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.LinkedHashMap;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Map;
-import java.util.ResourceBundle;
-import java.util.Set;
+import java.util.*;
 
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -111,6 +106,8 @@ public class LOGJSONObject {
      */
     private static Map<String, Object> keyPool = new LinkedHashMap<String, Object>(keyPoolSize);
 
+    private static final EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
+
     /**
      * JSONObject.NULL is equivalent to the value that JavaScript calls null,
      * whilst Java's null is equivalent to the value that JavaScript calls
@@ -139,6 +136,46 @@ public class LOGJSONObject {
             return object == null || object == this;
         }
 
+        /**
+         * Returns a hash code value for the object. This method is
+         * supported for the benefit of hash tables such as those provided by
+         * {@link HashMap}.
+         * <p>
+         * The general contract of {@code hashCode} is:
+         * <ul>
+         * <li>Whenever it is invoked on the same object more than once during
+         * an execution of a Java application, the {@code hashCode} method
+         * must consistently return the same integer, provided no information
+         * used in {@code equals} comparisons on the object is modified.
+         * This integer need not remain consistent from one execution of an
+         * application to another execution of the same application.
+         * <li>If two objects are equal according to the {@code equals(Object)}
+         * method, then calling the {@code hashCode} method on each of
+         * the two objects must produce the same integer result.
+         * <li>It is <em>not</em> required that if two objects are unequal
+         * according to the {@link Object#equals(Object)}
+         * method, then calling the {@code hashCode} method on each of the
+         * two objects must produce distinct integer results.  However, the
+         * programmer should be aware that producing distinct integer results
+         * for unequal objects may improve the performance of hash tables.
+         * </ul>
+         * <p>
+         * As much as is reasonably practical, the hashCode method defined by
+         * class {@code Object} does return distinct integers for distinct
+         * objects. (This is typically implemented by converting the internal
+         * address of the object into an integer, but this implementation
+         * technique is not required by the
+         * Java&trade; programming language.)
+         *
+         * @return a hash code value for this object.
+         * @see Object#equals(Object)
+         * @see System#identityHashCode
+         */
+        @Override
+        public int hashCode() {
+            return super.hashCode();
+        }
+
         /**
          * Get the "null" string value.
          *
@@ -169,7 +206,7 @@ public class LOGJSONObject {
      * Construct an empty JSONObject.
      */
     public LOGJSONObject() {
-        this.map = new LinkedHashMap<String, Object>();
+        this.map = new LinkedHashMap<>();
     }
 
 
@@ -180,8 +217,6 @@ public class LOGJSONObject {
      *
      * @param jo    A JSONObject.
      * @param names An array of strings.
-     * @throws JSONException
-     * @throws JSONException If a value is a non-finite number or if a name is duplicated.
      */
     public LOGJSONObject(LOGJSONObject jo, String[] names) {
         this();
@@ -189,6 +224,7 @@ public class LOGJSONObject {
             try {
                 this.putOnce(names[i], jo.opt(names[i]));
             } catch (Exception ignore) {
+                intlogger.error("PROV0001 LOGJSONObject: " + ignore.getMessage(), ignore);
             }
         }
     }
@@ -201,7 +237,7 @@ public class LOGJSONObject {
      * @throws JSONException If there is a syntax error in the source string
      *                       or a duplicated key.
      */
-    public LOGJSONObject(JSONTokener x) throws JSONException {
+    public LOGJSONObject(JSONTokener x) {
         this();
         char c;
         String key;
@@ -522,15 +558,15 @@ public class LOGJSONObject {
      * @throws JSONException if the key is not found or
      *                       if the value is not a Number object and cannot be converted to a number.
      */
-    public double getDouble(String key) throws JSONException {
+    public double getDouble(String key) {
         Object object = this.get(key);
         try {
             return object instanceof Number
                     ? ((Number) object).doubleValue()
                     : Double.parseDouble((String) object);
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key) +
-                    "] is not a number.");
+            intlogger.error("JSONObject[" + quote(key) + "] is not a number.", e);
+            throw new JSONException("JSONObject[" + quote(key) + "] is not a number.");
         }
     }
 
@@ -543,15 +579,15 @@ public class LOGJSONObject {
      * @throws JSONException if the key is not found or if the value cannot
      *                       be converted to an integer.
      */
-    public int getInt(String key) throws JSONException {
+    public int getInt(String key) {
         Object object = this.get(key);
         try {
             return object instanceof Number
                     ? ((Number) object).intValue()
                     : Integer.parseInt((String) object);
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key) +
-                    "] is not an int.");
+            intlogger.error("JSONObject[" + quote(key) + "] is not an int.", e);
+            throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
         }
     }
 
@@ -607,8 +643,8 @@ public class LOGJSONObject {
                     ? ((Number) object).longValue()
                     : Long.parseLong((String) object);
         } catch (Exception e) {
-            throw new JSONException("JSONObject[" + quote(key) +
-                    "] is not a long.");
+            intlogger.error("JSONObject[" + quote(key) + "] is not a long.", e);
+            throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
         }
     }
 
@@ -664,7 +700,7 @@ public class LOGJSONObject {
      * @return A string which is the value.
      * @throws JSONException if there is no string value for the key.
      */
-    public String getString(String key) throws JSONException {
+    public String getString(String key) {
         Object object = this.get(key);
         if (object instanceof String) {
             return (String) object;
@@ -695,7 +731,7 @@ public class LOGJSONObject {
      * @throws JSONException If there is already a property with this name
      *                       that is not an Integer, Long, Double, or Float.
      */
-    public LOGJSONObject increment(String key) throws JSONException {
+    public LOGJSONObject increment(String key) {
         Object value = this.opt(key);
         if (value == null) {
             this.put(key, 1);
@@ -840,6 +876,7 @@ public class LOGJSONObject {
         try {
             return this.getBoolean(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
@@ -873,6 +910,7 @@ public class LOGJSONObject {
         try {
             return this.getDouble(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
@@ -906,6 +944,7 @@ public class LOGJSONObject {
         try {
             return this.getInt(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
@@ -1042,6 +1081,7 @@ public class LOGJSONObject {
                     }
                 }
             } catch (Exception ignore) {
+                intlogger.trace("populateMap: " + ignore.getMessage(), ignore);
             }
         }
     }
@@ -1223,8 +1263,8 @@ public class LOGJSONObject {
         synchronized (sw.getBuffer()) {
             try {
                 return quote(string, sw).toString();
-            } catch (IOException ignored) {
-                // will never happen - we are writing to a string writer
+            } catch (IOException e) {
+                intlogger.trace("Ignore Exception message: ", e);
                 return "";
             }
         }
@@ -1347,7 +1387,8 @@ public class LOGJSONObject {
                         return myLong;
                     }
                 }
-            } catch (Exception ignore) {
+            } catch (Exception e) {
+                intlogger.trace("Ignore Exception message: ", e);
             }
         }
         return string;
@@ -1360,7 +1401,7 @@ public class LOGJSONObject {
      * @param o The object to test.
      * @throws JSONException If o is a non-finite number.
      */
-    public static void testValidity(Object o) throws JSONException {
+    public static void testValidity(Object o) {
         if (o != null) {
             if (o instanceof Double) {
                 if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
@@ -1413,7 +1454,8 @@ public class LOGJSONObject {
         try {
             return this.toString(0);
         } catch (Exception e) {
-            return null;
+            intlogger.trace("Exception: ", e);
+            return "";
         }
     }
 
@@ -1462,7 +1504,7 @@ public class LOGJSONObject {
      */
     @SuppressWarnings("unchecked")
     public static String valueToString(Object value) throws JSONException {
-        if (value == null || value.equals(null)) {
+        if (value == null) {
             return "null";
         }
         if (value instanceof JSONString) {
@@ -1546,6 +1588,7 @@ public class LOGJSONObject {
             }
             return new LOGJSONObject(object);
         } catch (Exception exception) {
+            intlogger.trace("Exception: ", exception);
             return null;
         }
     }
@@ -1568,7 +1611,7 @@ public class LOGJSONObject {
     @SuppressWarnings("unchecked")
     static final Writer writeValue(Writer writer, Object value,
                                    int indentFactor, int indent) throws JSONException, IOException {
-        if (value == null || value.equals(null)) {
+        if (value == null) {
             writer.write("null");
         } else if (value instanceof LOGJSONObject) {
             ((LOGJSONObject) value).write(writer, indentFactor, indent);