Change HashCodeUtils to HashCodeBuilder
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@136970 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
da49474963
commit
3e5b0bd6a0
|
@ -1,5 +1,7 @@
|
|||
package org.apache.commons.lang;
|
||||
|
||||
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
|
@ -76,295 +78,332 @@ package org.apache.commons.lang;
|
|||
* ...
|
||||
*
|
||||
* public int hashCode() {
|
||||
* int total = 17; // you pick a random, non-zero, odd number
|
||||
* total = HashCodeUtils.buildHashCode(total, name);
|
||||
* total = HashCodeUtils.buildHashCode(total, age);
|
||||
* total = HashCodeUtils.buildHashCode(total, isSmoker);
|
||||
* return total;
|
||||
* // you pick a hard-coded, randomly chosen, non-zero, odd number
|
||||
* // ideally different for each class
|
||||
* return new HashCodeBuilder(17).
|
||||
* append(name).
|
||||
* append(age).
|
||||
* append(smoker).
|
||||
* toHashCode();
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: HashCodeUtils.java,v 1.2 2002/08/11 17:52:21 dlr Exp $
|
||||
* @version $Id: HashCodeBuilder.java,v 1.1 2002/08/15 22:37:29 scolebourne Exp $
|
||||
*/
|
||||
public class HashCodeUtils {
|
||||
public class HashCodeBuilder {
|
||||
|
||||
/**
|
||||
* According to Bloch, the multiplier should be a random, odd
|
||||
* prime.
|
||||
* Constant to use in building the hashCode
|
||||
*/
|
||||
private static final int CONSTANT = 37;
|
||||
private final int iConstant;
|
||||
/**
|
||||
* Running total of the hashCode
|
||||
*/
|
||||
private int iTotal = 0;
|
||||
|
||||
/**
|
||||
* Prevent construction of HashCodeUtils instances
|
||||
* Constructor for HashCodeBuilder.
|
||||
* This constructor uses two hard coded choices for the constants needed
|
||||
* to build a hashCode.
|
||||
*/
|
||||
private HashCodeUtils() {
|
||||
public HashCodeBuilder() {
|
||||
super();
|
||||
iConstant = 37;
|
||||
iTotal = 17;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for HashCodeBuilder.
|
||||
* 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
|
||||
* @param multiplierNonZeroOddNumber
|
||||
* @throws IllegalArgumentException if the number is zero or even
|
||||
*/
|
||||
public HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) {
|
||||
super();
|
||||
if (initialNonZeroOddNumber == 0) {
|
||||
throw new IllegalArgumentException("HashCodeBuilder requires a non zero initial value");
|
||||
}
|
||||
if (initialNonZeroOddNumber % 2 == 0) {
|
||||
throw new IllegalArgumentException("HashCodeBuilder requires an odd initial value");
|
||||
}
|
||||
if (multiplierNonZeroOddNumber == 0) {
|
||||
throw new IllegalArgumentException("HashCodeBuilder requires a non zero multiplier");
|
||||
}
|
||||
if (multiplierNonZeroOddNumber % 2 == 0) {
|
||||
throw new IllegalArgumentException("HashCodeBuilder requires an odd multiplier");
|
||||
}
|
||||
iConstant = multiplierNonZeroOddNumber;
|
||||
iTotal = initialNonZeroOddNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for an Object.
|
||||
* Append a hashCode for an Object.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param object the object to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, Object object) {
|
||||
public HashCodeBuilder append(Object object) {
|
||||
if (object == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
return totalSoFar * CONSTANT + object.hashCode();
|
||||
iTotal = iTotal * iConstant + object.hashCode();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a long.
|
||||
* Append a hashCode for a long.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param value the long to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, long value) {
|
||||
return totalSoFar * CONSTANT + ((int) (value ^ (value >> 32)));
|
||||
public HashCodeBuilder append(long value) {
|
||||
iTotal = iTotal * iConstant + ((int) (value ^ (value >> 32)));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for an int.
|
||||
* Append a hashCode for an int.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param value the int to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, int value) {
|
||||
return totalSoFar * CONSTANT + value;
|
||||
public HashCodeBuilder append(int value) {
|
||||
iTotal = iTotal * iConstant + value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a short.
|
||||
* Append a hashCode for a short.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param value the short to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, short value) {
|
||||
return totalSoFar * CONSTANT + (int) value;
|
||||
public HashCodeBuilder append(short value) {
|
||||
iTotal = iTotal * iConstant + (int) value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a char.
|
||||
* Append a hashCode for a char.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param value the char to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, char value) {
|
||||
return totalSoFar * CONSTANT + (int) value;
|
||||
public HashCodeBuilder append(char value) {
|
||||
iTotal = iTotal * iConstant + (int) value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a byte.
|
||||
* Append a hashCode for a byte.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param value the byte to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, byte value) {
|
||||
return totalSoFar * CONSTANT + (int) value;
|
||||
public HashCodeBuilder append(byte value) {
|
||||
iTotal = iTotal * iConstant + (int) value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a double.
|
||||
* Append a hashCode for a double.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param value the double to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, double value) {
|
||||
return buildHashCode(totalSoFar, Double.doubleToLongBits(value));
|
||||
public HashCodeBuilder append(double value) {
|
||||
return append(Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a float.
|
||||
* Append a hashCode for a float.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param value the float to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, float value) {
|
||||
return totalSoFar * CONSTANT + Float.floatToIntBits(value);
|
||||
public HashCodeBuilder append(float value) {
|
||||
iTotal = iTotal * iConstant + Float.floatToIntBits(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a long.
|
||||
* Append a hashCode for a long.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param value the long to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, boolean value) {
|
||||
return totalSoFar * CONSTANT + (value ? 0 : 1);
|
||||
public HashCodeBuilder append(boolean value) {
|
||||
iTotal = iTotal * iConstant + (value ? 0 : 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for an Object array.
|
||||
* Append a hashCode for an Object array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, Object[] array) {
|
||||
public HashCodeBuilder append(Object[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a long array.
|
||||
* Append a hashCode for a long array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, long[] array) {
|
||||
public HashCodeBuilder append(long[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for an int array.
|
||||
* Append a hashCode for an int array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, int[] array) {
|
||||
public HashCodeBuilder append(int[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a short array.
|
||||
* Append a hashCode for a short array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, short[] array) {
|
||||
public HashCodeBuilder append(short[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a char array.
|
||||
* Append a hashCode for a char array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, char[] array) {
|
||||
public HashCodeBuilder append(char[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a byte array.
|
||||
* Append a hashCode for a byte array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, byte[] array) {
|
||||
public HashCodeBuilder append(byte[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a double array.
|
||||
* Append a hashCode for a double array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, double[] array) {
|
||||
public HashCodeBuilder append(double[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a float array.
|
||||
* Append a hashCode for a float array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, float[] array) {
|
||||
public HashCodeBuilder append(float[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashCode for a boolean array.
|
||||
* Append a hashCode for a boolean array.
|
||||
*
|
||||
* @param totalSoFar the hashCode total so far
|
||||
* @param array the array to add to the hashCode
|
||||
* @return updated totalSoFar
|
||||
* @return this
|
||||
*/
|
||||
public static int buildHashCode(int totalSoFar, boolean[] array) {
|
||||
public HashCodeBuilder append(boolean[] array) {
|
||||
if (array == null) {
|
||||
return totalSoFar * CONSTANT;
|
||||
iTotal = iTotal * iConstant;
|
||||
} else {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
totalSoFar = buildHashCode(totalSoFar, array[i]);
|
||||
append(array[i]);
|
||||
}
|
||||
return totalSoFar;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the computed hashCode
|
||||
*
|
||||
* @return int hashCode based on the fields appended
|
||||
*/
|
||||
public int toHashCode() {
|
||||
return iTotal;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,262 @@
|
|||
package org.apache.commons.lang;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2002 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import junit.textui.TestRunner;
|
||||
/**
|
||||
* Unit tests {@link org.apache.commons.lang.HashCodeBuilder}.
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: HashCodeBuilderTest.java,v 1.1 2002/08/15 22:37:30 scolebourne Exp $
|
||||
*/
|
||||
public class HashCodeBuilderTest extends TestCase {
|
||||
|
||||
public HashCodeBuilderTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TestRunner.run(suite());
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(HashCodeBuilderTest.class);
|
||||
suite.setName("HashCodeBuilder Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
public void testConstructorEx1() {
|
||||
try {
|
||||
new HashCodeBuilder(0, 0);
|
||||
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return;
|
||||
}
|
||||
fail();
|
||||
}
|
||||
|
||||
public void testConstructorEx2() {
|
||||
try {
|
||||
new HashCodeBuilder(2, 2);
|
||||
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return;
|
||||
}
|
||||
fail();
|
||||
}
|
||||
|
||||
public void testObject() {
|
||||
Object obj = null;
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj = new Object();
|
||||
assertEquals(17 * 37 + obj.hashCode(), new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testLong() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long) 0L).toHashCode());
|
||||
assertEquals(17 * 37 + (int) (123456789L ^ (123456789L >> 32)), new HashCodeBuilder(17, 37).append((long) 123456789L).toHashCode());
|
||||
}
|
||||
|
||||
public void testInt() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int) 0).toHashCode());
|
||||
assertEquals(17 * 37 + 123456, new HashCodeBuilder(17, 37).append((int) 123456).toHashCode());
|
||||
}
|
||||
|
||||
public void testShort() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((short) 0).toHashCode());
|
||||
assertEquals(17 * 37 + 12345, new HashCodeBuilder(17, 37).append((short) 12345).toHashCode());
|
||||
}
|
||||
|
||||
public void testChar() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((char) 0).toHashCode());
|
||||
assertEquals(17 * 37 + 1234, new HashCodeBuilder(17, 37).append((char) 1234).toHashCode());
|
||||
}
|
||||
|
||||
public void testByte() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte) 0).toHashCode());
|
||||
assertEquals(17 * 37 + 123, new HashCodeBuilder(17, 37).append((byte) 123).toHashCode());
|
||||
}
|
||||
|
||||
public void testDouble() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((double) 0d).toHashCode());
|
||||
double d = 1234567.89;
|
||||
long l = Double.doubleToLongBits(d);
|
||||
assertEquals(17 * 37 + (int) (l ^ (l >> 32)), new HashCodeBuilder(17, 37).append(d).toHashCode());
|
||||
}
|
||||
|
||||
public void testFloat() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((float) 0f).toHashCode());
|
||||
float f = 1234.89f;
|
||||
int i = Float.floatToIntBits(f);
|
||||
assertEquals(17 * 37 + i, new HashCodeBuilder(17, 37).append(f).toHashCode());
|
||||
}
|
||||
|
||||
public void testBoolean() {
|
||||
assertEquals(17 * 37 + 0, new HashCodeBuilder(17, 37).append(true).toHashCode());
|
||||
assertEquals(17 * 37 + 1, new HashCodeBuilder(17, 37).append(false).toHashCode());
|
||||
}
|
||||
|
||||
public void testObjectArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((Object[]) null).toHashCode());
|
||||
Object[] obj = new Object[2];
|
||||
assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = new Object();
|
||||
assertEquals((17 * 37 + obj[0].hashCode()) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = new Object();
|
||||
assertEquals( (17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testLongArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((long[]) null).toHashCode());
|
||||
long[] obj = new long[2];
|
||||
assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = 5L;
|
||||
int h1 = (int) (5L ^ (5L >> 32));
|
||||
assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = 6L;
|
||||
int h2 = (int) (6L ^ (6L >> 32));
|
||||
assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testIntArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((int[]) null).toHashCode());
|
||||
int[] obj = new int[2];
|
||||
assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = 5;
|
||||
assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = 6;
|
||||
assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testShortArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((short[]) null).toHashCode());
|
||||
short[] obj = new short[2];
|
||||
assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = (short) 5;
|
||||
assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = (short) 6;
|
||||
assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testCharArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((char[]) null).toHashCode());
|
||||
char[] obj = new char[2];
|
||||
assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = (char) 5;
|
||||
assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = (char) 6;
|
||||
assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testByteArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((byte[]) null).toHashCode());
|
||||
byte[] obj = new byte[2];
|
||||
assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = (byte) 5;
|
||||
assertEquals((17 * 37 + 5) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = (byte) 6;
|
||||
assertEquals( (17 * 37 + 5) * 37 + 6, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testDoubleArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((double[]) null).toHashCode());
|
||||
double[] obj = new double[2];
|
||||
assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = 5.4d;
|
||||
long l1 = Double.doubleToLongBits(5.4d);
|
||||
int h1 = (int) (l1 ^ (l1 >> 32));
|
||||
assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = 6.3d;
|
||||
long l2 = Double.doubleToLongBits(6.3d);
|
||||
int h2 = (int) (l2 ^ (l2 >> 32));
|
||||
assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testFloatArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((float[]) null).toHashCode());
|
||||
float[] obj = new float[2];
|
||||
assertEquals((17 * 37) * 37 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = 5.4f;
|
||||
int h1 = Float.floatToIntBits(5.4f);
|
||||
assertEquals((17 * 37 + h1) * 37, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = 6.3f;
|
||||
int h2 = Float.floatToIntBits(6.3f);
|
||||
assertEquals( (17 * 37 + h1) * 37 + h2, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
public void testBooleanArray() {
|
||||
assertEquals(17 * 37, new HashCodeBuilder(17, 37).append((boolean[]) null).toHashCode());
|
||||
boolean[] obj = new boolean[2];
|
||||
assertEquals((17 * 37 + 1) * 37 + 1 , new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[0] = true;
|
||||
assertEquals((17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
obj[1] = false;
|
||||
assertEquals( (17 * 37 + 0) * 37 + 1, new HashCodeBuilder(17, 37).append(obj).toHashCode());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,243 +0,0 @@
|
|||
package org.apache.commons.lang;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2002 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import junit.textui.TestRunner;
|
||||
/**
|
||||
* Unit tests {@link org.apache.commons.lang.ObjectUtils}.
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @version $Id: HashCodeUtilsTest.java,v 1.1 2002/08/10 12:13:10 scolebourne Exp $
|
||||
*/
|
||||
public class HashCodeUtilsTest extends TestCase {
|
||||
|
||||
public HashCodeUtilsTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TestRunner.run(suite());
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(HashCodeUtilsTest.class);
|
||||
suite.setName("HashCodeUtils Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
public void testObject() {
|
||||
Object obj = null;
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj = new Object();
|
||||
assertEquals(17 * 37 + obj.hashCode(), HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testLong() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (long) 0L));
|
||||
assertEquals(17 * 37 + (int) (123456789L ^ (123456789L >> 32)), HashCodeUtils.buildHashCode(17, (long) 123456789L));
|
||||
}
|
||||
|
||||
public void testInt() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (int) 0));
|
||||
assertEquals(17 * 37 + 123456, HashCodeUtils.buildHashCode(17, (int) 123456));
|
||||
}
|
||||
|
||||
public void testShort() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (short) 0));
|
||||
assertEquals(17 * 37 + 12345, HashCodeUtils.buildHashCode(17, (short) 12345));
|
||||
}
|
||||
|
||||
public void testChar() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (char) 0));
|
||||
assertEquals(17 * 37 + 1234, HashCodeUtils.buildHashCode(17, (char) 1234));
|
||||
}
|
||||
|
||||
public void testByte() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (byte) 0));
|
||||
assertEquals(17 * 37 + 123, HashCodeUtils.buildHashCode(17, (byte) 123));
|
||||
}
|
||||
|
||||
public void testDouble() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (double) 0d));
|
||||
double d = 1234567.89;
|
||||
long l = Double.doubleToLongBits(d);
|
||||
assertEquals(17 * 37 + (int) (l ^ (l >> 32)), HashCodeUtils.buildHashCode(17, d));
|
||||
}
|
||||
|
||||
public void testFloat() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (float) 0f));
|
||||
float f = 1234.89f;
|
||||
int i = Float.floatToIntBits(f);
|
||||
assertEquals(17 * 37 + i, HashCodeUtils.buildHashCode(17, f));
|
||||
}
|
||||
|
||||
public void testBoolean() {
|
||||
assertEquals(17 * 37 + 0, HashCodeUtils.buildHashCode(17, true));
|
||||
assertEquals(17 * 37 + 1, HashCodeUtils.buildHashCode(17, false));
|
||||
}
|
||||
|
||||
public void testObjectArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (Object[]) null));
|
||||
Object[] obj = new Object[2];
|
||||
assertEquals((17 * 37) * 37 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = new Object();
|
||||
assertEquals((17 * 37 + obj[0].hashCode()) * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = new Object();
|
||||
assertEquals( (17 * 37 + obj[0].hashCode()) * 37 + obj[1].hashCode(), HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testLongArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (long[]) null));
|
||||
long[] obj = new long[2];
|
||||
assertEquals((17 * 37) * 37 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = 5L;
|
||||
int h1 = (int) (5L ^ (5L >> 32));
|
||||
assertEquals((17 * 37 + h1) * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = 6L;
|
||||
int h2 = (int) (6L ^ (6L >> 32));
|
||||
assertEquals( (17 * 37 + h1) * 37 + h2, HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testIntArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (int[]) null));
|
||||
int[] obj = new int[2];
|
||||
assertEquals((17 * 37) * 37 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = 5;
|
||||
assertEquals((17 * 37 + 5) * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = 6;
|
||||
assertEquals( (17 * 37 + 5) * 37 + 6, HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testShortArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (short[]) null));
|
||||
short[] obj = new short[2];
|
||||
assertEquals((17 * 37) * 37 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = (short) 5;
|
||||
assertEquals((17 * 37 + 5) * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = (short) 6;
|
||||
assertEquals( (17 * 37 + 5) * 37 + 6, HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testCharArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (char[]) null));
|
||||
char[] obj = new char[2];
|
||||
assertEquals((17 * 37) * 37 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = (char) 5;
|
||||
assertEquals((17 * 37 + 5) * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = (char) 6;
|
||||
assertEquals( (17 * 37 + 5) * 37 + 6, HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testByteArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (byte[]) null));
|
||||
byte[] obj = new byte[2];
|
||||
assertEquals((17 * 37) * 37 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = (byte) 5;
|
||||
assertEquals((17 * 37 + 5) * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = (byte) 6;
|
||||
assertEquals( (17 * 37 + 5) * 37 + 6, HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testDoubleArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (double[]) null));
|
||||
double[] obj = new double[2];
|
||||
assertEquals((17 * 37) * 37 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = 5.4d;
|
||||
long l1 = Double.doubleToLongBits(5.4d);
|
||||
int h1 = (int) (l1 ^ (l1 >> 32));
|
||||
assertEquals((17 * 37 + h1) * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = 6.3d;
|
||||
long l2 = Double.doubleToLongBits(6.3d);
|
||||
int h2 = (int) (l2 ^ (l2 >> 32));
|
||||
assertEquals( (17 * 37 + h1) * 37 + h2, HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testFloatArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (float[]) null));
|
||||
float[] obj = new float[2];
|
||||
assertEquals((17 * 37) * 37 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = 5.4f;
|
||||
int h1 = Float.floatToIntBits(5.4f);
|
||||
assertEquals((17 * 37 + h1) * 37, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = 6.3f;
|
||||
int h2 = Float.floatToIntBits(6.3f);
|
||||
assertEquals( (17 * 37 + h1) * 37 + h2, HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
public void testBooleanArray() {
|
||||
assertEquals(17 * 37, HashCodeUtils.buildHashCode(17, (boolean[]) null));
|
||||
boolean[] obj = new boolean[2];
|
||||
assertEquals((17 * 37 + 1) * 37 + 1 , HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[0] = true;
|
||||
assertEquals((17 * 37 + 0) * 37 + 1, HashCodeUtils.buildHashCode(17, obj));
|
||||
obj[1] = false;
|
||||
assertEquals( (17 * 37 + 0) * 37 + 1, HashCodeUtils.buildHashCode(17, obj));
|
||||
}
|
||||
|
||||
}
|
|
@ -62,7 +62,7 @@ import junit.textui.TestRunner;
|
|||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @version $Id: LangTestSuite.java,v 1.2 2002/08/10 12:13:28 scolebourne Exp $
|
||||
* @version $Id: LangTestSuite.java,v 1.3 2002/08/15 22:37:29 scolebourne Exp $
|
||||
*/
|
||||
public class LangTestSuite extends TestCase {
|
||||
|
||||
|
@ -87,7 +87,7 @@ public class LangTestSuite extends TestCase {
|
|||
TestSuite suite = new TestSuite();
|
||||
suite.setName("Commons-Lang Tests");
|
||||
suite.addTest(CharSetUtilsTest.suite());
|
||||
suite.addTest(HashCodeUtilsTest.suite());
|
||||
suite.addTest(HashCodeBuilderTest.suite());
|
||||
suite.addTest(NumberRangeTest.suite());
|
||||
suite.addTest(NumberUtilsTest.suite());
|
||||
suite.addTest(ObjectUtilsTest.suite());
|
||||
|
|
Loading…
Reference in New Issue