harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r400395 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Arrays.java test/java/org/apache/harmony/tests/java/util/ArraysTest.java test/java/tests/api/java/util/ArraysTest.java
Date Sun, 07 May 2006 03:15:22 GMT
Author: tellison
Date: Sat May  6 20:15:20 2006
New Revision: 400395

URL: http://svn.apache.org/viewcvs?rev=400395&view=rev
Log:
Apply patch HARMONY-446 ([classlib][luni] java.util.Arrays sort and binarySearch generification
and bug fixes)

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/org/apache/harmony/tests/java/util/ArraysTest.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/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java?rev=400395&r1=400394&r2=400395&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
Sat May  6 20:15:20 2006
@@ -26,14 +26,14 @@
  */
 public class Arrays {
 
-	private static class ArrayList extends AbstractList implements List,
+	private static class ArrayList<E> extends AbstractList<E> implements List<E>,
 			Serializable, RandomAccess {
 
 		private static final long serialVersionUID = -2764017481108945198L;
 
-		private final Object[] a;
+		private final E[] a;
 
-		ArrayList(Object[] storage) {
+		ArrayList(E[] storage) {
 			if (storage == null) {
 				throw new NullPointerException();
 			}
@@ -53,7 +53,7 @@
 			return false;
 		}
 
-		public Object get(int location) {
+		public E get(int location) {
 			try {
 				return a[location];
 			} catch (ArrayIndexOutOfBoundsException e) {
@@ -87,9 +87,9 @@
 			return -1;
 		}
 
-		public Object set(int location, Object object) {
+		public E set(int location, E object) {
 			try {
-				Object result = a[location];
+				E result = a[location];
 				a[location] = object;
 				return result;
 			} catch (ArrayIndexOutOfBoundsException e) {
@@ -107,10 +107,10 @@
 			return (Object[]) a.clone();
 		}
 
-		public Object[] toArray(Object[] contents) {
+		public <T> T[] toArray(T[] contents) {
 			int size = size();
 			if (size > contents.length)
-				contents = (Object[]) Array.newInstance(contents.getClass()
+				contents = (T[]) Array.newInstance(contents.getClass()
 						.getComponentType(), size);
 			System.arraycopy(a, 0, contents, 0, size);
 			if (size < contents.length)
@@ -133,8 +133,8 @@
 	 *            the array
 	 * @return a List on the specified array
 	 */
-	public static List asList(Object[] array) {
-		return new ArrayList(array);
+	public static <T> List<T> asList(T... array) {
+		return new ArrayList<T>(array);
 	}
 
 	/**
@@ -349,8 +349,12 @@
 	 *                when an element in the array and the search element cannot
 	 *                be compared to each other using the Comparator
 	 */
-	public static int binarySearch(Object[] array, Object object,
-			Comparator comparator) {
+	public static <T> int binarySearch(T[] array, T object,
+			Comparator<? super T> comparator) {
+        if (comparator == null) {
+            return binarySearch(array, object);
+        }
+        
 		int low = 0, mid = 0, high = array.length - 1, result = 0;
 		while (low <= high) {
 			mid = (low + high) >> 1;
@@ -1843,8 +1847,8 @@
 	 *                when <code>start < 0</code> or
 	 *                <code>end > array.size()</code>
 	 */
-	public static void sort(Object[] array, int start, int end,
-			Comparator comparator) {
+	public static <T> void sort(T[] array, int start, int end,
+			Comparator<T> comparator) {
 		if (array == null) {
 			throw new NullPointerException();
 		}
@@ -1857,8 +1861,13 @@
 			throw new ArrayIndexOutOfBoundsException();
 	}
 
-	private static void sort(int start, int end, Object[] array,
-			Comparator comparator) {
+	private static <T> void sort(int start, int end, T[] array,
+			Comparator<T> comparator) {
+        if (comparator == null) {
+            sort(start, end, array);
+            return;
+        }
+        
 		int middle = (start + end) / 2;
 		if (start + 1 < middle)
 			sort(start, middle, array, comparator);
@@ -1870,7 +1879,7 @@
 		if (comparator.compare(array[middle - 1], array[middle]) <= 0)
 			return;
 		if (start + 2 == end) {
-			Object temp = array[start];
+			T temp = array[start];
 			array[start] = array[middle];
 			array[middle] = temp;
 			return;
@@ -1898,8 +1907,8 @@
 	 *                when elements in the array cannot be compared to each
 	 *                other using the Comparator
 	 */
-	public static void sort(Object[] array, Comparator comparator) {
-		sort(0, array.length, array, comparator);
+	public static <T> void sort(T[] array, Comparator<? super T> comparator) {
+            sort(0, array.length, array, comparator);
 	}
 
 	/**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java?rev=400395&r1=400394&r2=400395&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java
Sat May  6 20:15:20 2006
@@ -16,7 +16,13 @@
 
 package org.apache.harmony.tests.java.util;
 
+import java.io.Serializable;
 import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.RandomAccess;
+
+import tests.util.SerializationTester;
 
 import junit.framework.TestCase;
 
@@ -323,5 +329,133 @@
         String expected = "[[true, false], [0, 1], [a, b], [0.0, 1.0], [0.0, 1.0], [0, 1],
[0, 1], [0, 1], [true, false], [[...], [0, 1], [a, b], [0.0, 1.0], [0.0, 1.0], [0, 1], [0,
1], [0, 1], [innerFixture0, [...], [...], innerFixture3]]]";
         
         assertEquals(expected, Arrays.deepToString(fixture));
+    }
+    
+    public void test_asListTvararg() throws Exception {
+        List<String> stringsList = Arrays.asList("0", "1");
+        assertEquals(2, stringsList.size());
+        assertEquals("0", stringsList.get(0));
+        assertEquals("1", stringsList.get(1));
+        assertTrue(stringsList instanceof RandomAccess);
+        assertTrue(stringsList instanceof Serializable);
+        SerializationTester.assertEquals(stringsList);
+        
+        //test from javadoc
+        List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
+        assertEquals(3, stooges.size());
+        assertEquals("Larry", stooges.get(0));
+        assertEquals("Moe", stooges.get(1));
+        assertEquals("Curly", stooges.get(2));
+        
+        stringsList = Arrays.asList((String)null);
+        assertEquals(1, stringsList.size());
+        assertEquals((String)null, stringsList.get(0));
+        
+        try {
+            Arrays.asList((Object[])null);
+            fail("No NPE");
+        } catch (NullPointerException e) {}
+    }
+    
+    public void test_binarySearch$TTLjava_util_ComparatorsuperT() {
+        String[] strings = new String[] { "a", "B", "c", "D" };
+        Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER);
+        assertEquals(0, Arrays.binarySearch(strings, "a",
+                String.CASE_INSENSITIVE_ORDER));
+        assertEquals(0, Arrays.binarySearch(strings, "A",
+                String.CASE_INSENSITIVE_ORDER));
+        assertEquals(1, Arrays.binarySearch(strings, "b",
+                String.CASE_INSENSITIVE_ORDER));
+        assertEquals(1, Arrays.binarySearch(strings, "B",
+                String.CASE_INSENSITIVE_ORDER));
+        assertEquals(2, Arrays.binarySearch(strings, "c",
+                String.CASE_INSENSITIVE_ORDER));
+        assertEquals(2, Arrays.binarySearch(strings, "C",
+                String.CASE_INSENSITIVE_ORDER));
+        assertEquals(3, Arrays.binarySearch(strings, "d",
+                String.CASE_INSENSITIVE_ORDER));
+        assertEquals(3, Arrays.binarySearch(strings, "D",
+                String.CASE_INSENSITIVE_ORDER));
+
+
+        assertTrue(Arrays.binarySearch(strings, "e",
+                String.CASE_INSENSITIVE_ORDER) < 0);
+        assertTrue(Arrays.binarySearch(strings, "" + ('A' - 1),
+                String.CASE_INSENSITIVE_ORDER) < 0);
+
+        //test with null comparator, which switches back to Comparable
+        Arrays.sort(strings, null);
+        //B, D, a, c
+        assertEquals(2, Arrays.binarySearch(strings, "a", (Comparator<String>)null));
+        assertEquals(-1, Arrays.binarySearch(strings, "A", (Comparator<String>)null));
+        assertEquals(-4, Arrays.binarySearch(strings, "b", (Comparator<String>)null));
+        assertEquals(0, Arrays.binarySearch(strings, "B", (Comparator<String>)null));
+        assertEquals(3, Arrays.binarySearch(strings, "c", (Comparator<String>)null));
+        assertEquals(-2, Arrays.binarySearch(strings, "C", (Comparator<String>)null));
+        assertEquals(-5, Arrays.binarySearch(strings, "d", (Comparator<String>)null));
+        assertEquals(1, Arrays.binarySearch(strings, "D", (Comparator<String>)null));
      
+
+        assertTrue(Arrays.binarySearch(strings, "e", null) < 0);
+        assertTrue(Arrays.binarySearch(strings, "" + ('A' - 1), null) < 0);
+        
+        try {
+            Arrays.binarySearch((String[])null, "A", String.CASE_INSENSITIVE_ORDER);
+            fail("No NPE");
+        } catch (NullPointerException e) {}
+        
+        try {
+            Arrays.binarySearch(strings, (String)null, String.CASE_INSENSITIVE_ORDER);
+            fail("No NPE");
+        } catch (NullPointerException e) {}
+        
+        try {
+            Arrays.binarySearch(strings, (String)null, (Comparator<String>)null);
+            fail("No NPE");
+        } catch (NullPointerException e) {}
+        
+    }
+    
+    public void test_sort$TLjava_lang_ComparatorsuperT() {
+        String[] strings = new String[] { "a", "B", "c", "D" };
+        Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER);
+        assertEquals("a", strings[0]);
+        assertEquals("B", strings[1]);
+        assertEquals("c", strings[2]);
+        assertEquals("D", strings[3]);
+        
+        //test with null comparator, which switches back to Comparable
+        Arrays.sort(strings, null);
+        //B, D, a, c
+        assertEquals("B", strings[0]);
+        assertEquals("D", strings[1]);
+        assertEquals("a", strings[2]);
+        assertEquals("c", strings[3]);
+        
+        try {
+            Arrays.sort((String[])null, String.CASE_INSENSITIVE_ORDER);
+            fail("No NPE");
+        } catch (NullPointerException e) {}
+    }
+    
+    public void test_sort$TIILjava_lang_ComparatorsuperT() {
+        String[] strings = new String[] { "a", "B", "c", "D" };
+        Arrays.sort(strings, 0, strings.length, String.CASE_INSENSITIVE_ORDER);
+        assertEquals("a", strings[0]);
+        assertEquals("B", strings[1]);
+        assertEquals("c", strings[2]);
+        assertEquals("D", strings[3]);
+        
+        //test with null comparator, which switches back to Comparable
+        Arrays.sort(strings, 0, strings.length, null);
+        //B, D, a, c
+        assertEquals("B", strings[0]);
+        assertEquals("D", strings[1]);
+        assertEquals("a", strings[2]);
+        assertEquals("c", strings[3]);
+        
+        try {
+            Arrays.sort((String[])null, String.CASE_INSENSITIVE_ORDER);
+            fail("No NPE");
+        } catch (NullPointerException e) {}
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ArraysTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ArraysTest.java?rev=400395&r1=400394&r2=400395&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
Sat May  6 20:15:20 2006
@@ -88,11 +88,11 @@
 		}
 
 		try {
-			Arrays.asList(null);
+			Arrays.asList((Object[])null);
+			fail("asList with null arg didn't throw NPE");
 		} catch (NullPointerException e) {
-			return;
+			// Expected
 		}
-		fail("asList with null arg didn't throw NPE");
 	}
 
 	/**



Mime
View raw message