Class ObjectUtils


  • public final class ObjectUtils
    extends java.lang.Object
    Contains utility functions for working with the Object type
    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static int binarySearch​(double[] array, double key, int startIndex, int endIndex, int missingOffset)
      Fastest possible binary search.
      static int binarySearch​(float[] array, float key, int startIndex, int endIndex, int missingOffset)
      Fastest possible binary search.
      static int binarySearch​(int[] array, int key, int startIndex, int endIndex, int missingOffset)
      Fastest possible binary search.
      static int binarySearch​(long[] array, long key, int startIndex, int endIndex, int missingOffset)
      Fastest possible binary search.
      static <T extends java.lang.Comparable<T>>
      int
      binarySearch​(T[] array, T key, int startIndex, int endIndex, int missingOffset)
      Fastest possible binary search.
      static java.util.Date clone​(java.util.Date value)
      Null safe clone of a date.
      static <T> int compare​(java.lang.Comparable<T> a, T b)
      Null safe equivalent of a.compareTo(b)
      static int compareTo​(java.lang.Comparable a, java.lang.Comparable b)
      Deprecated.
      Use #compare(Comparable, Comparable) instead.
      static <T> java.util.List<T> copyOf​(java.util.List<T> value)
      Null safe copy of a list.
      static boolean equals​(double a, double b)
      Returns true if a is equivalent to b.
      static boolean equals​(float a, float b)
      Returns true if a is equivalent to b.
      static boolean equals​(java.lang.Object a, java.lang.Object b)
      Returns true if a is equivalent to b.
      static boolean equals​(java.lang.Throwable a, java.lang.Throwable b)
      Compares equality of 2 Throwables.
      static boolean equals​(java.util.List<? extends java.lang.Throwable> a, java.util.List<? extends java.lang.Throwable> b)
      Compares if 2 lists of Throwables are equivalent.
      static <T extends java.lang.Throwable>
      T
      getCause​(java.lang.Throwable ex, java.lang.Class<T> type)
      Returns the exception of 'type' if anywhere in the exception's cause tree a class of 'type' is found, null otherwise
      static boolean getDefault​(java.lang.Boolean value, boolean defaultValue)
      Returns defaultValue if value is null, value otherwise.
      static double getDefault​(java.lang.Double value, double defaultValue)
      Returns defaultValue if value is null, value otherwise.
      static float getDefault​(java.lang.Float value, float defaultValue)
      Returns defaultValue if value is null, value otherwise.
      static int getDefault​(java.lang.Integer value, int defaultValue)
      Returns defaultValue if value is null, value otherwise.
      static long getDefault​(java.lang.Long value, long defaultValue)
      Returns defaultValue if value is null, value otherwise.
      static <T> T getDefault​(T value, T defaultValue)
      Returns defaultValue if value is null, value otherwise.
      static int hashCode​(boolean value)
      Compute the hash code of a primitive boolean.
      static int hashCode​(double value)
      Compute the hash code of a primitive double.
      static int hashCode​(double[] data, int start, int count)
      Compute the hash code of a segment of an int[].
      static int hashCode​(float value)
      Compute the hash code of a primitive float.
      static int hashCode​(int[] data, int start, int count)
      Compute the hash code of a segment of an int[].
      static int hashCode​(int hash, boolean value)
      Add the hashCode for value to hash.
      static int hashCode​(int hash, double value)
      Add the hashCode for value to hash.
      static int hashCode​(int hash, float value)
      Add the hashCode for value to hash.
      static int hashCode​(int hash, int value)
      Add the hashCode for value to hash.
      static int hashCode​(int hash, long value)
      Add the hashCode for value to hash.
      static int hashCode​(int hash, java.lang.Object value)
      Add the hashCode for value to hash.
      static int hashCode​(long value)
      Compute the hash code of a primitive long.
      static int hashCode​(java.lang.Class<?> clazz)
      Compute the hash code of a Class by using the hashcode of Class.getName().
      static int hashCode​(java.lang.Object value)
      Returns the value's hash code or 0 if null.
      static int hashCode​(java.util.List<?> list)
      Compute the hash code of a list of objects, using our internal routines when possible.
      static java.lang.String intern​(java.lang.String value)
      Intern and return value (handles nulls).
      static boolean isFalse​(java.lang.String value)
      Returns true if the string value is a boolean value indicating false.
      static boolean isTrue​(java.lang.String value)
      Returns true if the string value is a boolean value indicating true.
      static <T> java.util.List<T> newList​(T... vals)
      Creates a new List implementation based on ordered input values.
      static <K,​V>
      java.util.Map<K,​V>
      newMap​(K key, V val, java.lang.Object... args)
      Creates a new LinkedHashMap implementation based on key value pairs specified as paired arguments.
      static java.lang.Boolean parseBoolean​(java.lang.Object value)
      Convert value to a Boolean (or null if not parseable).
      static java.lang.Boolean parseBoolean​(java.lang.String value)
      Parse a boolean value (more lenient than Boolean.valueOf()).
      static java.math.BigDecimal parseDecimal​(java.lang.Object value)
      Parse an arbitrary object as a BigDecimal.
      static double parseDouble​(java.lang.Object value)
      Parse an arbitrary object as a double.
      static float parseFloat​(java.lang.Object value)
      Parse an arbitrary object as a float.
      static int parseInt​(java.lang.Object value)
      Parse an arbitrary object as an integer.
      static long parseLong​(java.lang.Object value)
      Parse an arbitrary object as a long.
      static int parseUnsigned​(java.lang.Object value)
      Parses an arbitrary object into an unsigned integer.
      static void prettyPrint​(int[][] matrix)
      Pretty prints a 2-D int array
      static <T> void prettyPrint​(T[][] matrix)
      Pretty prints a 2-D object array
      static <T> int size​(java.util.List<java.util.List<T>> matrix)
      Returns the total number of cells in a matrix
      static <T> int size​(T[][] matrix)
      Returns the total number of cells in a matrix
      static <T> T[] toArray​(T... args)
      Autobox-like creating of array.
      static boolean toBooleanValue​(java.lang.Object value, boolean defaultValue)
      Converts an arbitrary object to a primitive boolean.
      static double toDoubleValue​(java.lang.Object value, double defaultValue)
      Converts an arbitrary object to a primitive double.
      static float toFloatValue​(java.lang.Object value, float defaultValue)
      Converts an arbitrary object to a primitive float.
      static int toIntValue​(java.lang.Object value, int defaultValue)
      Converts an arbitrary object to a primitive integer.
      static long toLongValue​(java.lang.Object value, long defaultValue)
      Converts an arbitrary object to a primitive long.
      static <T extends java.lang.Comparable<? super T>>
      java.util.List<T>
      toSortedList​(java.util.Collection<T> collec)
      Converts an arbitrary collection into a sorted list.
      static java.lang.String toStringValue​(java.lang.Object value, java.lang.String defaultValue)
      Converts an arbitrary object to a String.
      static int toUnsignedValue​(java.lang.Object value, int defaultValue)
      Converts an arbitrary object to a primitive unsigned integer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ObjectUtils

        public ObjectUtils()
    • Method Detail

      • getDefault

        public static <T> T getDefault​(T value,
                                       T defaultValue)
        Returns defaultValue if value is null, value otherwise.
      • intern

        public static java.lang.String intern​(java.lang.String value)
        Intern and return value (handles nulls).
      • getDefault

        public static boolean getDefault​(java.lang.Boolean value,
                                         boolean defaultValue)
        Returns defaultValue if value is null, value otherwise.
      • getDefault

        public static int getDefault​(java.lang.Integer value,
                                     int defaultValue)
        Returns defaultValue if value is null, value otherwise.
      • getDefault

        public static long getDefault​(java.lang.Long value,
                                      long defaultValue)
        Returns defaultValue if value is null, value otherwise.
      • getDefault

        public static float getDefault​(java.lang.Float value,
                                       float defaultValue)
        Returns defaultValue if value is null, value otherwise.
      • getDefault

        public static double getDefault​(java.lang.Double value,
                                        double defaultValue)
        Returns defaultValue if value is null, value otherwise.
      • toArray

        public static <T> T[] toArray​(T... args)
        Autobox-like creating of array.
      • binarySearch

        public static int binarySearch​(int[] array,
                                       int key,
                                       int startIndex,
                                       int endIndex,
                                       int missingOffset)
        Fastest possible binary search.

        WARNING: no bounds checking is performed.

        Parameters:
        array - the array to search.
        key - the key to search for.
        startIndex - the start index to start looking at (inclusive).
        endIndex - the end index to stop looking at (inclusive).
        Returns:
        the index where key (or the greatest value less than key) is located.
      • binarySearch

        public static int binarySearch​(long[] array,
                                       long key,
                                       int startIndex,
                                       int endIndex,
                                       int missingOffset)
        Fastest possible binary search.

        WARNING: no bounds checking is performed.

        Parameters:
        array - the array to search.
        key - the key to search for.
        startIndex - the start index to start looking at (inclusive).
        endIndex - the end index to stop looking at (inclusive).
        Returns:
        the index where key (or the greatest value less than key) is located.
      • binarySearch

        public static int binarySearch​(float[] array,
                                       float key,
                                       int startIndex,
                                       int endIndex,
                                       int missingOffset)
        Fastest possible binary search.

        WARNING: no bounds checking is performed.

        Parameters:
        array - the array to search.
        key - the key to search for.
        startIndex - the start index to start looking at (inclusive).
        endIndex - the end index to stop looking at (inclusive).
        Returns:
        the index where key (or the greatest value less than key) is located.
      • binarySearch

        public static int binarySearch​(double[] array,
                                       double key,
                                       int startIndex,
                                       int endIndex,
                                       int missingOffset)
        Fastest possible binary search.

        WARNING: no bounds checking is performed.

        Parameters:
        array - the array to search.
        key - the key to search for.
        startIndex - the start index to start looking at (inclusive).
        endIndex - the end index to stop looking at (inclusive).
        Returns:
        the index where key (or the greatest value less than key) is located.
      • clone

        public static java.util.Date clone​(java.util.Date value)
        Null safe clone of a date.
      • copyOf

        public static <T> java.util.List<T> copyOf​(java.util.List<T> value)
        Null safe copy of a list.

        NOTE: this is a shallow clone.

      • binarySearch

        public static <T extends java.lang.Comparable<T>> int binarySearch​(T[] array,
                                                                           T key,
                                                                           int startIndex,
                                                                           int endIndex,
                                                                           int missingOffset)
        Fastest possible binary search.

        WARNING: no bounds checking is performed.

        Parameters:
        array - the array to search.
        key - the key to search for.
        startIndex - the start index to start looking at (inclusive).
        endIndex - the end index to stop looking at (inclusive).
        Returns:
        the index where key (or the greatest value less than key) is located.
      • size

        public static <T> int size​(T[][] matrix)
        Returns the total number of cells in a matrix
        Parameters:
        matrix - the matrix
        Returns:
        the total number of cells
      • size

        public static <T> int size​(java.util.List<java.util.List<T>> matrix)
        Returns the total number of cells in a matrix
        Parameters:
        matrix - the matrix
        Returns:
        the total number of cells
      • equals

        public static boolean equals​(java.lang.Object a,
                                     java.lang.Object b)
        Returns true if a is equivalent to b.
        Parameters:
        a - left hand side
        b - right hand side
        Returns:
        true if a is equivalent to b
      • equals

        public static boolean equals​(float a,
                                     float b)
        Returns true if a is equivalent to b.
        Parameters:
        a - left hand side
        b - right hand side
        Returns:
        true if a is equivalent to b
      • equals

        public static boolean equals​(double a,
                                     double b)
        Returns true if a is equivalent to b.
        Parameters:
        a - left hand side
        b - right hand side
        Returns:
        true if a is equivalent to b
      • equals

        public static boolean equals​(java.util.List<? extends java.lang.Throwable> a,
                                     java.util.List<? extends java.lang.Throwable> b)
        Compares if 2 lists of Throwables are equivalent.

        Uses equals(Throwable, Throwable) for comparing elements.

      • equals

        public static boolean equals​(java.lang.Throwable a,
                                     java.lang.Throwable b)
        Compares equality of 2 Throwables.

        Equality check will validate a and b are of the same type and have same message.

      • compareTo

        @Deprecated
        public static int compareTo​(java.lang.Comparable a,
                                    java.lang.Comparable b)
        Deprecated.
        Use #compare(Comparable, Comparable) instead.
        Return the output of a.compareTo(b)

        Safe if a or b is null

        Parameters:
        a - left hand side
        b - right hand side
        Returns:
        true if a is equivalent to b
      • compare

        public static <T> int compare​(java.lang.Comparable<T> a,
                                      T b)
        Null safe equivalent of a.compareTo(b)
      • toSortedList

        public static <T extends java.lang.Comparable<? super T>> java.util.List<T> toSortedList​(java.util.Collection<T> collec)
        Converts an arbitrary collection into a sorted list.
        Parameters:
        collec - the collection to convert
        Returns:
        the sorted list
      • toStringValue

        public static java.lang.String toStringValue​(java.lang.Object value,
                                                     java.lang.String defaultValue)
        Converts an arbitrary object to a String.
        Parameters:
        value - the value to convert
        defaultValue - if value is null
        Returns:
        the value as a String, or defaultValue if conversion fails.
      • parseInt

        public static int parseInt​(java.lang.Object value)
        Parse an arbitrary object as an integer.
      • parseLong

        public static long parseLong​(java.lang.Object value)
        Parse an arbitrary object as a long.
      • parseFloat

        public static float parseFloat​(java.lang.Object value)
        Parse an arbitrary object as a float.
      • parseDouble

        public static double parseDouble​(java.lang.Object value)
        Parse an arbitrary object as a double.
      • parseDecimal

        public static java.math.BigDecimal parseDecimal​(java.lang.Object value)
        Parse an arbitrary object as a BigDecimal.
      • toIntValue

        public static int toIntValue​(java.lang.Object value,
                                     int defaultValue)
        Converts an arbitrary object to a primitive integer.
        Parameters:
        value - the value to convert
        defaultValue - if value cannot be converted, this will be returned
        Returns:
        the value as a int, or defaultValue if conversion fails.
      • toUnsignedValue

        public static int toUnsignedValue​(java.lang.Object value,
                                          int defaultValue)
        Converts an arbitrary object to a primitive unsigned integer.
        Parameters:
        value - the value to convert
        defaultValue - if value cannot be parsed, this will be returned
        Returns:
        the value as a int, or defaultValue if conversion fails.
      • parseUnsigned

        public static int parseUnsigned​(java.lang.Object value)
                                 throws java.lang.NumberFormatException,
                                        java.lang.IllegalArgumentException
        Parses an arbitrary object into an unsigned integer.
        Parameters:
        value - the value to convert
        Returns:
        the value as an int
        Throws:
        java.lang.NumberFormatException - if object cannot be converted to int
        java.lang.IllegalArgumentException - if integer value is negative
      • toLongValue

        public static long toLongValue​(java.lang.Object value,
                                       long defaultValue)
        Converts an arbitrary object to a primitive long.
        Parameters:
        value - the value to convert
        defaultValue - if value cannot be converted, this will be returned
        Returns:
        the value as a long, or defaultValue if conversion fails.
      • toFloatValue

        public static float toFloatValue​(java.lang.Object value,
                                         float defaultValue)
        Converts an arbitrary object to a primitive float.
        Parameters:
        value - the value to convert
        defaultValue - if value cannot be converted, this will be returned
        Returns:
        the value as a float, or defaultValue if conversion fails.
      • toDoubleValue

        public static double toDoubleValue​(java.lang.Object value,
                                           double defaultValue)
        Converts an arbitrary object to a primitive double.
        Parameters:
        value - the value to convert
        defaultValue - if value cannot be converted, this will be returned
        Returns:
        the value as a double, or defaultValue if conversion fails.
      • toBooleanValue

        public static boolean toBooleanValue​(java.lang.Object value,
                                             boolean defaultValue)
        Converts an arbitrary object to a primitive boolean.

        If value is a Boolean instance, return its value. If value is a Number instance, return true if it is non-zero. true will be returned if the value's lower cased string representation is true, yes, or on. false will be returned if values' lower cased string representation is false, no, or off. non-zero numbers will be return true, 0 returns false. Otherwise, the defaultValue will be returned.

        Parameters:
        value - the value to convert
        defaultValue - if value cannot be converted, this will be returned
        Returns:
        the value as a boolean, or defaultValue if conversion fails.
      • isTrue

        public static boolean isTrue​(java.lang.String value)
        Returns true if the string value is a boolean value indicating true.
      • isFalse

        public static boolean isFalse​(java.lang.String value)
        Returns true if the string value is a boolean value indicating false.
      • parseBoolean

        public static java.lang.Boolean parseBoolean​(java.lang.String value)
        Parse a boolean value (more lenient than Boolean.valueOf()).

        true will be returned if the value's lower cased string representation is true, yes, or on. false will be returned if values' lower cased string representation is false, no, or off. non-zero numbers will be return true, 0 returns false.

        Parameters:
        value -
        Returns:
        the value or null if not parseable as a boolean
      • parseBoolean

        public static java.lang.Boolean parseBoolean​(java.lang.Object value)
        Convert value to a Boolean (or null if not parseable).
      • prettyPrint

        public static void prettyPrint​(int[][] matrix)
        Pretty prints a 2-D int array
      • prettyPrint

        public static <T> void prettyPrint​(T[][] matrix)
        Pretty prints a 2-D object array
      • getCause

        public static <T extends java.lang.Throwable> T getCause​(java.lang.Throwable ex,
                                                                 java.lang.Class<T> type)
        Returns the exception of 'type' if anywhere in the exception's cause tree a class of 'type' is found, null otherwise
      • newMap

        public static <K,​V> java.util.Map<K,​V> newMap​(K key,
                                                                  V val,
                                                                  java.lang.Object... args)
                                                           throws java.lang.IllegalArgumentException
        Creates a new LinkedHashMap implementation based on key value pairs specified as paired arguments.

        The first key/value are used for compiler time type safety and are put into the map. All of the other arguments are NOT validated for type safety.

        Throws:
        java.lang.IllegalArgumentException - if an odd number of arguments are specified
      • newList

        @SafeVarargs
        public static <T> java.util.List<T> newList​(T... vals)
        Creates a new List implementation based on ordered input values. Same as Arrays.asList() but provides type safety
        Type Parameters:
        T - the type of generic list to create
        Parameters:
        vals - the values to add to the list
      • hashCode

        public static int hashCode​(boolean value)
        Compute the hash code of a primitive boolean.
      • hashCode

        public static int hashCode​(float value)
        Compute the hash code of a primitive float.
      • hashCode

        public static int hashCode​(double value)
        Compute the hash code of a primitive double.
      • hashCode

        public static int hashCode​(long value)
        Compute the hash code of a primitive long.
      • hashCode

        public static int hashCode​(java.lang.Class<?> clazz)
        Compute the hash code of a Class by using the hashcode of Class.getName(). Null safe.
      • hashCode

        public static int hashCode​(java.util.List<?> list)
        Compute the hash code of a list of objects, using our internal routines when possible.
      • hashCode

        public static int hashCode​(int[] data,
                                   int start,
                                   int count)
        Compute the hash code of a segment of an int[].
      • hashCode

        public static int hashCode​(double[] data,
                                   int start,
                                   int count)
        Compute the hash code of a segment of an int[].
      • hashCode

        public static int hashCode​(int hash,
                                   boolean value)
        Add the hashCode for value to hash.
      • hashCode

        public static int hashCode​(int hash,
                                   float value)
        Add the hashCode for value to hash.
      • hashCode

        public static int hashCode​(int hash,
                                   double value)
        Add the hashCode for value to hash.
      • hashCode

        public static int hashCode​(int hash,
                                   long value)
        Add the hashCode for value to hash.
      • hashCode

        public static int hashCode​(int hash,
                                   java.lang.Object value)
        Add the hashCode for value to hash.
      • hashCode

        public static int hashCode​(java.lang.Object value)
        Returns the value's hash code or 0 if null.
      • hashCode

        public static int hashCode​(int hash,
                                   int value)
        Add the hashCode for value to hash.