Package com.helger.base.hashcode
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
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
-
Field Summary
Fields inherited from interface com.helger.base.hashcode.IHashCodeGenerator
ILLEGAL_HASHCODE -
Constructor Summary
ConstructorsConstructorDescriptionHashCodeGenerator(@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.HashCodeGenerator(@NonNull Object aSrcObject) This is a sanity constructor that allows for any object to be passed in the constructor (e.g. -
Method Summary
Modifier and TypeMethodDescription@NonNull HashCodeGeneratorappend(boolean x) Atomic type hash code generation.@NonNull HashCodeGeneratorappend(boolean @Nullable [] x) Array hash code generation.@NonNull HashCodeGeneratorappend(boolean @Nullable [] x, int nOfs, int nLen) Array hash code generation.@NonNull HashCodeGeneratorappend(byte x) Atomic type hash code generation.@NonNull HashCodeGeneratorappend(byte @Nullable [] x) Array hash code generation.@NonNull HashCodeGeneratorappend(byte @Nullable [] x, int nOfs, int nLen) Array hash code generation.@NonNull HashCodeGeneratorappend(char x) Atomic type hash code generation.@NonNull HashCodeGeneratorappend(char @Nullable [] x) Array hash code generation.@NonNull HashCodeGeneratorappend(char @Nullable [] x, int nOfs, int nLen) Array hash code generation.@NonNull HashCodeGeneratorappend(double x) Atomic type hash code generation.@NonNull HashCodeGeneratorappend(double @Nullable [] x) Array hash code generation.@NonNull HashCodeGeneratorappend(double @Nullable [] x, int nOfs, int nLen) Array hash code generation.@NonNull HashCodeGeneratorappend(float x) Atomic type hash code generation.@NonNull HashCodeGeneratorappend(float @Nullable [] x) Array hash code generation.@NonNull HashCodeGeneratorappend(float @Nullable [] x, int nOfs, int nLen) Array hash code generation.@NonNull HashCodeGeneratorappend(int x) Atomic type hash code generation.@NonNull HashCodeGeneratorappend(int @Nullable [] x) Array hash code generation.@NonNull HashCodeGeneratorappend(int @Nullable [] x, int nOfs, int nLen) Array hash code generation.@NonNull HashCodeGeneratorappend(long x) Atomic type hash code generation.@NonNull HashCodeGeneratorappend(long @Nullable [] x) Array hash code generation.@NonNull HashCodeGeneratorappend(long @Nullable [] x, int nOfs, int nLen) Array hash code generation.@NonNull HashCodeGeneratorappend(short x) Atomic type hash code generation.@NonNull HashCodeGeneratorappend(short @Nullable [] x) Array hash code generation.@NonNull HashCodeGeneratorappend(short @Nullable [] x, int nOfs, int nLen) Array hash code generation.@NonNull HashCodeGeneratorObject hash code generation.@NonNull HashCodeGeneratorArray hash code generation.@NonNull HashCodeGeneratorArray hash code generation.booleanDeprecated.Don't call thisstatic @NonNull HashCodeGeneratorgetDerived(int nSuperHashCode) Create aHashCodeGeneratorfor derived classes where the base class also uses theHashCodeGenerator.intRetrieve the final hash code.static intgetHashCode(int nSuperHashCode, @Nullable Object... aMembers) Static helper method to create the hash code of an object with a single invocation.static intgetHashCode(@NonNull Object aThis, @Nullable Object... aMembers) Static helper method to create the hash code of an object with a single invocation.inthashCode()Deprecated.Don't call thisbooleanisClosed()
-
Constructor Details
-
HashCodeGenerator
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 benull.
-
HashCodeGenerator
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 benull.
-
-
Method Details
-
isClosed
public boolean isClosed()- Returns:
trueif the hash code has already been calculated and no further modifications are allowed,falseif appending is still possible.
-
append
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Object hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- Offset to start from. Must be ≥ 0.nLen- Number of array items to use. Must be ≥ 0.- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- Offset to start from. Must be ≥ 0.nLen- Number of array items to use. Must be ≥ 0.- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- Offset to start from. Must be ≥ 0.nLen- Number of array items to use. Must be ≥ 0.- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- Offset to start from. Must be ≥ 0.nLen- Number of array items to use. Must be ≥ 0.- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- Offset to start from. Must be ≥ 0.nLen- Number of array items to use. Must be ≥ 0.- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- Offset to start from. Must be ≥ 0.nLen- Number of array items to use. Must be ≥ 0.- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- Offset to start from. Must be ≥ 0.nLen- Number of array items to use. Must be ≥ 0.- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- Offset to start from. Must be ≥ 0.nLen- Number of array items to use. Must be ≥ 0.- Returns:
- this
-
append
Array hash code generation.- Specified by:
appendin interfaceIAppendable<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:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to addnOfs- 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:
getHashCodein interfaceIHashCodeGenerator- Returns:
- The finally completed hash code. The returned value is never
IHashCodeGenerator.ILLEGAL_HASHCODE. If the calculated hash code would beIHashCodeGenerator.ILLEGAL_HASHCODEit is changed to -1 instead.
-
equals
Deprecated.Don't call thisNever compareHashCodeGeneratorobjects :) -
hashCode
Deprecated.Don't call thisAlways usegetHashCode()- Overrides:
hashCodein classObject- Returns:
getHashCode()- See Also:
-
getDerived
Create aHashCodeGeneratorfor derived classes where the base class also uses theHashCodeGenerator. This avoid calculating the hash code of the class name more than once.- Parameters:
nSuperHashCode- Always pass insuper.hashCode ()- Returns:
- Never
null
-
getHashCode
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-thisaMembers- A list of all members. Primitive types must be boxed.- Returns:
- The generated hashCode.
- Since:
- 9.4.5
-
getHashCode
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 ofsuper.hashCode()aMembers- A list of all members. Primitive types must be boxed.- Returns:
- The generated hashCode.
- Since:
- 9.4.5
-