Return-Path:
* Assists in implementing {@link Object#hashCode()} methods.
*
* This class enables a good
- * 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 hashcode of 674, namely 17 * 37 + 45.
+ * 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 hashcode of 674, namely 17 * 37 + 45.
*
* All relevant fields from the object should be included in the
* To use this class write code as follows:
*
* If required, the superclass
* Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are
* usually private, the method,
* A typical invocation for this method would look like:
*
* A registry of objects used by reflection methods to detect cyclical object references and avoid infinite loops.
*
* Returns the registry of objects being traversed by the reflection methods in the current thread.
* 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.
* hashCode
method. Derived fields may be
* excluded. In general, any field used in the equals
method must be used in the hashCode
@@ -53,7 +53,7 @@ import org.apache.commons.lang3.ArrayUti
*
* public class Person {
* String name;
@@ -72,28 +72,28 @@ import org.apache.commons.lang3.ArrayUti
* }
* }
*
- *
+ *
* hashCode()
can be added using {@link #appendSuper}.
* 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.
*
* public int hashCode() {
* return HashCodeBuilder.reflectionHashCode(this);
* }
*
- *
+ *
* @author Apache Software Foundation
* @author Gary Gregory
* @author Pete Gieser
@@ -105,7 +105,7 @@ public class HashCodeBuilder implements
* true
if the registry contains the given object. Used by the reflection methods to avoid
* infinite loops.
*
true
if the registry contains the given object.
@@ -159,7 +159,7 @@ public class HashCodeBuilder implements
*
* Appends the fields and values defined by the given object of the given Class
.
*
* This method uses reflection to build a valid hash code. *
- * + * *
* It uses AccessibleObject.setAccessible
to gain access to private fields. This means that it will
* throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* also not as efficient as testing explicitly.
*
* Transient members will be not be used, as they are likely derived fields, and not part of the value of the
* Object
.
*
* 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. *
- * + * * @param initialNonZeroOddNumber * a non-zero, odd number used as the initial value * @param multiplierNonZeroOddNumber @@ -245,27 +245,27 @@ public class HashCodeBuilder implements ** This method uses reflection to build a valid hash code. *
- * + * *
* It uses AccessibleObject.setAccessible
to gain access to private fields. This means that it will
* throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* also not as efficient as testing explicitly.
*
* If the TestTransients parameter is set to true
, transient members will be tested, otherwise they
* are ignored, as they are likely derived fields, and not part of the value of the Object
.
*
* 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. *
- * + * * @param initialNonZeroOddNumber * a non-zero, odd number used as the initial value * @param multiplierNonZeroOddNumber @@ -289,7 +289,9 @@ public class HashCodeBuilder implements /** * Calls {@link #reflectionHashCode(int, int, Object, boolean, Class, String[])} with excludeFields set to *null
.
- *
+ *
+ * @param * This method uses reflection to build a valid hash code. *
- * + * *
* It uses AccessibleObject.setAccessible
to gain access to private fields. This means that it will
* throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* also not as efficient as testing explicitly.
*
* If the TestTransients parameter is set to true
, transient members will be tested, otherwise they
* are ignored, as they are likely derived fields, and not part of the value of the Object
.
*
* 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. *
- * + * + * @param* This method uses reflection to build a valid hash code. *
- * + * ** This constructor uses two hard coded choices for the constants needed to build a hash code. *
- * + * *
* It uses AccessibleObject.setAccessible
to gain access to private fields. This means that it will
* throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* also not as efficient as testing explicitly.
*
* Transient members will be not be used, as they are likely derived fields, and not part of the value of the
* Object
.
*
* Static fields will not be tested. Superclass fields will be included. *
- * + * * @param object * the Object to create ahashCode
for
* @return int hash code
@@ -407,26 +411,26 @@ public class HashCodeBuilder implements
* * This method uses reflection to build a valid hash code. *
- * + * ** This constructor uses two hard coded choices for the constants needed to build a hash code. *
- * + * *
* It uses AccessibleObject.setAccessible
to gain access to private fields. This means that it will
* throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* also not as efficient as testing explicitly.
*
* If the TestTransients parameter is set to true
, transient members will be tested, otherwise they
* are ignored, as they are likely derived fields, and not part of the value of the Object
.
*
* Static fields will not be tested. Superclass fields will be included. *
- * + * * @param object * the Object to create ahashCode
for
* @param testTransients
@@ -443,26 +447,26 @@ public class HashCodeBuilder implements
* * This method uses reflection to build a valid hash code. *
- * + * ** This constructor uses two hard coded choices for the constants needed to build a hash code. *
- * + * *
* It uses AccessibleObject.setAccessible
to gain access to private fields. This means that it will
* throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* also not as efficient as testing explicitly.
*
* Transient members will be not be used, as they are likely derived fields, and not part of the value of the
* Object
.
*
* Static fields will not be tested. Superclass fields will be included. *
- * + * * @param object * the Object to create ahashCode
for
* @param excludeFields
@@ -481,26 +485,26 @@ public class HashCodeBuilder implements
* * This method uses reflection to build a valid hash code. *
- * + * ** This constructor uses two hard coded choices for the constants needed to build a hash code. *
- * + * *
* It uses AccessibleObject.setAccessible
to gain access to private fields. This means that it will
* throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* also not as efficient as testing explicitly.
*
* Transient members will be not be used, as they are likely derived fields, and not part of the value of the
* Object
.
*
* Static fields will not be tested. Superclass fields will be included. *
- * + * * @param object * the Object to create ahashCode
for
* @param excludeFields
@@ -517,7 +521,7 @@ public class HashCodeBuilder implements
* * Registers the given object. Used by the reflection methods to avoid infinite loops. *
- * + * * @param value * The object to register. */ @@ -534,10 +538,10 @@ public class HashCodeBuilder implements ** Unregisters the given object. *
- * + * ** Used by the reflection methods to avoid infinite loops. - * + * * @param value * The object to unregister. * @since 2.3 @@ -581,11 +585,11 @@ public class HashCodeBuilder implements * 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. *
- * + * * @param initialNonZeroOddNumber * a non-zero, odd number used as the initial value * @param multiplierNonZeroOddNumber @@ -626,7 +630,7 @@ public class HashCodeBuilder implements *
* This is in accordance with the Effective Java
design.
*
hashCode
* @return this
@@ -640,7 +644,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a boolean
array.
*
hashCode
* @return this
@@ -662,7 +666,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a byte
.
*
hashCode
* @return this
@@ -678,7 +682,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a byte
array.
*
hashCode
* @return this
@@ -698,7 +702,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a char
.
*
hashCode
* @return this
@@ -712,7 +716,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a char
array.
*
hashCode
* @return this
@@ -732,7 +736,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a double
.
*
hashCode
* @return this
@@ -745,7 +749,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a double
array.
*
hashCode
* @return this
@@ -765,7 +769,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a float
.
*
hashCode
* @return this
@@ -779,7 +783,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a float
array.
*
hashCode
* @return this
@@ -799,7 +803,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for an int
.
*
hashCode
* @return this
@@ -813,7 +817,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for an int
array.
*
hashCode
* @return this
@@ -833,14 +837,14 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a long
.
*
hashCode
* @return this
*/
- // NOTE: This method uses >> and not >>> as Effective Java and
- // Long.hashCode do. Ideally we should switch to >>> at
- // some stage. There are backwards compat issues, so
+ // NOTE: This method uses >> and not >>> as Effective Java and
+ // Long.hashCode do. Ideally we should switch to >>> at
+ // some stage. There are backwards compat issues, so
// that will have to wait for the time being. cf LANG-342.
public HashCodeBuilder append(long value) {
iTotal = iTotal * iConstant + ((int) (value ^ (value >> 32)));
@@ -851,7 +855,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a long
array.
*
hashCode
* @return this
@@ -871,7 +875,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for an Object
.
*
hashCode
* @return this
@@ -915,7 +919,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for an Object
array.
*
hashCode
* @return this
@@ -935,7 +939,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a short
.
*
hashCode
* @return this
@@ -949,7 +953,7 @@ public class HashCodeBuilder implements
*
* Append a hashCode
for a short
array.
*
hashCode
* @return this
@@ -969,7 +973,7 @@ public class HashCodeBuilder implements
* * Adds the result of super.hashCode() to this builder. *
- * + * * @param superHashCode * the result of callingsuper.hashCode()
* @return this HashCodeBuilder, used to chain calls.
@@ -984,18 +988,18 @@ public class HashCodeBuilder implements
*
* Return the computed hashCode
.
*
hashCode
based on the fields appended
*/
public int toHashCode() {
return iTotal;
}
-
+
/**
* Returns the computed hashCode
.
- *
+ *
* @return hashCode
based on the fields appended
- *
+ *
* @since 3.0
*/
public Integer build() {
@@ -1004,10 +1008,10 @@ public class HashCodeBuilder implements
/**
*
- * The computed hashCode
from toHashCode() is returned due to the likelyhood
- * of bugs in mis-calling toHashCode() and the unlikelyness of it mattering what the hashCode for
+ * The computed hashCode
from toHashCode() is returned due to the likelyhood
+ * of bugs in mis-calling toHashCode() and the unlikelyness of it mattering what the hashCode for
* HashCodeBuilder itself is.
- *
+ *
* @return hashCode
based on the fields appended
* @since 2.5
*/