Add ability to build compound hashCode using superclass

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137175 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2002-12-08 21:22:42 +00:00
parent bd5f9fc92a
commit 3129990f42
2 changed files with 25 additions and 3 deletions

View File

@ -80,7 +80,7 @@ import java.lang.reflect.Modifier;
* 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
* return new HashCodeBuilder(17, 37). * return new HashCodeBuilder(17, 37).
* append(name). * append(name).
* append(age). * append(age).
* append(smoker). * append(smoker).
@ -88,6 +88,8 @@ import java.lang.reflect.Modifier;
* } * }
* } * }
* </pre> * </pre>
*
* <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,
@ -104,7 +106,7 @@ import java.lang.reflect.Modifier;
* </pre> * </pre>
* *
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: HashCodeBuilder.java,v 1.5 2002/11/17 21:46:42 scolebourne Exp $ * @version $Id: HashCodeBuilder.java,v 1.6 2002/12/08 21:22:07 scolebourne Exp $
*/ */
public class HashCodeBuilder { public class HashCodeBuilder {
@ -297,6 +299,19 @@ public class HashCodeBuilder {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
/**
* <p>Adds the result of super.hashCode() to this builder.</p>
*
* @param superHashCode the result of calling <code>super.hashCode()</code>
* @return this HashCodeBuilder, used to chain calls.
*/
public HashCodeBuilder appendSuper(int superHashCode) {
iTotal = iTotal * iConstant + superHashCode;
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>
* *

View File

@ -61,7 +61,7 @@ import junit.textui.TestRunner;
* Unit tests {@link org.apache.commons.lang.HashCodeBuilder}. * Unit tests {@link org.apache.commons.lang.HashCodeBuilder}.
* *
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: HashCodeBuilderTest.java,v 1.1 2002/09/12 22:01:00 scolebourne Exp $ * @version $Id: HashCodeBuilderTest.java,v 1.2 2002/12/08 21:22:42 scolebourne Exp $
*/ */
public class HashCodeBuilderTest extends TestCase { public class HashCodeBuilderTest extends TestCase {
@ -166,6 +166,13 @@ public class HashCodeBuilderTest extends TestCase {
fail(); fail();
} }
public void testSuper() {
Object obj = new Object();
assertEquals(17 * 37 + (19 * 41 + obj.hashCode()), new HashCodeBuilder(17, 37).appendSuper(
new HashCodeBuilder(19, 41).append(obj).toHashCode()
).toHashCode());
}
public void testObject() { public void testObject() {
Object obj = null; Object obj = null;
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode()); assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());