Class HashUtil


  • public final class HashUtil
    extends Object
    Utility methods for hashing different data types.

    These hashes are based on C code placed in the public domain by Bob Jenkins. They are advertised as strong enough to allow the use of hash & mask instead of hash % prime for computing a hash bucket.

    • Field Detail

      • NULL_HASH_CODE

        public static final int NULL_HASH_CODE
        An arbitrary nonzero constant for the hash code of null valued tokens.
        See Also:
        Constant Field Values
    • Constructor Detail

      • HashUtil

        public HashUtil()
    • Method Detail

      • binaryHashCode

        public static final int binaryHashCode​(byte[] bytes)
        Hash a binary value using Bob Jenkins' "One-at-a-Time Hash".
        Parameters:
        bytes - the value to hash
        Returns:
        the hash code
      • binaryHashCode

        public static final int binaryHashCode​(byte[] bytes,
                                               int offset,
                                               int len)
        Hash a binary value using Bob Jenkins' "One-at-a-Time Hash".
        Parameters:
        bytes - the value to hash
        offset - start offset
        len - number of bytes to code
        Returns:
        the hash code
      • charHashCode

        public static final int charHashCode​(char c)
        Hashes a character value using the same algorithm as intHashCode(int).
        Parameters:
        c - the value to hash
        Returns:
        the hash code
      • booleanHashCode

        public static final int booleanHashCode​(boolean b)
        Hashes a boolean value using the same algorithm as intHashCode(int).
        Parameters:
        b - the value to hash
        Returns:
        the hash code
      • doubleHashCode

        public static final int doubleHashCode​(double d)
        Hashes a double value using the same algorithm as longHashCode(long).
        Parameters:
        d - the value to hash
        Returns:
        the hash code
      • floatHashCode

        public static final int floatHashCode​(float f)
        Hashes a float value using the same algorithm as intHashCode(int).
        Parameters:
        f - the value to hash
        Returns:
        the hash code
      • longHashCode

        public static final int longHashCode​(long l)
        Hashes a long value.
        Parameters:
        l - the value to hash
        Returns:
        the hash code
      • objectHashCode

        public static final int objectHashCode​(Object o)
        Hashes an object using Object.hashCode(). Null values are given their own hash code.
        Parameters:
        o - the value to hash
        Returns:
        the hash code
      • timestampHashCode

        public static final int timestampHashCode​(long epochSecs,
                                                  int nanos)
        Hashes a timestamp decomposed in seconds and nanos parts. Note that time zone offset is deliberately not included. Equal timestamp values in different time zones must have the same hashCode.
        Parameters:
        epochSecs - the seconds-since-epoch portion of the timestamp
        nanos - the nanoseconds portion of the timestamp
        Returns:
        the hash code
      • finalMix

        public static final int finalMix​(int a,
                                         int b,
                                         int c)
        Replicates the computation of Bob Jenkins' "final" macro.
        Parameters:
        a - the first input to the macro
        b - the second input to the macro
        c - the third input to the macro
        Returns:
        the resulting mix