Object#hashCode() methods.
This class enables a good hashCode method to be built for any class. It follows the rules laid out in
the book Effective Java by Joshua Bloch. Writing a
good hashCode method is actually quite difficult. This class aims to simplify the process.
The following is the approach taken. When appending a data field, the current total is multiplied by the multiplier then a relevant value for that data type is added. For example, if the current hashCode is 17, and the multiplier is 37, then appending the integer 45 will create a hash code of 674, namely 17 * 37 + 45.
All relevant fields from the object should be included in the hashCode method. Derived fields may be
excluded. In general, any field used in the equals method must be used in the hashCode
method.
To use this class write code as follows:
public class Person {
String name;
int age;
boolean smoker;
...
public int hashCode() {
// you pick a hard-coded, randomly chosen, non-zero, odd number
// ideally different for each class
return new HashCodeBuilder(17, 37).
append(name).
append(age).
append(smoker).
toHashCode();
}
}
If required, the superclass hashCode() can be added using appendSuper.
Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are
usually private, the method, reflectionHashCode, uses AccessibleObject.setAccessible
to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions
are set up correctly. It is also slower than testing explicitly.
A typical invocation for this method would look like:
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
The HashCodeExclude annotation can be used to exclude fields from being
used by the reflectionHashCode methods.
| Modifier and Type | Field and Description |
|---|---|
| private static final int | DEFAULT_INITIAL_VALUE
The default initial value to use in reflection hash code building. |
| private static final int | DEFAULT_MULTIPLIER_VALUE
The default multiplier value to use in reflection hash code building. |
| private final int | iConstant
Constant to use in building the hashCode. |
| private int | iTotal
Running total of the hashCode. |
| private static final ThreadLocal | REGISTRY
A registry of objects used by reflection methods to detect cyclical object references and avoid infinite loops. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | HashCodeBuilder(final int
an odd number used as the initial value initialOddNumber, final int an odd number used as the multiplier multiplierOddNumber)Two randomly chosen, odd numbers must be passed in. Ideally these should be different for each class, however this is not vital. |
| Modifier and Type | Method and Description |
|---|---|
| public HashCodeBuilder | Returns: this instance.the boolean to add to the value)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the array to add to the array)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the byte to add to the value)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the array to add to the array)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the char to add to the value)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the array to add to the array)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the double to add to the value)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the array to add to the array)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the float to add to the value)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the array to add to the array)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the int to add to the value)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the array to add to the array)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the long to add to the value)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the array to add to the array)hashCodeAppend a |
| public HashCodeBuilder | |
| public HashCodeBuilder | |
| public HashCodeBuilder | Returns: this instance.the short to add to the value)hashCodeAppend a |
| public HashCodeBuilder | Returns: this instance.the array to add to the array)hashCodeAppend a |
| private void | |
| public HashCodeBuilder | Returns: this instance.the result of calling superHashCode)super.hashCode()Adds the result of super.hashCode() to this builder. |
| public Integer | Returns: hashCode based on the fields appendedImplements org. Returns the computed |
| public boolean | |
| pack-priv static Set | Returns: Set the registry of objects being traversedGets the registry of objects being traversed by the reflection methods in the current thread. |
| public int | Returns: hashCode based on the fields appendedOverrides java. The computed |
| pack-priv static boolean | Returns: booleantrue if the registry contains the given object.The object to lookup in the registry. value)Tests whether the registry contains the given object. |
| private static void | reflectionAppend(final Object
the object to append details of object, final Class<?> the class to append details of clazz, final HashCodeBuilder the builder to append to builder, final boolean whether to use transient fields useTransients, final String[] Collection of String field names to exclude from use in calculation of hash code excludeFields)Appends the fields and values defined by the given object of the given |
| public static int | Returns: int hash codea non-zero, odd number used as the initial value. This will be the returned
value if no fields are found to include in the hash code initialNonZeroOddNumber, final int a non-zero, odd number used as the multiplier multiplierNonZeroOddNumber, final Object the Object to create a object)hashCode forUses reflection to build a valid hash code from the fields of |
| public static int | Returns: int hash codea non-zero, odd number used as the initial value. This will be the returned
value if no fields are found to include in the hash code initialNonZeroOddNumber, final int a non-zero, odd number used as the multiplier multiplierNonZeroOddNumber, final Object the Object to create a object, final boolean hashCode forwhether to include transient fields testTransients)Uses reflection to build a valid hash code from the fields of |
| public static < the type of the object involved T> int | Returns: int hash codea non-zero, odd number used as the initial value. This will be the returned
value if no fields are found to include in the hash code initialNonZeroOddNumber, final int a non-zero, odd number used as the multiplier multiplierNonZeroOddNumber, final T the Object to create a object, final boolean hashCode forwhether to include transient fields testTransients, final Class<? super T> the superclass to reflect up to (inclusive), may be reflectUpToClass, final String... nullarray of field names to exclude from use in calculation of hash code excludeFields)Uses reflection to build a valid hash code from the fields of |
| public static int | Returns: int hash codethe Object to create a object, final boolean hashCode forwhether to include transient fields testTransients)Uses reflection to build a valid hash code from the fields of |
| public static int | Returns: int hash codethe Object to create a object, final Collection<String> hashCode forCollection of String field names to exclude from use in calculation of hash code excludeFields)Uses reflection to build a valid hash code from the fields of |
| public static int | Returns: int hash codethe Object to create a object, final String... hashCode forarray of field names to exclude from use in calculation of hash code excludeFields)Uses reflection to build a valid hash code from the fields of |
| private static void | |
| public int | |
| private static void |
| DEFAULT_INITIAL_VALUE | back to summary |
|---|---|
| private static final int DEFAULT_INITIAL_VALUE The default initial value to use in reflection hash code building. | |
| DEFAULT_MULTIPLIER_VALUE | back to summary |
|---|---|
| private static final int DEFAULT_MULTIPLIER_VALUE The default multiplier value to use in reflection hash code building. | |
| iConstant | back to summary |
|---|---|
| private final int iConstant Constant to use in building the hashCode. | |
| iTotal | back to summary |
|---|---|
| private int iTotal Running total of the hashCode. | |
| REGISTRY | back to summary |
|---|---|
| private static final ThreadLocal<Set<IDKey>> REGISTRY A registry of objects used by reflection methods to detect cyclical object references and avoid infinite loops.
| |
| HashCodeBuilder | back to summary |
|---|---|
| public HashCodeBuilder() Uses two hard coded choices for the constants needed to build a | |
| HashCodeBuilder | back to summary |
|---|---|
| public HashCodeBuilder(final int initialOddNumber, final int multiplierOddNumber) Two randomly chosen, 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.
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final boolean value) Append a
This adds
This is in contrast to the standard This is in accordance with the Effective Java design.
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final boolean[] array) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final byte value) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final byte[] array) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final char value) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final char[] array) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final double value) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final double[] array) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final float value) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final float[] array) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final int value) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final int[] array) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final long value) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final long[] array) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final Object object) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final Object[] array) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final short value) Append a
| |
| append | back to summary |
|---|---|
| public HashCodeBuilder append(final short[] array) Append a
| |
| appendArray | back to summary |
|---|---|
| private void appendArray(final Object object) Append a
| |
| appendSuper | back to summary |
|---|---|
| public HashCodeBuilder appendSuper(final int superHashCode) Adds the result of super.hashCode() to this builder.
| |
| build | back to summary |
|---|---|
| public Integer build() Implements org. Returns the computed | |
| equals | back to summary |
|---|---|
| public boolean equals(final Object obj) Overrides java. Implements equals using the hash code.
| |
| getRegistry | back to summary |
|---|---|
| pack-priv static Set Gets the registry of objects being traversed by the reflection methods in the current thread. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. The computed
| |
| isRegistered | back to summary |
|---|---|
| pack-priv static boolean isRegistered(final Object value) Tests whether the registry contains the given object. Used by the reflection methods to avoid infinite loops.
| |
| reflectionAppend | back to summary |
|---|---|
| private static void reflectionAppend(final Object object, final Class<?> clazz, final HashCodeBuilder builder, final boolean useTransients, final String[] excludeFields) Appends the fields and values defined by the given object of the given
| |
| reflectionHashCode | back to summary |
|---|---|
| public static int reflectionHashCode(final int initialNonZeroOddNumber, final int multiplierNonZeroOddNumber, final Object object) Uses reflection to build a valid hash code from the fields of
It uses
Transient members will be not be used, as they are likely derived fields, and not part of the value of the
Static fields will not be tested. Superclass fields will be included. 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.
| |
| reflectionHashCode | back to summary |
|---|---|
| public static int reflectionHashCode(final int initialNonZeroOddNumber, final int multiplierNonZeroOddNumber, final Object object, final boolean testTransients) Uses reflection to build a valid hash code from the fields of
It uses
If the TestTransients parameter is set to Static fields will not be tested. Superclass fields will be included. 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.
| |
| reflectionHashCode | back to summary |
|---|---|
| public static <T> int reflectionHashCode(final int initialNonZeroOddNumber, final int multiplierNonZeroOddNumber, final T object, final boolean testTransients, final Class<? super T> reflectUpToClass, final String... excludeFields) Uses reflection to build a valid hash code from the fields of
It uses
If the TestTransients parameter is set to Static fields will not be included. Superclass fields will be included up to and including the specified superclass. A null superclass is treated as java.lang.Object. 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.
| |
| reflectionHashCode | back to summary |
|---|---|
| public static int reflectionHashCode(final Object object, final boolean testTransients) Uses reflection to build a valid hash code from the fields of This constructor uses two hard coded choices for the constants needed to build a hash code.
It uses
If the TestTransients parameter is set to Static fields will not be tested. Superclass fields will be included. If no fields are found to include in the hash code, the result of this method will be constant.
| |
| reflectionHashCode | back to summary |
|---|---|
| public static int reflectionHashCode(final Object object, final Collection<String> excludeFields) Uses reflection to build a valid hash code from the fields of This constructor uses two hard coded choices for the constants needed to build a hash code.
It uses
Transient members will be not be used, as they are likely derived fields, and not part of the value of the
Static fields will not be tested. Superclass fields will be included. If no fields are found to include in the hash code, the result of this method will be constant.
| |
| reflectionHashCode | back to summary |
|---|---|
| public static int reflectionHashCode(final Object object, final String... excludeFields) Uses reflection to build a valid hash code from the fields of This constructor uses two hard coded choices for the constants needed to build a hash code.
It uses
Transient members will be not be used, as they are likely derived fields, and not part of the value of the
Static fields will not be tested. Superclass fields will be included. If no fields are found to include in the hash code, the result of this method will be constant.
| |
| register | back to summary |
|---|---|
| private static void register(final Object value) Registers the given object. Used by the reflection methods to avoid infinite loops.
| |
| toHashCode | back to summary |
|---|---|
| public int toHashCode() Returns the computed
| |
| unregister | back to summary |
|---|---|
| private static void unregister(final Object value) Unregisters the given object. Used by the reflection methods to avoid infinite loops.
| |