Class Objects


  • public class Objects
    extends java.lang.Object
    Utilities related to the Object class.
    Author:
    tomyeh
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.Character NULL_CHARACTER
      The null character.
      static java.lang.Object UNKNOWN
      Denotes unknown.
      static java.math.BigDecimal ZERO_BIG_DECIMAL
      Represents 0 in big decimal.
      static java.math.BigInteger ZERO_BIG_INTEGER
      Represents 0 in big integer.
      static java.lang.Byte ZERO_BYTE
      The zero integer.
      static java.lang.Double ZERO_DOUBLE
      The zero double.
      static java.lang.Float ZERO_FLOAT
      The zero float.
      static java.lang.Integer ZERO_INTEGER
      The zero integer.
      static java.lang.Long ZERO_LONG
      The zero long.
      static java.lang.Short ZERO_SHORT
      The zero short.
    • Constructor Summary

      Constructors 
      Constructor Description
      Objects()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object clone​(java.lang.Object o)
      Clones the specified object.
      static boolean equals​(java.lang.Object a, java.lang.Object b)
      Tests whether two objects are equals.
      static int hashCode​(boolean[] v)
      Generates hash codes for an array of boolean.
      static int hashCode​(byte[] v)
      Generates hash codes for an array of bytes.
      static int hashCode​(byte[] v, int len)
      Generates hash codes for an array of bytes up to the specified length.
      static int hashCode​(char[] v)
      Generates hash codes for an array.
      static int hashCode​(int[] v)
      Generates hash codes for an array.
      static int hashCode​(long[] v)
      Generates hash codes for an array.
      static int hashCode​(short[] v)
      Generates hash codes for an array.
      static int hashCode​(java.lang.Object o)
      Returns the object's hash code, or zero if null.
      static int nextHashCode​(int prevHashVal, int newVal)
      Returns the next hash value by giving the previous one and a new one.
      static byte[] toByteArray​(byte v)
      Converts a byte to a big-endian byte array.
      static byte[] toByteArray​(int v)
      Converts an integer to a big-endian byte array.
      static byte[] toByteArray​(long v)
      Converts a long to a big-endian byte array.
      static byte[] toByteArray​(short v)
      Converts a short to a big-endian byte array.
      static char[] toCharArray​(java.lang.Object o)
      Converts any object to a character array.
      static java.lang.String toString​(java.lang.Object o)
      Converts any object to a string.
      • Methods inherited from class java.lang.Object

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

      • UNKNOWN

        public static final java.lang.Object UNKNOWN
        Denotes unknown. It is useful if both null and unknown is required.
      • ZERO_LONG

        public static final java.lang.Long ZERO_LONG
        The zero long.
      • ZERO_INTEGER

        public static final java.lang.Integer ZERO_INTEGER
        The zero integer.
      • ZERO_SHORT

        public static final java.lang.Short ZERO_SHORT
        The zero short.
      • ZERO_BYTE

        public static final java.lang.Byte ZERO_BYTE
        The zero integer.
      • ZERO_FLOAT

        public static final java.lang.Float ZERO_FLOAT
        The zero float.
      • ZERO_DOUBLE

        public static final java.lang.Double ZERO_DOUBLE
        The zero double.
      • ZERO_BIG_DECIMAL

        public static final java.math.BigDecimal ZERO_BIG_DECIMAL
        Represents 0 in big decimal. The same as BigDecimals.ZERO.
        See Also:
        BigDecimals.ONE
      • ZERO_BIG_INTEGER

        public static final java.math.BigInteger ZERO_BIG_INTEGER
        Represents 0 in big integer. Same as BigIntegers.ZERO.
      • NULL_CHARACTER

        public static final java.lang.Character NULL_CHARACTER
        The null character.
    • Constructor Detail

      • Objects

        public Objects()
    • Method Detail

      • nextHashCode

        public static final int nextHashCode​(int prevHashVal,
                                             int newVal)
        Returns the next hash value by giving the previous one and a new one. The caller usually uses a loop to accumulate all related fields.
        Parameters:
        prevHashVal - the previous hash value returned by this method; 0 if it is the first call.
        newVal - the new value to put in
        Returns:
        the new hash value
      • hashCode

        public static final int hashCode​(boolean[] v)
        Generates hash codes for an array of boolean. It is suggested to cache the hash code.
        Parameters:
        v - the array
        Returns:
        the hash code
      • hashCode

        public static final int hashCode​(byte[] v)
        Generates hash codes for an array of bytes. It is suggested to cache the hash code.
        Parameters:
        v - the array
        Returns:
        the hash code
      • hashCode

        public static final int hashCode​(byte[] v,
                                         int len)
        Generates hash codes for an array of bytes up to the specified length. It is suggested to cache the hash code.
        Parameters:
        v - the array
        len - the maximal length to generate hashCode
        Returns:
        the hash code
      • hashCode

        public static final int hashCode​(char[] v)
        Generates hash codes for an array. It is suggested to cache the hash code.
        Parameters:
        v - the array
        Returns:
        the hash code
      • hashCode

        public static final int hashCode​(short[] v)
        Generates hash codes for an array. It is suggested to cache the hash code.
        Parameters:
        v - the array
        Returns:
        the hash code
      • hashCode

        public static final int hashCode​(int[] v)
        Generates hash codes for an array. It is suggested to cache the hash code.
        Parameters:
        v - the byte array
        Returns:
        the hash code
      • hashCode

        public static final int hashCode​(long[] v)
        Generates hash codes for an array. It is suggested to cache the hash code.
        Parameters:
        v - the array
        Returns:
        the hash code
      • hashCode

        public static final int hashCode​(java.lang.Object o)
        Returns the object's hash code, or zero if null.
      • equals

        public static final boolean equals​(java.lang.Object a,
                                           java.lang.Object b)
        Tests whether two objects are equals.

        It takes care of the null case. Thus, it is helpful to implement Object.equals.

        Notice: it uses compareTo if BigDecimal is found. So, in this case, a.equals(b) might not be the same as Objects.equals(a, b).

        If both a and b are Object[], they are compared item-by-item.

      • toCharArray

        public static final char[] toCharArray​(java.lang.Object o)
        Converts any object to a character array.
        Parameters:
        o - the object to convert
        Returns:
        the char array or null if o is null
      • toString

        public static final java.lang.String toString​(java.lang.Object o)
        Converts any object to a string. If o is an object array, it invokes ArraysX.toString(java.lang.Object[]) to make the string more readable.
      • toByteArray

        public static final byte[] toByteArray​(int v)
        Converts an integer to a big-endian byte array.
      • toByteArray

        public static final byte[] toByteArray​(long v)
        Converts a long to a big-endian byte array.
      • toByteArray

        public static final byte[] toByteArray​(short v)
        Converts a short to a big-endian byte array.
      • toByteArray

        public static final byte[] toByteArray​(byte v)
        Converts a byte to a big-endian byte array.
      • clone

        public static final java.lang.Object clone​(java.lang.Object o)
        Clones the specified object. Use clone() if Cloneable. Otherwise, try to serialize/deserialize it by use of MarshalledObject.

        If o is null, null is returned.

        Throws:
        SystemException - if failed to clone