Package com.helger.commons.hashcode
Class HashCodeGenerator
- java.lang.Object
-
- com.helger.commons.hashcode.HashCodeGenerator
-
- All Implemented Interfaces:
IHashCodeGenerator,IAppendable<IHashCodeGenerator>
@NotThreadSafe 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 callingappend(Object)for all objects usegetHashCode()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
Objector 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 Modifier and Type Field Description static intINITIAL_HASHCODEUse a prime number as the start.-
Fields inherited from interface com.helger.commons.hashcode.IHashCodeGenerator
ILLEGAL_HASHCODE
-
-
Constructor Summary
Constructors Constructor Description HashCodeGenerator(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(Object aSrcObject)This is a sanity constructor that allows for any object to be passed in the constructor (e.g.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description HashCodeGeneratorappend(boolean x)Atomic type hash code generation.HashCodeGeneratorappend(boolean[] x)Array hash code generation.HashCodeGeneratorappend(byte x)Atomic type hash code generation.HashCodeGeneratorappend(byte[] x)Array hash code generation.HashCodeGeneratorappend(char x)Atomic type hash code generation.HashCodeGeneratorappend(char[] x)Array hash code generation.HashCodeGeneratorappend(double x)Atomic type hash code generation.HashCodeGeneratorappend(double[] x)Array hash code generation.HashCodeGeneratorappend(float x)Atomic type hash code generation.HashCodeGeneratorappend(float[] x)Array hash code generation.HashCodeGeneratorappend(int x)Atomic type hash code generation.HashCodeGeneratorappend(int[] x)Array hash code generation.HashCodeGeneratorappend(long x)Atomic type hash code generation.HashCodeGeneratorappend(long[] x)Array hash code generation.HashCodeGeneratorappend(short x)Atomic type hash code generation.HashCodeGeneratorappend(short[] x)Array hash code generation.HashCodeGeneratorappend(Enum<?> x)Object hash code generation.HashCodeGeneratorappend(Enum<?>[] x)Array hash code generation.HashCodeGeneratorappend(Iterable<?> x)HashCodeGeneratorappend(Object x)Object hash code generation.HashCodeGeneratorappend(Object[] x)Array hash code generation.HashCodeGeneratorappend(StringBuffer x)Type specific hash code generation because parameter class has no overloaded equals method.HashCodeGeneratorappend(StringBuilder x)Type specific hash code generation because parameter class has no overloaded equals method.HashCodeGeneratorappend(Map<?,?> x)HashCodeGeneratorappend(Node x)booleanequals(Object o)Deprecated.Don't call thisstatic HashCodeGeneratorgetDerived(int nSuperHashCode)Create aHashCodeGeneratorfor derived classes where the base class also uses theHashCodeGenerator.intgetHashCode()Retrieve the final hash code.static intgetHashCode(int nSuperHashCode, Object... aMembers)Static helper method to create the hashcode of an object with a single invocation.static intgetHashCode(Object aThis, Object... aMembers)Static helper method to create the hashcode of an object with a single invocation.inthashCode()Deprecated.Don't call thisbooleanisClosed()
-
-
-
Field Detail
-
INITIAL_HASHCODE
public static final int INITIAL_HASHCODE
Use a prime number as the start.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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 benull.
-
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 benull.
-
-
Method Detail
-
isClosed
public boolean isClosed()
-
append
@Nonnull public HashCodeGenerator append(boolean x)
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(byte x)
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(char x)
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(double x)
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(float x)
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(int x)
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(long x)
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(short x)
Atomic type hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable Object x)
Object hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable Enum<?> x)
Object hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable boolean[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable byte[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable char[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable double[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable float[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable int[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable long[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable short[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable Object[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable Enum<?>[] x)
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable StringBuffer x)
Type specific hash code generation because parameter class has no overloaded equals method.- Parameters:
x- object to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable StringBuilder x)
Type specific hash code generation because parameter class has no overloaded equals method.- Parameters:
x- object to add- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable Iterable<?> x)
- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- to be included in the hash code generation.- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable Map<?,?> x)
- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- to be included in the hash code generation.- Returns:
- this
-
append
@Nonnull public HashCodeGenerator append(@Nullable Node x)
- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- to be included in the hash code generation.- 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 public boolean equals(Object o)
Deprecated.Don't call thisNever compareHashCodeGeneratorobjects :)
-
hashCode
@Deprecated public int hashCode()
Deprecated.Don't call thisAlways usegetHashCode()- Overrides:
hashCodein classObject- Returns:
getHashCode()- See Also:
getHashCode()
-
getDerived
@Nonnull public static HashCodeGenerator getDerived(int nSuperHashCode)
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
public static int getHashCode(@Nonnull Object aThis, @Nullable Object... aMembers)
Static helper method to create the hashcode 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
public static int getHashCode(int nSuperHashCode, @Nullable Object... aMembers)Static helper method to create the hashcode 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
-
-