Package com.helger.commons.hashcode
Class HashCodeGenerator
java.lang.Object
com.helger.commons.hashcode.HashCodeGenerator
- All Implemented Interfaces:
IHashCodeGenerator,IAppendable<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
FieldsModifier and TypeFieldDescriptionstatic final intUse a prime number as the start.Fields inherited from interface com.helger.commons.hashcode.IHashCodeGenerator
ILLEGAL_HASHCODE -
Constructor Summary
ConstructorsConstructorDescriptionHashCodeGenerator(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
Modifier and TypeMethodDescriptionappend(boolean x) Atomic type hash code generation.append(boolean[] x) Array hash code generation.append(byte x) Atomic type hash code generation.append(byte[] x) Array hash code generation.append(char x) Atomic type hash code generation.append(char[] x) Array hash code generation.append(double x) Atomic type hash code generation.append(double[] x) Array hash code generation.append(float x) Atomic type hash code generation.append(float[] x) Array hash code generation.append(int x) Atomic type hash code generation.append(int[] x) Array hash code generation.append(long x) Atomic type hash code generation.append(long[] x) Array hash code generation.append(short x) Atomic type hash code generation.append(short[] x) Array hash code generation.Object hash code generation.Array hash code generation.Object hash code generation.Array hash code generation.Type specific hash code generation because parameter class has no overloaded equals method.Type specific hash code generation because parameter class has no overloaded equals method.booleanDeprecated.Don't call thisstatic HashCodeGeneratorgetDerived(int nSuperHashCode) Create aHashCodeGeneratorfor derived classes where the base class also uses theHashCodeGenerator.intRetrieve 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 Details
-
INITIAL_HASHCODE
public static final int INITIAL_HASHCODEUse a prime number as the start.- See Also:
-
-
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() -
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
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
Array 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
Array 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
Array 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
Array 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
Array hash code generation.- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- Array to add- Returns:
- this
-
append
Type specific hash code generation because parameter class has no overloaded equals method.- Parameters:
x- object to add- Returns:
- this
-
append
Type specific hash code generation because parameter class has no overloaded equals method.- Parameters:
x- object to add- Returns:
- this
-
append
- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- to be included in the hash code generation.- Returns:
- this
-
append
- Specified by:
appendin interfaceIAppendable<IHashCodeGenerator>- Parameters:
x- to be included in the hash code generation.- Returns:
- this
-
append
- 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.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 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
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
-