Index: src/test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java
===================================================================
--- src/test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java (revision 501392)
+++ src/test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java (working copy)
@@ -77,22 +77,5 @@
if (!aNumber1.equals(aNumber2)) {
assertTrue("hash codes for unequal objects are equal", code1 != code2);
}
- }
-
- /**
- * Test hash codes for unequal objects.
- * The codes are equal.
- */
- public void testUnequalObjectsEqual() {
- byte aBytes[] = {56, 100, -2, -76, 98, 54, 19, 3, -15, 45, 89, -111, 69, 103, 8, -9};
- byte bBytes[] = {56, 100, -2, -76, 89, 45, 91, 3, -15, 45, 89, -111, 69, 103, 8, -9};
- int aSign = 1;
- BigInteger aNumber = new BigInteger(aSign, aBytes);
- BigInteger bNumber = new BigInteger(aSign, bBytes);
- int code1 = aNumber.hashCode();
- int code2 = bNumber.hashCode();
- if (!aNumber.equals(bNumber)) {
- assertTrue("hash codes for these unequal objects should be equal", code1 == code2);
- }
- }
+ }
}
Index: src/main/java/java/math/BigInteger.java
===================================================================
--- src/main/java/java/math/BigInteger.java (revision 501392)
+++ src/main/java/java/math/BigInteger.java (working copy)
@@ -85,6 +85,8 @@
/** @ar.org.fitc.spec_ref */
private byte[] magnitude;
+
+ private transient int hashCode = 0;
/* Public Constructors */
@@ -541,7 +543,13 @@
/** @ar.org.fitc.spec_ref */
@Override
public int hashCode() {
- return intValue();
+ if (hashCode != 0) {
+ return hashCode;
+ }
+ for (int i = 0; i < digits.length; i ++) {
+ hashCode = (int)(hashCode * 33 + (digits[i] & 0xffffffff));
+ }
+ return hashCode * sign;
}
/** @ar.org.fitc.spec_ref */
Index: src/main/java/java/math/BigDecimal.java
===================================================================
--- src/main/java/java/math/BigDecimal.java (revision 501392)
+++ src/main/java/java/math/BigDecimal.java (working copy)
@@ -74,6 +74,8 @@
/** The String representation is cached. */
private transient String toStringImage = null;
+
+ private transient int hashCode = 0;
/**
* An array with powers of five that fit in the type long
@@ -1368,10 +1370,18 @@
/** @ar.org.fitc.spec_ref */
@Override
- public int hashCode() {
- /* Take the 24 trailing bits of BigInteger hashcode
- * and the 8 trailing bits of scale. */
- return ((getUnscaledValue().hashCode() << 24) | (0xFF & scale));
+ public int hashCode() {
+ if (hashCode != 0) {
+ return hashCode;
+ }
+ if (bitLength < 64) {
+ hashCode = (int)(smallValue & 0xffffffff);
+ hashCode = 33 * hashCode + (int)((smallValue >> 32) & 0xffffffff);
+ hashCode = 17 * hashCode + scale;
+ return hashCode;
+ }
+ hashCode = 17 * intVal.hashCode() + scale;
+ return hashCode;
}
/** @ar.org.fitc.spec_ref */