Class HashCodeBuilder
Object.hashCode() methods. The code is based on
HashCodeBuilder from commons-lang 2.1.- Since:
- 3.0
-
Constructor Summary
ConstructorsConstructorDescriptionUses two hard coded choices for the constants needed to build ahashCode.HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) Two randomly chosen, non-zero, odd numbers must be passed in. -
Method Summary
Modifier and TypeMethodDescriptionappend(boolean value) Append ahashCodefor aboolean.append(boolean[] array) Append ahashCodefor abooleanarray.append(byte value) Append ahashCodefor abyte.append(byte[] array) Append ahashCodefor abytearray.append(char value) Append ahashCodefor achar.append(char[] array) Append ahashCodefor achararray.append(double value) Append ahashCodefor adouble.append(double[] array) Append ahashCodefor adoublearray.append(float value) Append ahashCodefor afloat.append(float[] array) Append ahashCodefor afloatarray.append(int value) Append ahashCodefor anint.append(int[] array) Append ahashCodefor anintarray.append(long value) Append ahashCodefor along.append(long[] array) Append ahashCodefor alongarray.append(short value) Append ahashCodefor ashort.append(short[] array) Append ahashCodefor ashortarray.Append ahashCodefor anObject.Append ahashCodefor anObjectarray.appendSuper(int superHashCode) Adds the result of super.hashCode() to this builder.intReturn the computedhashCode.
-
Constructor Details
-
HashCodeBuilder
public HashCodeBuilder()Uses two hard coded choices for the constants needed to build a
hashCode. -
HashCodeBuilder
public HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) Two randomly chosen, non-zero, odd numbers must be passed in. Ideally these should be different for each class, however this is not vital.
Prime numbers are preferred, especially for the multiplier.
- Parameters:
initialNonZeroOddNumber- a non-zero, odd number used as the initial valuemultiplierNonZeroOddNumber- a non-zero, odd number used as the multiplier- Throws:
IllegalArgumentException- if the number is zero or even
-
-
Method Details
-
appendSuper
Adds the result of super.hashCode() to this builder.
- Parameters:
superHashCode- the result of callingsuper.hashCode()- Returns:
- this HashCodeBuilder, used to chain calls.
- Since:
- 2.0
-
append
Append a
hashCodefor anObject.- Parameters:
object- the Object to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor along.- Parameters:
value- the long to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor anint.- Parameters:
value- the int to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor ashort.- Parameters:
value- the short to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor achar.- Parameters:
value- the char to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor abyte.- Parameters:
value- the byte to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor adouble.- Parameters:
value- the double to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor afloat.- Parameters:
value- the float to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor aboolean.This adds
iConstant * 1to thehashCodeand not a1231or1237as done in java.lang.Boolean. This is in accordance with the Effective Java design.- Parameters:
value- the boolean to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor anObjectarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor alongarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor anintarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor ashortarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor achararray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor abytearray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor adoublearray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor afloatarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
Append a
hashCodefor abooleanarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
toHashCode
public int toHashCode()Return the computed
hashCode.- Returns:
hashCodebased on the fields appended
-