harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r410341 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Arrays.java test/java/tests/api/java/util/ArraysTest.java
Date Tue, 30 May 2006 18:37:37 GMT
Author: tellison
Date: Tue May 30 11:37:37 2006
New Revision: 410341

URL: http://svn.apache.org/viewvc?rev=410341&view=rev
Log:
Apply patch HARMONY-533 (Java 5 Enhancement: util.Arrays should implement deepHashCode, deepEquals,
hashCode(*) methods)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ArraysTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java?rev=410341&r1=410340&r2=410341&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java
Tue May 30 11:37:37 2006
@@ -773,6 +773,229 @@
 			array[i] = value;
 	}
 
+    public static int hashCode(boolean[] array) {
+        if (array == null) {
+            return 0;
+        }
+        
+        int hashCode = 1;
+                
+        for (int i = 0; i < array.length; i++) {
+            
+            //1231, 1237 are hash code values for boolean value
+            hashCode = 31 * hashCode + (array[i]? 1231: 1237);
+        }
+        
+        return hashCode;
+    }
+    
+    public static int hashCode(int[] array) {
+        if (array == null) {
+            return 0;
+        }
+                
+        int hashCode = 1;
+        
+        for (int i = 0; i < array.length; i++) {
+            
+            //the hash code value for integer value is integer value itself
+            hashCode = 31 * hashCode + array[i];
+        }
+        
+        return hashCode;
+    }
+        
+    public static int hashCode(short[] array) {
+        if (array == null) {
+            return 0;
+        }
+        
+        int hashCode = 1;
+        
+        for (int i = 0; i < array.length; i++) {
+            
+            //the hash code value for short value is its integer value
+            hashCode = 31 * hashCode + array[i];
+        }
+        
+        return hashCode;
+    }
+    
+    public static int hashCode(char[] array) {
+        if (array == null) {
+            return 0;
+        }
+        
+        int hashCode = 1;
+        
+        for (int i = 0; i < array.length; i++) {
+            
+            //the hash code value for char value is its integer value
+            hashCode = 31 * hashCode + array[i];
+        }
+        
+        return hashCode;
+    }
+    
+    public static int hashCode(byte[] array) {
+        if (array == null) {
+            return 0;
+        }
+        
+        int hashCode = 1;
+        
+        for (int i = 0; i < array.length; i++) {
+            
+            //the hash code value for byte value is its integer value
+            hashCode = 31 * hashCode + array[i];
+        }
+        
+        return hashCode;
+    }
+
+    public static int hashCode(long[] array) {
+        if (array == null) {
+            return 0;
+        }
+        
+        int hashCode = 1;
+        
+        for (int i = 0; i < array.length; i++) {
+            long elementValue = array[i];
+            
+            /*
+             * the hash code value for long value is 
+             * (int) (value ^ (value >>> 32))
+             */
+            hashCode = 31 * hashCode + 
+                    (int) (elementValue ^ (elementValue >>> 32));
+        }
+        
+        return hashCode;
+    }
+    
+    public static int hashCode(float[] array) {
+        if (array == null) {
+            return 0;
+        }
+        
+        int hashCode = 1;
+        
+        for (int i = 0; i < array.length; i++) {
+
+            /*
+             * the hash code value for float value is 
+             * Float.floatToIntBits(value)
+             */
+            hashCode = 31 * hashCode + Float.floatToIntBits(array[i]);                  
 
+        }
+        
+        return hashCode;
+    }
+
+    public static int hashCode(double[] array) {
+        if (array == null) {
+            return 0;
+        }
+        
+        int hashCode = 1;
+        
+        for (int i = 0; i < array.length; i++) {
+            long v = Double.doubleToLongBits(array[i]);
+
+            /*
+             * the hash code value for double value is 
+             * (int) (v ^ (v >>> 32)) where 
+             * v = Double.doubleToLongBits(value)
+             */
+            hashCode = 31 * hashCode + (int) (v ^ (v >>> 32));                 
  
+        }
+        
+        return hashCode;
+    }
+    
+    public static int hashCode(Object[] array) {
+        if (array == null) {
+            return 0;
+        }
+        
+        int hashCode = 1;
+        
+        for (int i = 0; i < array.length; i++) {
+            int elementHashCode;
+            
+            if (array[i] == null) {
+                elementHashCode = 0;
+            } else {
+                elementHashCode = (array[i]).hashCode();
+            }
+            
+            hashCode = 31 * hashCode + elementHashCode;                    
+        }
+        
+        return hashCode;
+    }
+    
+    public static int deepHashCode(Object[] array) {
+        if (array == null) {
+            return 0;
+        }
+
+        int hashCode = 1;
+                
+        for (int i = 0; i < array.length; i++) {
+            int elementHashCode = deepHashCodeElement(array[i]);
+                        
+            hashCode = 31 * hashCode + elementHashCode;
+        }
+        
+        return hashCode;
+    }
+    
+    private static int deepHashCodeElement(Object element) {
+        Class cl; 
+        
+        if (element == null) {
+            return 0;
+        } 
+        
+        cl = element.getClass().getComponentType();
+        
+        if (cl == null) {
+            return element.hashCode();
+        }
+                
+        /*
+         * element is an array
+         */
+        if (!cl.isPrimitive()) {
+            return deepHashCode((Object[]) element);
+        }
+                     
+        if (cl.equals(int.class)) {
+            return hashCode((int []) element);
+        }
+        if (cl.equals(char.class)) {
+            return hashCode((char []) element);
+        }
+        if (cl.equals(boolean.class)) {
+            return hashCode((boolean []) element);
+        }
+        if (cl.equals(byte.class)) {
+            return hashCode((byte []) element);
+        }
+        if (cl.equals(long.class)) {
+            return hashCode((long []) element);
+        }
+        if (cl.equals(float.class)) {
+            return hashCode((float []) element);
+        }
+        if (cl.equals(double.class)) {
+            return hashCode((double []) element);
+        }
+        return hashCode((short []) element);
+    }
+    
 	/**
 	 * Compares the two arrays.
 	 * 
@@ -982,6 +1205,76 @@
 		return true;
 	}
 
+    public static boolean deepEquals(Object[] array1, Object[] array2) {
+        if (array1 == array2) {
+            return true;
+        }
+        if (array1 == null || array2 == null || array1.length != array2.length) {
+            return false;
+        }
+        for (int i = 0; i < array1.length; i++) {
+            Object e1 = array1[i], e2 = array2[i];
+            
+            if (!deepEqualsElements(e1, e2)) {
+                return false;
+            }
+        }
+        return true;
+    }
+    
+    private static boolean deepEqualsElements(Object e1, Object e2) {
+        Class cl1, cl2;
+        
+        if (e1 == e2) {
+            return true;
+        }
+        
+        if (e1 == null || e2 == null) {
+            return false;
+        }
+                
+        cl1 = e1.getClass().getComponentType();
+        cl2 = e2.getClass().getComponentType();
+        
+        if (cl1 != cl2) {
+            return false;
+        }
+            
+        if (cl1 == null) {
+            return e1.equals(e2);
+        }
+                
+        /*
+         * compare as arrays
+         */
+        if (!cl1.isPrimitive()) {
+            return deepEquals((Object[]) e1, (Object[]) e2);
+        }
+        
+        if (cl1.equals(int.class)) {
+            return equals((int []) e1, (int []) e2);
+        }
+        if (cl1.equals(char.class)) {
+            return equals((char []) e1, (char []) e2);
+        }
+        if (cl1.equals(boolean.class)) {
+            return equals((boolean []) e1, (boolean []) e2);
+        }
+        if (cl1.equals(byte.class)) {
+            return equals((byte []) e1, (byte []) e2);
+        }
+        if (cl1.equals(long.class)) {
+            return equals((long []) e1, (long []) e2);
+        }
+        if (cl1.equals(float.class)) {
+            return equals((float []) e1, (float []) e2);
+        }
+        if (cl1.equals(double.class)) {
+            return equals((double []) e1, (double []) e2);
+        }   
+        return equals((short []) e1, (short []) e2);                 
+    }
+    
 	private static boolean lessThan(double double1, double double2) {
 		long d1, d2;
 		long NaNbits = Double.doubleToLongBits(Double.NaN);
@@ -1848,7 +2141,7 @@
 	 *                <code>end > array.size()</code>
 	 */
 	public static <T> void sort(T[] array, int start, int end,
-			Comparator<T> comparator) {
+            Comparator<? super T> comparator) {
 		if (array == null) {
 			throw new NullPointerException();
 		}
@@ -1862,7 +2155,7 @@
 	}
 
 	private static <T> void sort(int start, int end, T[] array,
-			Comparator<T> comparator) {
+            Comparator<? super T> comparator) {
         if (comparator == null) {
             sort(start, end, array);
             return;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ArraysTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ArraysTest.java?rev=410341&r1=410340&r2=410341&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ArraysTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ArraysTest.java
Tue May 30 11:37:37 2006
@@ -17,6 +17,7 @@
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.List;
+import java.util.LinkedList;
 
 import tests.support.Support_UnmodifiableCollectionTest;
 
@@ -1307,7 +1308,213 @@
             // Expected
         }
     }
-
+    
+    /**
+     * @tests java.util.Arrays#deepEquals(Object[], Object[])      
+     */
+    public void test_deepEquals$Ljava_lang_ObjectLjava_lang_Object() {
+       int [] a1 = {1, 2, 3};
+       short [] a2 = {0, 1};
+       Object [] a3 = {new Integer(1), a2};
+       int [] a4 = {6, 5, 4};
+       
+       int [] b1 = {1, 2, 3};
+       short [] b2 = {0, 1};
+       Object [] b3 = {new Integer(1), b2};
+       
+       Object a [] = {a1, a2, a3};
+       Object b [] = {b1, b2, b3};
+       
+       assertFalse(Arrays.equals(a, b));
+       assertTrue(Arrays.deepEquals(a,b));
+       
+       a[2] = a4;
+       
+       assertFalse(Arrays.deepEquals(a, b));
+    }
+    
+    /**
+     * @tests java.util.Arrays#deepHashCode(Object[])
+     */
+    public void test_deepHashCode$Ljava_lang_Object() {
+        int [] a1 = {1, 2, 3};
+        short [] a2 = {0, 1};
+        Object [] a3 = {new Integer(1), a2};
+        
+        int [] b1 = {1, 2, 3};
+        short [] b2 = {0, 1};
+        Object [] b3 = {new Integer(1), b2};
+        
+        Object a [] = {a1, a2, a3};
+        Object b [] = {b1, b2, b3};
+       
+        int deep_hash_a = Arrays.deepHashCode(a);
+        int deep_hash_b = Arrays.deepHashCode(b);
+        
+        assertEquals(deep_hash_a, deep_hash_b);
+     }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(boolean[] a)
+     */
+    public void test_hashCode$LZ() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        boolean [] boolArr = {true, false, false, true, false};    
+        List listOfBoolean = new LinkedList();
+        for (int i = 0; i < boolArr.length; i++) {
+            listOfBoolean.add(new Boolean(boolArr[i]));
+        }
+        listHashCode = listOfBoolean.hashCode();
+        arrayHashCode = Arrays.hashCode(boolArr);
+        assertEquals(listHashCode, arrayHashCode);
+    }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(int[] a)
+     */
+    public void test_hashCode$LI() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        int [] intArr = {10, 5, 134, 7, 19};    
+        List listOfInteger = new LinkedList();
+         
+        for (int i = 0; i < intArr.length; i++) {
+            listOfInteger.add(new Integer(intArr[i]));           
+        }               
+        listHashCode = listOfInteger.hashCode();
+        arrayHashCode = Arrays.hashCode(intArr);       
+        assertEquals(listHashCode, arrayHashCode);
+        
+        int [] intArr2 = {10, 5, 134, 7, 19};                
+        assertEquals(Arrays.hashCode(intArr2), Arrays.hashCode(intArr));
+    }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(char[] a)
+     */
+    public void test_hashCode$LC() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        char [] charArr = {'a', 'g', 'x', 'c', 'm'};    
+        List listOfCharacter = new LinkedList();
+        for (int i = 0; i < charArr.length; i++) {
+            listOfCharacter.add(new Character(charArr[i]));
+        }
+        listHashCode = listOfCharacter.hashCode();
+        arrayHashCode = Arrays.hashCode(charArr);
+        assertEquals(listHashCode, arrayHashCode);
+    }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(byte[] a)
+     */
+    public void test_hashCode$LB() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        byte [] byteArr = {5, 9, 7, 6, 17};    
+        List listOfByte = new LinkedList();
+        for (int i = 0; i < byteArr.length; i++) {
+            listOfByte.add(new Byte(byteArr[i]));
+        }
+        listHashCode = listOfByte.hashCode();
+        arrayHashCode = Arrays.hashCode(byteArr);
+        assertEquals(listHashCode, arrayHashCode);
+    }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(long[] a)
+     */
+    public void test_hashCode$LJ() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        long [] longArr = {67890234512l, 97587236923425l, 257421912912l,
+                6754268100l, 5};    
+        List listOfLong = new LinkedList();
+        for (int i = 0; i < longArr.length; i++) {
+            listOfLong.add(new Long(longArr[i]));
+        }
+        listHashCode = listOfLong.hashCode();
+        arrayHashCode = Arrays.hashCode(longArr);
+        assertEquals(listHashCode, arrayHashCode);
+    }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(float[] a)
+     */
+    public void test_hashCode$LF() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        float [] floatArr = {0.13497f, 0.268934f, 12e-5f, -3e+2f, 10e-4f};    
+        List listOfFloat = new LinkedList();
+        for (int i = 0; i < floatArr.length; i++) {
+            listOfFloat.add(new Float(floatArr[i]));
+        }
+        listHashCode = listOfFloat.hashCode();
+        arrayHashCode = Arrays.hashCode(floatArr);
+        assertEquals(listHashCode, arrayHashCode);
+           
+        float [] floatArr2 = {0.13497f, 0.268934f, 12e-5f, -3e+2f, 10e-4f};
+        assertEquals(Arrays.hashCode(floatArr2), Arrays.hashCode(floatArr));
+    }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(double[] a)
+     */
+    public void test_hashCode$LD() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        double [] doubleArr = {0.134945657, 0.0038754, 11e-150, -30e-300, 10e-4};    
+        List listOfDouble = new LinkedList();
+        for (int i = 0; i < doubleArr.length; i++) {
+            listOfDouble.add(new Double(doubleArr[i]));
+        }
+        listHashCode = listOfDouble.hashCode();
+        arrayHashCode = Arrays.hashCode(doubleArr);
+        assertEquals(listHashCode, arrayHashCode);
+    }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(short[] a)
+     */
+    public void test_hashCode$LS() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        short [] shortArr = {35, 13, 45, 2, 91};    
+        List listOfShort = new LinkedList();
+        for (int i = 0; i < shortArr.length; i++) {
+            listOfShort.add(new Short(shortArr[i]));
+        }
+        listHashCode = listOfShort.hashCode();
+        arrayHashCode = Arrays.hashCode(shortArr);
+        assertEquals(listHashCode, arrayHashCode);
+    }
+    
+    /**
+     * @tests java.util.Arrays#hashCode(Object[] a)
+     */
+    public void test_hashCode$Ljava_lang_Object() {
+        int listHashCode;
+        int arrayHashCode;
+        
+        Object[] objectArr = {new Integer(1), new Float(10e-12f), null};
+        List listOfObject= new LinkedList();
+        for (int i = 0; i < objectArr.length; i++) {
+            listOfObject.add(objectArr[i]);
+        }
+        listHashCode = listOfObject.hashCode();
+        arrayHashCode = Arrays.hashCode(objectArr);
+        assertEquals(listHashCode, arrayHashCode);
+    }
+    
 	/**
 	 * Sets up the fixture, for example, open a network connection. This method
 	 * is called before a test is executed.



Mime
View raw message