Class HashCodeBuilder
- java.lang.Object
-
- org.apache.cayenne.util.HashCodeBuilder
-
public class HashCodeBuilder extends Object
Assists in implementingObject.hashCode()methods. The code is based on HashCodeBuilder from commons-lang 2.1.- Since:
- 3.0
-
-
Constructor Summary
Constructors Constructor Description HashCodeBuilder()Uses 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HashCodeBuilderappend(boolean value)Append ahashCodefor aboolean.HashCodeBuilderappend(boolean[] array)Append ahashCodefor abooleanarray.HashCodeBuilderappend(byte value)Append ahashCodefor abyte.HashCodeBuilderappend(byte[] array)Append ahashCodefor abytearray.HashCodeBuilderappend(char value)Append ahashCodefor achar.HashCodeBuilderappend(char[] array)Append ahashCodefor achararray.HashCodeBuilderappend(double value)Append ahashCodefor adouble.HashCodeBuilderappend(double[] array)Append ahashCodefor adoublearray.HashCodeBuilderappend(float value)Append ahashCodefor afloat.HashCodeBuilderappend(float[] array)Append ahashCodefor afloatarray.HashCodeBuilderappend(int value)Append ahashCodefor anint.HashCodeBuilderappend(int[] array)Append ahashCodefor anintarray.HashCodeBuilderappend(long value)Append ahashCodefor along.HashCodeBuilderappend(long[] array)Append ahashCodefor alongarray.HashCodeBuilderappend(short value)Append ahashCodefor ashort.HashCodeBuilderappend(short[] array)Append ahashCodefor ashortarray.HashCodeBuilderappend(Object object)Append ahashCodefor anObject.HashCodeBuilderappend(Object[] array)Append ahashCodefor anObjectarray.HashCodeBuilderappendSuper(int superHashCode)Adds the result of super.hashCode() to this builder.inttoHashCode()Return the computedhashCode.
-
-
-
Constructor Detail
-
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 Detail
-
appendSuper
public HashCodeBuilder appendSuper(int superHashCode)
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
public HashCodeBuilder append(Object object)
Append a
hashCodefor anObject.- Parameters:
object- the Object to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(long value)
Append a
hashCodefor along.- Parameters:
value- the long to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(int value)
Append a
hashCodefor anint.- Parameters:
value- the int to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(short value)
Append a
hashCodefor ashort.- Parameters:
value- the short to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(char value)
Append a
hashCodefor achar.- Parameters:
value- the char to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(byte value)
Append a
hashCodefor abyte.- Parameters:
value- the byte to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(double value)
Append a
hashCodefor adouble.- Parameters:
value- the double to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(float value)
Append a
hashCodefor afloat.- Parameters:
value- the float to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(boolean value)
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
public HashCodeBuilder append(Object[] array)
Append a
hashCodefor anObjectarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(long[] array)
Append a
hashCodefor alongarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(int[] array)
Append a
hashCodefor anintarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(short[] array)
Append a
hashCodefor ashortarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(char[] array)
Append a
hashCodefor achararray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(byte[] array)
Append a
hashCodefor abytearray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(double[] array)
Append a
hashCodefor adoublearray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(float[] array)
Append a
hashCodefor afloatarray.- Parameters:
array- the array to add to thehashCode- Returns:
- this
-
append
public HashCodeBuilder append(boolean[] array)
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
-
-