Class HashUtils


  • public class HashUtils
    extends Object

    Assists in implementing Object.hashCode() methods.

    In this class HashCodeBuilder is referred to build a good hashCode method for any class.

    The following is the approach taken. When appending a data field, the current total is multiplied by the multiplier then a relevant value for that data type is added. For example, if the current hashCode is 17, and the multiplier is 37, then appending the integer 45 will create a hash code of 674, namely 17 * 37 + 45.

    Version:
    1.0 10 August 2018
    Author:
    Jyoti Prakash Nayak
    • Constructor Detail

      • HashUtils

        public HashUtils()
        A constructor of HashUtil class without any parameters. Here the created HashCodeBuilder object is initialized without any parameters. The initializing odd number and multiplying odd number are assigned default values 7919l, 7664345821815920749l respectively.
      • HashUtils

        public HashUtils​(long initialOddNumber,
                         long multiplierOddNumber)
                  throws HashUtilException
        A constructor of HashUtil class with parameters. Here the created HashCodeBuilder object is initialized with the given initializing odd number and multiplying odd number .
        Parameters:
        initialOddNumber - an odd number used as the initial value
        multiplierOddNumber - an odd number used as the multiplier
        Throws:
        HashUtilException - if the number is even
    • Method Detail

      • append

        public HashUtils append​(boolean value)
        Append a hashCode for a boolean. This adds 1 when true, and 0 when false to the hashCode. This is in contrast to the standard java.lang.Boolean.hashCode handling, which computes a hashCode value of 1231 for java.lang.Boolean instances that represent true or 1237 for java.lang.Boolean instances that represent false. This is in accordance with the Effective Java design.
        Parameters:
        value - the boolean to add to the hashCode
        Returns:
        this
      • append

        public HashUtils append​(boolean[] array)
        Append a hashCode for a boolean array.
        Parameters:
        array - the array to add to the hashCode
        Returns:
        this
      • append

        public HashUtils append​(byte value)
        Append a hashCode for a byte.
        Parameters:
        value - the byte to add to the hashcode
        Returns:
        this
      • append

        public HashUtils append​(byte[] array)
        Append a hashcode for a byte array
        Parameters:
        array - array to add to the hashcode
        Returns:
        this
      • append

        public HashUtils append​(char value)
        Append a hashcode for a character
        Parameters:
        value - the character to add to the hashcode
        Returns:
        this
      • append

        public HashUtils append​(char[] array)
        Append a hashcode for a character array
        Parameters:
        array - array to add to the hashcode
        Returns:
        this
      • append

        public HashUtils append​(double value)
        Append a hashcode for a double
        Parameters:
        value - double to add to the hash code
        Returns:
        this
      • append

        public HashUtils append​(double[] array)
        Append a hashcode for a double array
        Parameters:
        array - array to add to the hash code
        Returns:
        this
      • append

        public HashUtils append​(float value)
        Append a hashcode for a float
        Parameters:
        value - float to add to the hash code
        Returns:
        this
      • append

        public HashUtils append​(float[] array)
        Append a hashcode for a float array
        Parameters:
        array - array to add to the hash code
        Returns:
        this
      • append

        public HashUtils append​(int value)
        Append a hashcode for a integer
        Parameters:
        value - integer to add to the hash code
        Returns:
        this
      • append

        public HashUtils append​(int[] array)
        Append a hashcode for a integer array
        Parameters:
        array - array to add to the hash code
        Returns:
        this
      • append

        public HashUtils append​(long value)
        Append a hashcode for a long
        Parameters:
        value - long to add to the hash code
        Returns:
        this
      • append

        public HashUtils append​(long[] array)
        Append a hashcode for a long array
        Parameters:
        array - array to add to the hashcode
        Returns:
        this
      • append

        public HashUtils append​(Object value)
        Append a hashcode for a object
        Parameters:
        value - object to add to the hashcode
        Returns:
        this
      • append

        public HashUtils append​(Object[] array)
        Append a hashcode for a object array
        Parameters:
        array - array to add to the hashcode
        Returns:
        this
      • append

        public HashUtils append​(short value)
        Append a hashcode for a short
        Parameters:
        value - value to add to the hashcode
        Returns:
        this
      • append

        public HashUtils append​(short[] array)
        Append a hashcode for a short array
        Parameters:
        array - array to add to the hashcode
        Returns:
        this
      • build

        public long build()
        Returns the computed hashCode.
        Returns:
        integer hashcode based on the fields appended
      • toHashCode

        public long toHashCode()
        Return the computed hashCode.
        Returns:
        integer hashcode based on the fields appended
      • isDigestEqual

        public boolean isDigestEqual​(byte[] source,
                                     byte[] target)
        This method checks whether two digest are equal or not.
        Parameters:
        source - the digest to be compared.
        target - the digest to be compared with.
        Returns:
        true if they are equal.