mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-11 12:35:07 +00:00
Javadoc patch for java example.
Submitted by: Christopher M. Judd <cjudd@columbus.rr.com> git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137225 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f76f9bf04d
commit
a46f4fec1d
@ -76,7 +76,7 @@
|
|||||||
* int age;
|
* int age;
|
||||||
* boolean isSmoker;
|
* boolean isSmoker;
|
||||||
* ...
|
* ...
|
||||||
*
|
*
|
||||||
* public int hashCode() {
|
* public int hashCode() {
|
||||||
* // you pick a hard-coded, randomly chosen, non-zero, odd number
|
* // you pick a hard-coded, randomly chosen, non-zero, odd number
|
||||||
* // ideally different for each class
|
* // ideally different for each class
|
||||||
@ -88,29 +88,29 @@
|
|||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <p>If required, the superclass hashCode can be added using {@link #appendSuper}.</p>
|
* <p>If required, the superclass hashCode can be added using {@link #appendSuper}.</p>
|
||||||
*
|
*
|
||||||
* <p>Alternatively, there is a method that uses reflection to determine
|
* <p>Alternatively, there is a method that uses reflection to determine
|
||||||
* the fields to test. Because these fields are usually private, the method,
|
* the fields to test. Because these fields are usually private, the method,
|
||||||
* <code>reflectionHashCode</code>, uses <code>Field.setAccessible</code> to
|
* <code>reflectionHashCode</code>, uses <code>Field.setAccessible</code> to
|
||||||
* change the visibility of the fields. This will fail under a security manager,
|
* 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
|
* unless the appropriate permissions are set up correctly. It is also slower
|
||||||
* than testing explicitly.</p>
|
* than testing explicitly.</p>
|
||||||
*
|
*
|
||||||
* <p>A typical invocation for this method would look like:</p>
|
* <p>A typical invocation for this method would look like:</p>
|
||||||
* <pre>
|
* <pre>
|
||||||
* public boolean hashCode(Object o) {
|
* public int hashCode() {
|
||||||
* return HashCodeBuilder.reflectionHashCode(this);
|
* return HashCodeBuilder.reflectionHashCode(this);
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
* @version $Id: HashCodeBuilder.java,v 1.7 2002/12/23 00:20:31 scolebourne Exp $
|
* @version $Id: HashCodeBuilder.java,v 1.8 2003/01/15 20:51:57 bayard Exp $
|
||||||
*/
|
*/
|
||||||
public class HashCodeBuilder {
|
public class HashCodeBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constant to use in building the hashCode
|
* Constant to use in building the hashCode
|
||||||
*/
|
*/
|
||||||
@ -119,7 +119,7 @@ public class HashCodeBuilder {
|
|||||||
* Running total of the hashCode
|
* Running total of the hashCode
|
||||||
*/
|
*/
|
||||||
private int iTotal = 0;
|
private int iTotal = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Constructor for HashCodeBuilder.</p>
|
* <p>Constructor for HashCodeBuilder.</p>
|
||||||
*
|
*
|
||||||
@ -131,7 +131,7 @@ public HashCodeBuilder() {
|
|||||||
iConstant = 37;
|
iConstant = 37;
|
||||||
iTotal = 17;
|
iTotal = 17;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Constructor for <code>HashCodeBuilder</code>.</p>
|
* <p>Constructor for <code>HashCodeBuilder</code>.</p>
|
||||||
*
|
*
|
||||||
@ -140,7 +140,7 @@ public HashCodeBuilder() {
|
|||||||
* not vital.</p>
|
* not vital.</p>
|
||||||
*
|
*
|
||||||
* <p>Prime numbers are preferred, especially for the multiplier.</p>
|
* <p>Prime numbers are preferred, especially for the multiplier.</p>
|
||||||
*
|
*
|
||||||
* @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
|
* @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
|
||||||
* @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
|
* @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
|
||||||
* @throws IllegalArgumentException if the number is zero or even
|
* @throws IllegalArgumentException if the number is zero or even
|
||||||
@ -164,7 +164,7 @@ public HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumb
|
|||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>This method uses reflection to build a valid hash code.</p>
|
* <p>This method uses reflection to build a valid hash code.</p>
|
||||||
*
|
*
|
||||||
@ -214,7 +214,7 @@ public static int reflectionHashCode(Object object) {
|
|||||||
public static int reflectionHashCode(Object object, boolean testTransients) {
|
public static int reflectionHashCode(Object object, boolean testTransients) {
|
||||||
return reflectionHashCode(17, 37, object, testTransients);
|
return reflectionHashCode(17, 37, object, testTransients);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>This method uses reflection to build a valid hash code.</p>
|
* <p>This method uses reflection to build a valid hash code.</p>
|
||||||
*
|
*
|
||||||
@ -231,7 +231,7 @@ public static int reflectionHashCode(Object object, boolean testTransients) {
|
|||||||
* <p>Two randomly chosen, non-zero, odd numbers must be passed in. Ideally
|
* <p>Two randomly chosen, non-zero, odd numbers must be passed in. Ideally
|
||||||
* these should be different for each class, however this is not vital.
|
* these should be different for each class, however this is not vital.
|
||||||
* Prime numbers are preferred, especially for the multiplier.</p>
|
* Prime numbers are preferred, especially for the multiplier.</p>
|
||||||
*
|
*
|
||||||
* @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
|
* @param initialNonZeroOddNumber a non-zero, odd number used as the initial value
|
||||||
* @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
|
* @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier
|
||||||
* @param object the Object to create a <code>hashCode</code> for
|
* @param object the Object to create a <code>hashCode</code> for
|
||||||
@ -240,11 +240,11 @@ public static int reflectionHashCode(Object object, boolean testTransients) {
|
|||||||
* @throws IllegalArgumentException if the number is zero or even
|
* @throws IllegalArgumentException if the number is zero or even
|
||||||
*/
|
*/
|
||||||
public static int reflectionHashCode(
|
public static int reflectionHashCode(
|
||||||
int initialNonZeroOddNumber, int multiplierNonZeroOddNumber,
|
int initialNonZeroOddNumber, int multiplierNonZeroOddNumber,
|
||||||
Object object) {
|
Object object) {
|
||||||
return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, false);
|
return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>This method uses reflection to build a valid hash code.</p>
|
* <p>This method uses reflection to build a valid hash code.</p>
|
||||||
*
|
*
|
||||||
@ -262,7 +262,7 @@ public static int reflectionHashCode(
|
|||||||
* <p>Two randomly chosen, non-zero, odd numbers must be passed in. Ideally
|
* <p>Two randomly chosen, non-zero, odd numbers must be passed in. Ideally
|
||||||
* these should be different for each class, however this is not vital.
|
* these should be different for each class, however this is not vital.
|
||||||
* Prime numbers are preferred, especially for the multiplier.</p>
|
* Prime numbers are preferred, especially for the multiplier.</p>
|
||||||
*
|
*
|
||||||
* @param initialNonZeroOddNumber
|
* @param initialNonZeroOddNumber
|
||||||
* @param multiplierNonZeroOddNumber
|
* @param multiplierNonZeroOddNumber
|
||||||
* @param object the Object to create a <code>hashCode</code> for
|
* @param object the Object to create a <code>hashCode</code> for
|
||||||
@ -274,7 +274,7 @@ public static int reflectionHashCode(
|
|||||||
public static int reflectionHashCode(
|
public static int reflectionHashCode(
|
||||||
int initialNonZeroOddNumber, int multiplierNonZeroOddNumber,
|
int initialNonZeroOddNumber, int multiplierNonZeroOddNumber,
|
||||||
Object object, boolean testTransients) {
|
Object object, boolean testTransients) {
|
||||||
|
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
throw new IllegalArgumentException("The object to build a hash code for must not be null");
|
throw new IllegalArgumentException("The object to build a hash code for must not be null");
|
||||||
}
|
}
|
||||||
@ -299,7 +299,7 @@ public static int reflectionHashCode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Adds the result of super.hashCode() to this builder.</p>
|
* <p>Adds the result of super.hashCode() to this builder.</p>
|
||||||
*
|
*
|
||||||
@ -310,9 +310,9 @@ public HashCodeBuilder appendSuper(int superHashCode) {
|
|||||||
iTotal = iTotal * iConstant + superHashCode;
|
iTotal = iTotal * iConstant + superHashCode;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Append a <code>hashCode</code> for an <code>Object</code>.</p>
|
* <p>Append a <code>hashCode</code> for an <code>Object</code>.</p>
|
||||||
*
|
*
|
||||||
@ -322,12 +322,12 @@ public HashCodeBuilder appendSuper(int superHashCode) {
|
|||||||
public HashCodeBuilder append(Object object) {
|
public HashCodeBuilder append(Object object) {
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
iTotal = iTotal * iConstant;
|
iTotal = iTotal * iConstant;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (object.getClass().isArray() == false) {
|
if (object.getClass().isArray() == false) {
|
||||||
//the simple case, not an array, just the element
|
//the simple case, not an array, just the element
|
||||||
iTotal = iTotal * iConstant + object.hashCode();
|
iTotal = iTotal * iConstant + object.hashCode();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//'Switch' on type of array, to dispatch to the correct handler
|
//'Switch' on type of array, to dispatch to the correct handler
|
||||||
// This handles multi dimensional arrays
|
// This handles multi dimensional arrays
|
||||||
@ -347,7 +347,7 @@ public HashCodeBuilder append(Object object) {
|
|||||||
append((float[]) object);
|
append((float[]) object);
|
||||||
} else if (object instanceof boolean[]) {
|
} else if (object instanceof boolean[]) {
|
||||||
append((boolean[]) object);
|
append((boolean[]) object);
|
||||||
} else {
|
} else {
|
||||||
// Not an array of primitives
|
// Not an array of primitives
|
||||||
append((Object[]) object);
|
append((Object[]) object);
|
||||||
}
|
}
|
||||||
@ -598,9 +598,9 @@ public HashCodeBuilder append(boolean[] array) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Return the computed <code>hashCode</code>.</p>
|
* <p>Return the computed <code>hashCode</code>.</p>
|
||||||
*
|
*
|
||||||
* @return <code>hashCode</code> based on the fields appended
|
* @return <code>hashCode</code> based on the fields appended
|
||||||
*/
|
*/
|
||||||
public int toHashCode() {
|
public int toHashCode() {
|
||||||
return iTotal;
|
return iTotal;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user