Made it clear that the fields of the object form the hash code (LANG-1039).

Pulled magic values into constants.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1627889 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Duncan Jones 2014-09-26 21:19:12 +00:00
parent bdb1a684a7
commit bc91bedd45
1 changed files with 32 additions and 14 deletions

View File

@ -99,6 +99,16 @@ import org.apache.commons.lang3.Validate;
* @version $Id$
*/
public class HashCodeBuilder implements Builder<Integer> {
/**
* The default initial value to use in reflection hash code building.
*/
private static final int DEFAULT_INITIAL_VALUE = 17;
/**
* The default multipler value to use in reflection hash code building.
*/
private static final int DEFAULT_MULTIPLIER_VALUE = 37;
/**
* <p>
* A registry of objects used by reflection methods to detect cyclical object references and avoid infinite loops.
@ -200,7 +210,7 @@ public class HashCodeBuilder implements Builder<Integer> {
/**
* <p>
* This method uses reflection to build a valid hash code.
* Uses reflection to build a valid hash code from the fields of {@code object}.
* </p>
*
* <p>
@ -224,7 +234,8 @@ public class HashCodeBuilder implements Builder<Integer> {
* </p>
*
* @param initialNonZeroOddNumber
* a non-zero, odd number used as the initial value
* a 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
* @param multiplierNonZeroOddNumber
* a non-zero, odd number used as the multiplier
* @param object
@ -241,7 +252,7 @@ public class HashCodeBuilder implements Builder<Integer> {
/**
* <p>
* This method uses reflection to build a valid hash code.
* Uses reflection to build a valid hash code from the fields of {@code object}.
* </p>
*
* <p>
@ -265,7 +276,8 @@ public class HashCodeBuilder implements Builder<Integer> {
* </p>
*
* @param initialNonZeroOddNumber
* a non-zero, odd number used as the initial value
* a 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
* @param multiplierNonZeroOddNumber
* a non-zero, odd number used as the multiplier
* @param object
@ -285,7 +297,7 @@ public class HashCodeBuilder implements Builder<Integer> {
/**
* <p>
* This method uses reflection to build a valid hash code.
* Uses reflection to build a valid hash code from the fields of {@code object}.
* </p>
*
* <p>
@ -312,7 +324,8 @@ public class HashCodeBuilder implements Builder<Integer> {
* @param <T>
* the type of the object involved
* @param initialNonZeroOddNumber
* a non-zero, odd number used as the initial value
* a 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
* @param multiplierNonZeroOddNumber
* a non-zero, odd number used as the multiplier
* @param object
@ -348,7 +361,7 @@ public class HashCodeBuilder implements Builder<Integer> {
/**
* <p>
* This method uses reflection to build a valid hash code.
* Uses reflection to build a valid hash code from the fields of {@code object}.
* </p>
*
* <p>
@ -367,7 +380,8 @@ public class HashCodeBuilder implements Builder<Integer> {
* </p>
*
* <p>
* Static fields will not be tested. Superclass fields will be included.
* 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.
* </p>
*
* @param object
@ -379,12 +393,13 @@ public class HashCodeBuilder implements Builder<Integer> {
* if the object is <code>null</code>
*/
public static int reflectionHashCode(final Object object, final boolean testTransients) {
return reflectionHashCode(17, 37, object, testTransients, null);
return reflectionHashCode(DEFAULT_INITIAL_VALUE, DEFAULT_MULTIPLIER_VALUE, object,
testTransients, null);
}
/**
* <p>
* This method uses reflection to build a valid hash code.
* Uses reflection to build a valid hash code from the fields of {@code object}.
* </p>
*
* <p>
@ -403,7 +418,8 @@ public class HashCodeBuilder implements Builder<Integer> {
* </p>
*
* <p>
* Static fields will not be tested. Superclass fields will be included.
* 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.
* </p>
*
* @param object
@ -422,7 +438,7 @@ public class HashCodeBuilder implements Builder<Integer> {
/**
* <p>
* This method uses reflection to build a valid hash code.
* Uses reflection to build a valid hash code from the fields of {@code object}.
* </p>
*
* <p>
@ -441,7 +457,8 @@ public class HashCodeBuilder implements Builder<Integer> {
* </p>
*
* <p>
* Static fields will not be tested. Superclass fields will be included.
* 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.
* </p>
*
* @param object
@ -453,7 +470,8 @@ public class HashCodeBuilder implements Builder<Integer> {
* if the object is <code>null</code>
*/
public static int reflectionHashCode(final Object object, final String... excludeFields) {
return reflectionHashCode(17, 37, object, false, null, excludeFields);
return reflectionHashCode(DEFAULT_INITIAL_VALUE, DEFAULT_MULTIPLIER_VALUE, object, false,
null, excludeFields);
}
/**