Class HashCalc


  • public class HashCalc
    extends Object
    A disposable class to facilitate the calculation of hash codes, i.e. this class is intended to be used in implementations of Object.hashCode().

    This class knows how to deal with primitive types and objects, but also arrays (of primitive types and objects), even nested ones. It also deals with order, i.e. feeding it the same values in a different order will also produce a different hash code. null values don't have to be treated specially either.

    The initial hash code is based on the class name provided in the constructor. Arbitrary values (usually those that are also queried in Object.equals(Object)) can then be fed to the HashCalc using the various feed(..) methods. The resulting hash code can be retrieved with hashCode().

    • Constructor Detail

      • HashCalc

        public HashCalc​(Class<?> forClass)
        Constructs a new HashCalc for an object of the specified class.
        Parameters:
        forClass - the type of object for which to calculate a hash code
    • Method Detail

      • feed

        public HashCalc feed​(boolean value)
        Feeds a boolean to the computed hash code.
        Parameters:
        value - value to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(char value)
        Feeds a char to the computed hash code.
        Parameters:
        value - value to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(byte value)
        Feeds a byte to the computed hash code.
        Parameters:
        value - value to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(short value)
        Feeds a short to the computed hash code.
        Parameters:
        value - value to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(int value)
        Feeds a int to the computed hash code.

        All other feed() methods are redirected to this method. So in order to provide custom hash code computations only this method needs to be overridden.

        Parameters:
        value - value to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(long value)
        Feeds a long value to the computed hash code.
        Parameters:
        value - value to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(float value)
        Feeds a float to the computed hash code.
        Parameters:
        value - value to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(double value)
        Feeds a double to the computed hash code.
        Parameters:
        value - value to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(boolean[] array)
        Feeds a boolean array to the computed hash code. The arrays hash code is acquired using Arrays.hashCode(boolean[]).
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(byte[] array)
        Feeds a byte array to the computed hash code. The arrays hash code is acquired using Arrays.hashCode(byte[]).
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(short[] array)
        Feeds a short array to the computed hash code. The arrays hash code is acquired using Arrays.hashCode(short[]).
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(int[] array)
        Feeds a int array to the computed hash code. The arrays hash code is acquired using Arrays.hashCode(int[]).
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(long[] array)
        Feeds a long array to the computed hash code. The arrays hash code is acquired using Arrays.hashCode(long[]).
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(float[] array)
        Feeds a float array to the computed hash code. The arrays hash code is acquired using Arrays.hashCode(float[]).
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(double[] array)
        Feeds a double array to the computed hash code. The arrays hash code is acquired using Arrays.hashCode(double[]).
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • feed

        public HashCalc feed​(Object[] array)
        Feeds an Object array to the computed hash code. The array's deep (!) hash code is acquired using Arrays.deepHashCode(Object[]). So be careful if the array could potentially contain direct or indirect references to itself! If you are absolutely sure that the array does not contain any other arrays, using feedShallow(Object[]) may save some instanceofs.
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • feedShallow

        public HashCalc feedShallow​(Object[] array)
        Feeds an Object array to the computed hash code. The array's shallow (!) hash code is acquired using Arrays.hashCode(Object[]). So if the array contains other arrays, their hash code is based on their identity rather than their content. This may be useful if the array could potentially contain direct or indirect references to itself.
        Parameters:
        array - array to be included in the hash code
        Returns:
        this HashCalc
      • hashCode

        public int hashCode()
        Returns the computed hash code.
        Overrides:
        hashCode in class Object
        Returns:
        the computed hash code