Class HashCodeGenerator

java.lang.Object
com.helger.base.hashcode.HashCodeGenerator
All Implemented Interfaces:
IHashCodeGenerator, IAppendable<IHashCodeGenerator>

@NotThreadSafe @UsedInGeneratedCode public final class HashCodeGenerator extends Object implements IHashCodeGenerator
A small hash code creation class based on the article found in the net. See this article for details.
After calling append(Object) for all objects use getHashCode() to retrieve the calculated hash code. Once the hash code was calculated no modifications are allowed.

A real world example for a final class derived from Object or a base class looks like this:

 @Override
 public int hashCode ()
 {
   return new HashCodeGenerator (this).append (member1).append (member2).getHashCode ();
 }
 

For a derived class, the typical code looks like this, assuming the base class also uses HashCodeGenerator:

 @Override
 public int hashCode ()
 {
   return HashCodeGenerator.getDerived (super.hashCode ()).append (member3).append (member4).getHashCode ();
 }
 
Author:
Philip Helger
  • Constructor Details

    • HashCodeGenerator

      public HashCodeGenerator(@NonNull Object aSrcObject)
      This is a sanity constructor that allows for any object to be passed in the constructor (e.g. this) from which the class is extracted as the initial value of the hash code.
      Parameters:
      aSrcObject - The source object from which the class is extracted. May not be null.
    • HashCodeGenerator

      public HashCodeGenerator(@NonNull Class<?> aClass)
      This constructor requires a class name, because in case a class has no instance variables the hash code may be the same for different instances of different classes.
      Parameters:
      aClass - The class this instance is about to create a hash code for. May not be null.
  • Method Details

    • isClosed

      public boolean isClosed()
      Returns:
      true if the hash code has already been calculated and no further modifications are allowed, false if appending is still possible.
    • append

      public @NonNull HashCodeGenerator append(boolean x)
      Atomic type hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(byte x)
      Atomic type hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(char x)
      Atomic type hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(double x)
      Atomic type hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(float x)
      Atomic type hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(int x)
      Atomic type hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(long x)
      Atomic type hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(short x)
      Atomic type hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(@Nullable Object x)
      Object hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(boolean @Nullable [] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(boolean @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(byte @Nullable [] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(byte @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(char @Nullable [] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(char @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(double @Nullable [] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(double @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(float @Nullable [] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(float @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(int @Nullable [] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(int @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(long @Nullable [] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(long @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(short @Nullable [] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(short @Nullable [] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(@Nullable Object[] x)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      Returns:
      this
    • append

      public @NonNull HashCodeGenerator append(@Nullable Object[] x, @Nonnegative int nOfs, @Nonnegative int nLen)
      Array hash code generation.
      Specified by:
      append in interface IAppendable<IHashCodeGenerator>
      Parameters:
      x - Array to add
      nOfs - Offset to start from. Must be ≥ 0.
      nLen - Number of array items to use. Must be ≥ 0.
      Returns:
      this
    • getHashCode

      public int getHashCode()
      Retrieve the final hash code. Once this method has been called, no further calls to append can be done since the hash value is locked!
      Specified by:
      getHashCode in interface IHashCodeGenerator
      Returns:
      The finally completed hash code. The returned value is never IHashCodeGenerator.ILLEGAL_HASHCODE. If the calculated hash code would be IHashCodeGenerator.ILLEGAL_HASHCODE it is changed to -1 instead.
    • equals

      @Deprecated(forRemoval=false) public boolean equals(Object o)
      Deprecated.
      Don't call this
      Never compare HashCodeGenerator objects :)
      Overrides:
      equals in class Object
    • hashCode

      @Deprecated(forRemoval=false) public int hashCode()
      Deprecated.
      Don't call this
      Always use getHashCode()
      Overrides:
      hashCode in class Object
      Returns:
      getHashCode()
      See Also:
    • getDerived

      @UsedInGeneratedCode public static @NonNull HashCodeGenerator getDerived(int nSuperHashCode)
      Create a HashCodeGenerator for derived classes where the base class also uses the HashCodeGenerator. This avoid calculating the hash code of the class name more than once.
      Parameters:
      nSuperHashCode - Always pass in super.hashCode ()
      Returns:
      Never null
    • getHashCode

      public static int getHashCode(@NonNull Object aThis, @Nullable Object... aMembers)
      Static helper method to create the hash code of an object with a single invocation. This method must be used by objects that directly derive from Object.
      Parameters:
      aThis - this
      aMembers - A list of all members. Primitive types must be boxed.
      Returns:
      The generated hashCode.
      Since:
      9.4.5
    • getHashCode

      public static int getHashCode(int nSuperHashCode, @Nullable Object... aMembers)
      Static helper method to create the hash code of an object with a single invocation. This method must be used by objects that derive from a class other than Object.
      Parameters:
      nSuperHashCode - The result of super.hashCode()
      aMembers - A list of all members. Primitive types must be boxed.
      Returns:
      The generated hashCode.
      Since:
      9.4.5