Class HashCodeCalculator

java.lang.Object
com.helger.base.hashcode.HashCodeCalculator

@Immutable public final class HashCodeCalculator extends Object
This class provides the hash code generation for different data types.
Author:
Philip Helger
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The hash code value to be used for null values.
    static final int
    Use a prime number as the start.
    static final int
    Each value is multiplied with this value. 31 because it can easily be optimized to (1 << 5) - 1.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    append(int nPrevHashCode, boolean x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, boolean @Nullable [] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, boolean @Nullable [] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, byte x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, byte @Nullable [] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, byte @Nullable [] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, char x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, char @Nullable [] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, char @Nullable [] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, double x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, double @Nullable [] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, double @Nullable [] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, float x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, float @Nullable [] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, float @Nullable [] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, int x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, int @Nullable [] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, int @Nullable [] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, long x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, long @Nullable [] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, long @Nullable [] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, short x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, short @Nullable [] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, short @Nullable [] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, @Nullable Object x)
    Object hash code generation.
    static int
    append(int nPrevHashCode, @Nullable Object[] x)
    Array hash code generation.
    static int
    append(int nPrevHashCode, @Nullable Object[] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    hashCode(boolean x)
     
    static int
    hashCode(boolean @NonNull [] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of booleans.
    static int
    hashCode(byte x)
     
    static int
    hashCode(byte @NonNull [] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of bytes.
    static int
    hashCode(char x)
     
    static int
    hashCode(char @NonNull [] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of chars.
    static long
    hashCode(double x)
     
    static int
    hashCode(double @NonNull [] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of doubles.
    static int
    hashCode(float x)
     
    static int
    hashCode(float @NonNull [] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of floats.
    static int
    hashCode(int @NonNull [] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of ints.
    static int
    hashCode(long @NonNull [] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of longs.
    static int
    hashCode(short x)
     
    static int
    hashCode(short @NonNull [] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of shorts.
    static int
    hashCode(@NonNull Object[] x, int nOfs, int nLen)
    Compute a hash code for a sub-array of objects.
    static int
    hashCode(@Nullable Object x)
     

    Methods inherited from class java.lang.Object

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

    • INITIAL_HASHCODE

      public static final int INITIAL_HASHCODE
      Use a prime number as the start.
      See Also:
    • MULTIPLIER

      public static final int MULTIPLIER
      Each value is multiplied with this value. 31 because it can easily be optimized to (1 << 5) - 1.
      See Also:
    • HASHCODE_NULL

      public static final int HASHCODE_NULL
      The hash code value to be used for null values. Do not use 0 as e.g. BigDecimal ("0") also results in a 0 hash code.
      See Also:
  • Method Details

    • hashCode

      public static int hashCode(boolean x)
      Parameters:
      x - The value to hash.
      Returns:
      The hash code for the boolean value.
    • hashCode

      public static int hashCode(byte x)
      Parameters:
      x - The value to hash.
      Returns:
      The hash code for the byte value.
    • hashCode

      public static int hashCode(char x)
      Parameters:
      x - The value to hash.
      Returns:
      The hash code for the char value.
    • hashCode

      public static long hashCode(double x)
      Parameters:
      x - The value to hash.
      Returns:
      The hash code for the double value.
    • hashCode

      public static int hashCode(float x)
      Parameters:
      x - The value to hash.
      Returns:
      The hash code for the float value.
    • hashCode

      public static int hashCode(short x)
      Parameters:
      x - The value to hash.
      Returns:
      The hash code for the short value.
    • hashCode

      @UsedInGeneratedCode public static int hashCode(@Nullable Object x)
      Parameters:
      x - The object to hash. May be null.
      Returns:
      The hash code for the object, or HASHCODE_NULL if null.
    • append

      public static int append(int nPrevHashCode, boolean x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, byte x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, char x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, double x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, float x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, int x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, long x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, short x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, @Nullable Object x)
      Object hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Object to add. May be null.
      Returns:
      The updated hash code
    • hashCode

      public static int hashCode(boolean @NonNull [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of booleans.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • hashCode

      public static int hashCode(byte @NonNull [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of bytes.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • hashCode

      public static int hashCode(char @NonNull [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of chars.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • hashCode

      public static int hashCode(double @NonNull [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of doubles.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • hashCode

      public static int hashCode(float @NonNull [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of floats.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • hashCode

      public static int hashCode(int @NonNull [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of ints.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • hashCode

      public static int hashCode(long @NonNull [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of longs.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • hashCode

      public static int hashCode(short @NonNull [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of shorts.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • hashCode

      public static int hashCode(@NonNull Object[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Compute a hash code for a sub-array of objects.
      Parameters:
      x - The array. May not be null.
      nOfs - The offset. Must be ≥ 0.
      nLen - The length. Must be ≥ 0.
      Returns:
      The computed hash code.
    • append

      public static int append(int nPrevHashCode, boolean @Nullable [] x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, boolean @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, byte @Nullable [] x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, byte @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, char @Nullable [] x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, char @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, double @Nullable [] x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, double @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, float @Nullable [] x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, float @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, int @Nullable [] x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, int @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, long @Nullable [] x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, long @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, short @Nullable [] x)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, short @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, @Nullable Object[] x)
      Array hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Object array to add. May be null.
      Returns:
      The updated hash code
    • append

      public static int append(int nPrevHashCode, @Nullable Object[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Atomic type hash code generation.
      Parameters:
      nPrevHashCode - The previous hash code used as the basis for calculation
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      The updated hash code