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[] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, boolean[] 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[] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, byte[] 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[] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, char[] 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[] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, double[] 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[] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, float[] 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[] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, int[] 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[] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, long[] 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[] x)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, short[] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    append(int nPrevHashCode, Object x)
    Object hash code generation.
    static int
    append(int nPrevHashCode, Object[] x)
    Array hash code generation.
    static int
    append(int nPrevHashCode, Object[] x, int nOfs, int nLen)
    Atomic type hash code generation.
    static int
    hashCode(boolean x)
     
    static int
    hashCode(boolean[] x, int nOfs, int nLen)
     
    static int
    hashCode(byte x)
     
    static int
    hashCode(byte[] x, int nOfs, int nLen)
     
    static int
    hashCode(char x)
     
    static int
    hashCode(char[] x, int nOfs, int nLen)
     
    static long
    hashCode(double x)
     
    static int
    hashCode(double[] x, int nOfs, int nLen)
     
    static int
    hashCode(float x)
     
    static int
    hashCode(float[] x, int nOfs, int nLen)
     
    static int
    hashCode(int[] x, int nOfs, int nLen)
     
    static int
    hashCode(long[] x, int nOfs, int nLen)
     
    static int
    hashCode(short x)
     
    static int
    hashCode(short[] x, int nOfs, int nLen)
     
    static int
     
    static int
    hashCode(Object[] x, int nOfs, int nLen)
     

    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)
    • hashCode

      public static int hashCode(byte x)
    • hashCode

      public static int hashCode(char x)
    • hashCode

      public static long hashCode(double x)
    • hashCode

      public static int hashCode(float x)
    • hashCode

      public static int hashCode(short x)
    • hashCode

      @UsedInGeneratedCode public static int hashCode(@Nullable Object x)
    • 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(@Nonnull boolean[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • hashCode

      public static int hashCode(@Nonnull byte[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • hashCode

      public static int hashCode(@Nonnull char[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • hashCode

      public static int hashCode(@Nonnull double[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • hashCode

      public static int hashCode(@Nonnull float[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • hashCode

      public static int hashCode(@Nonnull int[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • hashCode

      public static int hashCode(@Nonnull long[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • hashCode

      public static int hashCode(@Nonnull short[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • hashCode

      public static int hashCode(@Nonnull Object[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
    • append

      public static int append(int nPrevHashCode, @Nullable 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, @Nullable boolean[] 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 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, @Nullable byte[] 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 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, @Nullable char[] 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 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, @Nullable double[] 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 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, @Nullable float[] 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 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, @Nullable int[] 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 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, @Nullable long[] 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 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 short[] 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