Removing unused code
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / LOGJSONObject.java
index afb0de2..1140a1c 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.
          *
@@ -149,13 +186,11 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * The map where the JSONObject's properties are kept.
      */
     private final Map<String, Object> map;
 
-
     /**
      * It is sometimes more convenient and less ambiguous to have a
      * <code>NULL</code> object than to use Java's <code>null</code> value.
@@ -164,15 +199,13 @@ public class LOGJSONObject {
      */
     public static final Object NULL = new Null();
 
-
     /**
      * Construct an empty JSONObject.
      */
     public LOGJSONObject() {
-        this.map = new LinkedHashMap<String, Object>();
+        this.map = new LinkedHashMap<>();
     }
 
-
     /**
      * Construct a JSONObject from a subset of another JSONObject.
      * An array of strings is used to identify the keys that should be copied.
@@ -180,8 +213,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,11 +220,11 @@ public class LOGJSONObject {
             try {
                 this.putOnce(names[i], jo.opt(names[i]));
             } catch (Exception ignore) {
+                intlogger.error("PROV0001 LOGJSONObject: " + ignore.getMessage(), ignore);
             }
         }
     }
 
-
     /**
      * Construct a JSONObject from a JSONTokener.
      *
@@ -201,7 +232,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;
@@ -251,7 +282,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Construct a JSONObject from a Map.
      *
@@ -273,7 +303,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Construct a JSONObject from an Object using bean getters.
      * It reflects on all of the public methods of the object.
@@ -298,7 +327,6 @@ public class LOGJSONObject {
         this.populateMap(bean);
     }
 
-
     /**
      * Construct a JSONObject from an Object, using reflection to find the
      * public members. The resulting JSONObject's keys will be the strings
@@ -323,7 +351,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Construct a JSONObject from a source JSON text string.
      * This is the most commonly used JSONObject constructor.
@@ -338,7 +365,6 @@ public class LOGJSONObject {
         this(new JSONTokener(source));
     }
 
-
     /**
      * Construct a JSONObject from a ResourceBundle.
      *
@@ -379,7 +405,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Accumulate values under a key. It is similar to the put method except
      * that if there is already an object stored under the key then a
@@ -415,7 +440,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Append values to the array under a key. If the key does not exist in the
      * JSONObject, then the key is put in the JSONObject with its value being a
@@ -442,7 +466,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Produce a string from a double. The string "null" will be returned if
      * the number is not finite.
@@ -470,7 +493,6 @@ public class LOGJSONObject {
         return string;
     }
 
-
     /**
      * Get the value object associated with a key.
      *
@@ -490,7 +512,6 @@ public class LOGJSONObject {
         return object;
     }
 
-
     /**
      * Get the boolean value associated with a key.
      *
@@ -513,7 +534,6 @@ public class LOGJSONObject {
                 "] is not a Boolean.");
     }
 
-
     /**
      * Get the double value associated with a key.
      *
@@ -522,19 +542,18 @@ 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.");
         }
     }
 
-
     /**
      * Get the int value associated with a key.
      *
@@ -543,19 +562,18 @@ 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.");
         }
     }
 
-
     /**
      * Get the JSONArray value associated with a key.
      *
@@ -573,7 +591,6 @@ public class LOGJSONObject {
                 "] is not a JSONArray.");
     }
 
-
     /**
      * Get the JSONObject value associated with a key.
      *
@@ -591,7 +608,6 @@ public class LOGJSONObject {
                 "] is not a JSONObject.");
     }
 
-
     /**
      * Get the long value associated with a key.
      *
@@ -607,12 +623,11 @@ 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.");
         }
     }
 
-
     /**
      * Get an array of field names from a JSONObject.
      *
@@ -633,30 +648,6 @@ public class LOGJSONObject {
         return names;
     }
 
-
-    /**
-     * Get an array of field names from an Object.
-     *
-     * @return An array of field names, or null if there are no names.
-     */
-    public static String[] getNames(Object object) {
-        if (object == null) {
-            return null;
-        }
-        Class<? extends Object> klass = object.getClass();
-        Field[] fields = klass.getFields();
-        int length = fields.length;
-        if (length == 0) {
-            return null;
-        }
-        String[] names = new String[length];
-        for (int i = 0; i < length; i += 1) {
-            names[i] = fields[i].getName();
-        }
-        return names;
-    }
-
-
     /**
      * Get the string associated with a key.
      *
@@ -664,7 +655,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;
@@ -673,7 +664,6 @@ public class LOGJSONObject {
                 "] not a string.");
     }
 
-
     /**
      * Determine if the JSONObject contains a specific key.
      *
@@ -684,7 +674,6 @@ public class LOGJSONObject {
         return this.map.containsKey(key);
     }
 
-
     /**
      * Increment a property of a JSONObject. If there is no such property,
      * create one with a value of 1. If there is such a property, and if
@@ -695,7 +684,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);
@@ -713,20 +702,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
-    /**
-     * Determine if the value associated with the key is null or if there is
-     * no value.
-     *
-     * @param key A key string.
-     * @return true if there is no value associated with the key or if
-     * the value is the JSONObject.NULL object.
-     */
-    public boolean isNull(String key) {
-        return LOGJSONObject.NULL.equals(this.opt(key));
-    }
-
-
     /**
      * Get an enumeration of the keys of the JSONObject.
      *
@@ -736,7 +711,6 @@ public class LOGJSONObject {
         return this.keySet().iterator();
     }
 
-
     /**
      * Get a set of keys of the JSONObject.
      *
@@ -746,7 +720,6 @@ public class LOGJSONObject {
         return this.map.keySet();
     }
 
-
     /**
      * Get the number of keys stored in the JSONObject.
      *
@@ -756,7 +729,6 @@ public class LOGJSONObject {
         return this.map.size();
     }
 
-
     /**
      * Produce a JSONArray containing the names of the elements of this
      * JSONObject.
@@ -802,7 +774,6 @@ public class LOGJSONObject {
         return string;
     }
 
-
     /**
      * Get an optional value associated with a key.
      *
@@ -813,20 +784,6 @@ public class LOGJSONObject {
         return key == null ? null : this.map.get(key);
     }
 
-
-    /**
-     * Get an optional boolean associated with a key.
-     * It returns false if there is no such key, or if the value is not
-     * Boolean.TRUE or the String "true".
-     *
-     * @param key A key string.
-     * @return The truth.
-     */
-    public boolean optBoolean(String key) {
-        return this.optBoolean(key, false);
-    }
-
-
     /**
      * Get an optional boolean associated with a key.
      * It returns the defaultValue if there is no such key, or if it is not
@@ -840,25 +797,11 @@ public class LOGJSONObject {
         try {
             return this.getBoolean(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
 
-
-    /**
-     * Get an optional double associated with a key,
-     * or NaN if there is no such key or if its value is not a number.
-     * If the value is a string, an attempt will be made to evaluate it as
-     * a number.
-     *
-     * @param key A string which is the key.
-     * @return An object which is the value.
-     */
-    public double optDouble(String key) {
-        return this.optDouble(key, Double.NaN);
-    }
-
-
     /**
      * Get an optional double associated with a key, or the
      * defaultValue if there is no such key or if its value is not a number.
@@ -873,25 +816,11 @@ public class LOGJSONObject {
         try {
             return this.getDouble(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
 
-
-    /**
-     * Get an optional int value associated with a key,
-     * or zero if there is no such key or if the value is not a number.
-     * If the value is a string, an attempt will be made to evaluate it as
-     * a number.
-     *
-     * @param key A key string.
-     * @return An object which is the value.
-     */
-    public int optInt(String key) {
-        return this.optInt(key, 0);
-    }
-
-
     /**
      * Get an optional int value associated with a key,
      * or the default if there is no such key or if the value is not a number.
@@ -906,25 +835,11 @@ public class LOGJSONObject {
         try {
             return this.getInt(key);
         } catch (Exception e) {
+            intlogger.trace("Using defaultValue: " + defaultValue, e);
             return defaultValue;
         }
     }
 
-
-    /**
-     * Get an optional JSONArray associated with a key.
-     * It returns null if there is no such key, or if its value is not a
-     * JSONArray.
-     *
-     * @param key A key string.
-     * @return A JSONArray which is the value.
-     */
-    public JSONArray optJSONArray(String key) {
-        Object o = this.opt(key);
-        return o instanceof JSONArray ? (JSONArray) o : null;
-    }
-
-
     /**
      * Get an optional JSONObject associated with a key.
      * It returns null if there is no such key, or if its value is not a
@@ -938,21 +853,6 @@ public class LOGJSONObject {
         return object instanceof LOGJSONObject ? (LOGJSONObject) object : null;
     }
 
-
-    /**
-     * Get an optional long value associated with a key,
-     * or zero if there is no such key or if the value is not a number.
-     * If the value is a string, an attempt will be made to evaluate it as
-     * a number.
-     *
-     * @param key A key string.
-     * @return An object which is the value.
-     */
-    public long optLong(String key) {
-        return this.optLong(key, 0);
-    }
-
-
     /**
      * Get an optional long value associated with a key,
      * or the default if there is no such key or if the value is not a number.
@@ -971,20 +871,6 @@ public class LOGJSONObject {
         }
     }
 
-
-    /**
-     * Get an optional string associated with a key.
-     * It returns an empty string if there is no such key. If the value is not
-     * a string and is not null, then it is converted to a string.
-     *
-     * @param key A key string.
-     * @return A string which is the value.
-     */
-    public String optString(String key) {
-        return this.optString(key, "");
-    }
-
-
     /**
      * Get an optional string associated with a key.
      * It returns the defaultValue if there is no such key.
@@ -998,7 +884,6 @@ public class LOGJSONObject {
         return NULL.equals(object) ? defaultValue : object.toString();
     }
 
-
     private void populateMap(Object bean) {
         Class<? extends Object> klass = bean.getClass();
 
@@ -1042,11 +927,11 @@ public class LOGJSONObject {
                     }
                 }
             } catch (Exception ignore) {
+                intlogger.trace("populateMap: " + ignore.getMessage(), ignore);
             }
         }
     }
 
-
     /**
      * Put a key/boolean pair in the JSONObject.
      *
@@ -1060,7 +945,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject, where the value will be a
      * JSONArray which is produced from a Collection.
@@ -1075,7 +959,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/double pair in the JSONObject.
      *
@@ -1089,7 +972,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/int pair in the JSONObject.
      *
@@ -1103,7 +985,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/long pair in the JSONObject.
      *
@@ -1117,7 +998,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject, where the value will be a
      * JSONObject which is produced from a Map.
@@ -1132,7 +1012,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject. If the value is null,
      * then the key will be removed from the JSONObject if it is present.
@@ -1168,7 +1047,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject, but only if the key and the
      * value are both non-null, and only if there is not already a member
@@ -1189,7 +1067,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Put a key/value pair in the JSONObject, but only if the
      * key and the value are both non-null.
@@ -1208,7 +1085,6 @@ public class LOGJSONObject {
         return this;
     }
 
-
     /**
      * Produce a string in double quotes with backslash sequences in all the
      * right places. A backslash will be inserted within </, producing <\/,
@@ -1223,8 +1099,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,20 +1223,20 @@ public class LOGJSONObject {
                         return myLong;
                     }
                 }
-            } catch (Exception ignore) {
+            } catch (Exception e) {
+                intlogger.trace("Ignore Exception message: ", e);
             }
         }
         return string;
     }
 
-
     /**
      * Throw an exception if the object is a NaN or infinite number.
      *
      * @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()) {
@@ -1376,7 +1252,6 @@ public class LOGJSONObject {
         }
     }
 
-
     /**
      * Produce a JSONArray containing the values of the members of this
      * JSONObject.
@@ -1413,11 +1288,11 @@ public class LOGJSONObject {
         try {
             return this.toString(0);
         } catch (Exception e) {
-            return null;
+            intlogger.trace("Exception: ", e);
+            return "";
         }
     }
 
-
     /**
      * Make a prettyprinted JSON text of this JSONObject.
      * <p>
@@ -1462,7 +1337,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,29 +1421,15 @@ public class LOGJSONObject {
             }
             return new LOGJSONObject(object);
         } catch (Exception exception) {
+            intlogger.trace("Exception: ", exception);
             return null;
         }
     }
 
-
-    /**
-     * Write the contents of the JSONObject as JSON text to a writer.
-     * For compactness, no whitespace is added.
-     * <p>
-     * Warning: This method assumes that the data structure is acyclical.
-     *
-     * @return The writer.
-     * @throws JSONException
-     */
-    public Writer write(Writer writer) throws JSONException {
-        return this.write(writer, 0, 0);
-    }
-
-
     @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);