harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apetre...@apache.org
Subject svn commit: r501765 - in /harmony/enhanced/classlib/trunk/modules/math/src: main/java/java/math/BigDecimal.java main/java/java/math/BigInteger.java test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java
Date Wed, 31 Jan 2007 08:35:48 GMT
Author: apetrenko
Date: Wed Jan 31 00:35:47 2007
New Revision: 501765

URL: http://svn.apache.org/viewvc?view=rev&rev=501765
Log:
Patch for HARMONY-3086 "[math] poor hashCode in java.math.BigDecimal and java.math.BigDecimal"

Modified:
    harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigDecimal.java
    harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java
    harmony/enhanced/classlib/trunk/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java

Modified: harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigDecimal.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigDecimal.java?view=diff&rev=501765&r1=501764&r2=501765
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigDecimal.java (original)
+++ harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigDecimal.java Wed
Jan 31 00:35:47 2007
@@ -74,6 +74,8 @@
 
     /** The <code>String</code> representation is cached. */
     private transient String toStringImage = null;
+    
+    private transient int hashCode = 0;
 
     /**
      * An array with powers of five that fit in the type <code>long</code>
@@ -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 */

Modified: harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java?view=diff&rev=501765&r1=501764&r2=501765
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java (original)
+++ harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/BigInteger.java Wed
Jan 31 00:35:47 2007
@@ -85,6 +85,8 @@
 
     /** @ar.org.fitc.spec_ref */
     private byte[] magnitude;
+    
+    private transient int hashCode = 0;
 
     /* Public Constructors */
 
@@ -541,7 +543,15 @@
     /** @ar.org.fitc.spec_ref */
     @Override
     public int hashCode() {
-        return intValue();
+    	if (hashCode != 0) {
+    	    return hashCode;	
+    	}    	  
+    	System.out.println(digits.length);
+    	for (int i = 0; i < digits.length; i ++) {
+    		hashCode = (int)(hashCode * 33 + (digits[i] & 0xffffffff));    		
+    	}  
+    	hashCode = hashCode * sign;
+        return hashCode;
     }
 
     /** @ar.org.fitc.spec_ref */

Modified: harmony/enhanced/classlib/trunk/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java?view=diff&rev=501765&r1=501764&r2=501765
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerHashCodeTest.java
Wed Jan 31 00:35:47 2007
@@ -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);
-        }
-    }
+    }      
 }



Mime
View raw message