You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/06/20 13:33:48 UTC

svn commit: r549073 - in /harmony/enhanced/classlib/branches/java6/modules/luni/src: main/java/java/util/PriorityQueue.java test/api/common/tests/api/java/util/PriorityQueueTest.java

Author: tellison
Date: Wed Jun 20 04:33:47 2007
New Revision: 549073

URL: http://svn.apache.org/viewvc?view=rev&rev=549073
Log:
Apply patch HARMONY-4203 ([classlib][luni][java6] New feature of java.util.PriorityQueue for java6)

Modified:
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/PriorityQueue.java
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PriorityQueueTest.java

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/PriorityQueue.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/PriorityQueue.java?view=diff&rev=549073&r1=549072&r2=549073
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/PriorityQueue.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/PriorityQueue.java Wed Jun 20 04:33:47 2007
@@ -19,6 +19,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
+import java.lang.reflect.Array;
 
 /**
  * PriorityQueue holds elements on a priority heap, which orders elements
@@ -230,17 +231,13 @@
         if (o == null) {
             return false;
         }
-        int targetIndex;
-        for (targetIndex = 0; targetIndex < size; targetIndex++) {
-            if (0 == this.compare((E) o, elements[targetIndex])) {
-                break;
+        for (int i = 0; i < size; i++) {
+            if (elements[i].equals(o)) {
+                removeAt(i);
+                return true;
             }
         }
-        if (size == 0 || size == targetIndex) {
-            return false;
-        }
-        removeAt(targetIndex);
-        return true;
+        return false;
     }
 
     /**
@@ -256,6 +253,73 @@
     @Override
     public boolean add(E o) {
         return offer(o);
+    }
+    
+    /**
+     * Answers if there is an element in this queue equals to the object.
+     * 
+     * @see java.util.AbstractCollection#contains(java.lang.Object)
+     */
+    @Override
+    public boolean contains(Object object) {
+        for (int i = 0; i < size; i++) {
+            if(elements[i].equals(object)){
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns all the elements in an array. The result is a copy of all the
+     * elements.
+     * 
+     * @return the Array of all the elements
+     * @see java.util.AbstractCollection#toArray()
+     */
+    @Override
+    public Object[] toArray() {
+        return newArray(new Object[size()]);
+    }
+
+    /**
+     * Returns all the elements in an array, and the type of the result array is
+     * the type of the argument array. If the argument array is big enough, the
+     * elements from the queue will be stored in it(element immediately
+     * following the end of the queue is set to null, if any); otherwise, it
+     * will return a new array with the size of the argument array and size of
+     * the queue.
+     * 
+     * @param <T>
+     *            the type of elements in the array
+     * @param array
+     *            the array stores all the elements from the queue, if it has
+     *            enough space; otherwise, a new array of the same type and the
+     *            size of the queue will be used
+     * @return the Array of all the elements
+     * @throws ArrayStoreException
+     *             if the type of the argument array is not compatible with
+     *             every element in the queue
+     * @throws NullPointerException
+     *             if the argument array is null
+     * @see java.util.AbstractCollection#toArray(T[])
+     */
+    @Override
+    public <T> T[] toArray(T[] array) {
+        return newArray(array);
+    }
+
+    @SuppressWarnings("unchecked")
+    private <T> T[] newArray(T[] array) {
+        if (size > array.length) {
+            Class<?> clazz = array.getClass().getComponentType();
+            array = (T[]) Array.newInstance(clazz, size);
+        }
+        System.arraycopy(elements, 0, array, 0, size);
+        if (size < array.length) {
+            array[size] = null;
+        }
+        return array;
     }
 
     private class PriorityIterator implements Iterator<E> {

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PriorityQueueTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PriorityQueueTest.java?view=diff&rev=549073&r1=549072&r2=549073
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PriorityQueueTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PriorityQueueTest.java Wed Jun 20 04:33:47 2007
@@ -27,9 +27,8 @@
 import java.util.SortedSet;
 import java.util.TreeSet;
 
-import tests.util.SerializationTester;
-
 import junit.framework.TestCase;
+import tests.util.SerializationTester;
 
 public class PriorityQueueTest extends TestCase {
 
@@ -594,7 +593,7 @@
             queue.offer(array[i]);
         }
         assertFalse(queue.contains("BB"));
-        assertTrue(queue.remove("BB"));
+        assertFalse(queue.remove("BB"));
     }
 
     /**
@@ -607,12 +606,7 @@
         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
         assertFalse(integerQueue.remove(111));
         assertFalse(integerQueue.remove(null));
-        try {
-            integerQueue.remove("");
-            fail("should throw ClassCastException");
-        } catch (ClassCastException e) {
-            // expected
-        }
+        assertFalse(integerQueue.remove(""));
     }
 
     /**
@@ -634,12 +628,7 @@
         Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
         List<Integer> list = Arrays.asList(array);
         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
-        try {
-            integerQueue.remove(new Float(1.3F));
-            fail("should throw ClassCastException");
-        } catch (ClassCastException e) {
-            // expected
-        }
+        assertFalse(integerQueue.remove(new Float(1.3F)));
 
         // although argument element type is not compatible with those in queue,
         // but comparator supports it.
@@ -652,12 +641,7 @@
         PriorityQueue<Object> queue = new PriorityQueue<Object>();
         Object o = new Object();
         queue.offer(o);
-        try {
-            queue.remove(o);
-            fail("should throw ClassCastException");
-        } catch (ClassCastException e) {
-            // expected
-        }
+        assertTrue(queue.remove(o));
     }
 
     /**
@@ -671,7 +655,7 @@
         queue = new PriorityQueue<Object>(100, comparator);
         assertEquals(comparator, queue.comparator());
     }
-
+    
     /**
      * @tests serialization/deserialization.
      */
@@ -722,6 +706,73 @@
         Arrays.sort(array);
         Integer I = (Integer) o;
         assertEquals(array[0], I);
+    }
+    
+    /**
+     * @tests {@link PriorityQueue#contains(Object)}
+     */
+    public void test_contains() throws Exception {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.add(array[i]);
+        }
+        for (int i = 0; i < array.length; i++) {
+            assertTrue(integerQueue.contains(array[i]));
+        }
+        assertFalse(integerQueue.contains(null));
+    }
+    
+    /**
+     * @tests {@link PriorityQueue#toArray()}
+     */
+    public void test_toArray() throws Exception {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.add(array[i]);
+        }
+        Object[] returnArray = integerQueue.toArray();
+        assertEquals(returnArray.length,integerQueue.size());
+        for (int i = 0; i < returnArray.length; i++) {
+            assertTrue(integerQueue.contains(returnArray[i]));
+        }
+    }
+    
+    /**
+     * @tests {@link PriorityQueue#toArray(T[])}
+     */
+    public void test_toArray_$T() throws Exception {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.add(array[i]);
+        }
+        Object[] returnArray = integerQueue.toArray(new Integer[0]);
+        assertEquals(returnArray.length,integerQueue.size());
+        for (int i = 0; i < returnArray.length; i++) {
+            assertTrue(integerQueue.contains(returnArray[i]));
+        }
+        returnArray = integerQueue.toArray(new Integer[10]);
+        assertEquals(10,returnArray.length);
+        for (int i = 0; i < array.length; i++) {
+            assertTrue(integerQueue.contains(returnArray[i]));
+        }
+        for (int i = array.length; i < 10; i++) {
+            assertNull(returnArray[i]);
+        }
+        try {
+            integerQueue.toArray(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            integerQueue.toArray(new String[1]);
+            fail("should throw ArrayStoreException");
+        } catch (ArrayStoreException e) {
+            // expected
+        }
     }
 
     private static class MockComparator<E> implements Comparator<E> {