You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/06/08 05:59:22 UTC

svn commit: r412643 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/AbstractQueue.java test/java/tests/api/java/util/AbstractQueueTest.java test/java/tests/api/java/util/AllTests.java

Author: mloenko
Date: Wed Jun  7 20:59:22 2006
New Revision: 412643

URL: http://svn.apache.org/viewvc?rev=412643&view=rev
Log:
improvement from HARMONY-559
[classlib][luni]new classes java.util.AbstractQueue

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractQueue.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AbstractQueueTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractQueue.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractQueue.java?rev=412643&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractQueue.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractQueue.java Wed Jun  7 20:59:22 2006
@@ -0,0 +1,128 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+/**
+ * 
+ * An abstract class which gives out skeletal implementations for some methods
+ * in Queue which include add, remove, and element that are based on offer,
+ * poll, and peek except that they throw exception to indicate the occurence of
+ * some error instead of the return value of false or null.
+ * 
+ * @param <E>
+ *            the type of the element in the collection.
+ */
+public abstract class AbstractQueue<E> extends AbstractCollection<E> implements
+        Queue<E> {
+
+    /**
+     * Constructor for the sub classes.
+     * 
+     */
+    protected AbstractQueue() {
+    }
+
+    /**
+     * Adds an element to the queue.
+     * 
+     * @param o the element added to the queue.
+     * @return true if the operation succeeds.
+     * @throws NullPointerException if the element is null.
+     * @throws IllegalStateException if the element is not allowed to be added
+     *         to the queue.
+     */
+    public boolean add(E o) {
+        if (null == o) {
+            throw new NullPointerException();
+        }
+        if (offer(o)) {
+            return true;
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    /**
+     * Adds all the elements of a collection to the queue. If the collection is
+     * the queue itself, then an IllegalArgumentException will be thrown out. If
+     * during the process, some runtime exception is thrown out, then part of
+     * the elements in the collection that have successfully added will remain
+     * in the queue.
+     * 
+     * The result of the method is undefined if the collection is modified
+     * during the process of the method.
+     * 
+     * @param c the collection to be added to the queue.
+     * @return true if the operation succeeds.
+     * @throws NullPointerException if the collection or any element of it is
+     *         null.
+     * @throws IllegalArgumentException If the collection to be added to the
+     *         queue is the queue itself.
+     */
+    public boolean addAll(Collection<? extends E> c) {
+        if (null == c) {
+            throw new NullPointerException();
+        }
+        if (this == c) {
+            throw new IllegalArgumentException();
+        }
+        Iterator<? extends E> iter = c.iterator();
+        while (iter.hasNext()) {
+            E o = iter.next();
+            add(o);
+        }
+        return true;
+
+    }
+
+    /**
+     * Gets and removes the element in the head of the queue.
+     * 
+     * @return the element in the head of the queue.
+     * @throws NoSuchElementException if the queue is empty.
+     */
+    public E remove() {
+        E o = poll();
+        if (null == o) {
+            throw new NoSuchElementException();
+        }
+        return o;
+    }
+
+    /**
+     * Gets but not removes the element in the head of the queue.
+     * 
+     * @return the element in the head of the queue.
+     * @throws NoSuchElementException if the queue is empty.
+     */
+    public E element() {
+        E o = peek();
+        if (null == o) {
+            throw new NoSuchElementException();
+        }
+        return o;
+    }
+
+    /**
+     * Removes all elements of the queue.
+     */
+    public void clear() {
+        E o;
+        do {
+            o = poll();
+        } while (null != o);
+    }
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AbstractQueueTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AbstractQueueTest.java?rev=412643&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AbstractQueueTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AbstractQueueTest.java Wed Jun  7 20:59:22 2006
@@ -0,0 +1,296 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.util;
+
+import java.util.AbstractQueue;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import junit.framework.TestCase;
+
+public class AbstractQueueTest extends TestCase {
+
+    private MockAbstractQueue<Object> queue;
+
+    private class MockAbstractQueue<E> extends AbstractQueue<E> {
+
+        static final int CAPACITY = 10;
+
+        private int size = 0;
+
+        private Object[] elements = new Object[CAPACITY];
+
+        public Iterator<E> iterator() {
+            return new Iterator<E>() {
+
+                private int currentIndex = -1;
+
+                public boolean hasNext() {
+                    return size > 0 && currentIndex < size;
+                }
+
+                public E next() {
+                    if (!hasNext()) {
+                        throw new NoSuchElementException();
+                    }
+                    currentIndex++;
+                    return (E) elements[currentIndex];
+                }
+
+                public void remove() {
+                    if (-1 == currentIndex) {
+                        throw new IllegalStateException();
+                    }
+                    for (int i = currentIndex; i < size - 1; i++) {
+                        elements[i] = elements[i + 1];
+                    }
+                    size--;
+                }
+            };
+        }
+
+        public int size() {
+            return size;
+        }
+
+        public boolean offer(E o) {
+            if (null == o) {
+                throw new NullPointerException();
+            }
+
+            if (size >= CAPACITY) {
+                return false;
+            }
+
+            elements[size++] = o;
+            return true;
+        }
+
+        public E poll() {
+            if (isEmpty()) {
+                return null;
+            }
+            E e = (E) elements[0];
+            for (int i = 0; i < size - 1; i++) {
+                elements[i] = elements[i + 1];
+            }
+            size--;
+            return e;
+        }
+
+        public E peek() {
+            if (isEmpty()) {
+                return null;
+            }
+            return (E) elements[0];
+        }
+
+    }
+
+    /**
+     * @tests java.util.AbstractQueue.add(E)
+     */
+    public void test_addLE_null() {
+        try {
+            queue.add(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.AbstractQueue.add(E)
+     */
+    public void test_addLE_Full() {
+        Object o = new Object();
+
+        for(int i = 0; i < MockAbstractQueue.CAPACITY; i++ ) {
+            queue.add(o);
+        }
+
+        try {
+            queue.add(o);
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            //expected
+        }
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#add(E)
+     */
+    public void test_addLE() {
+        Object o = new Object();
+        final int LAST_INDEX = 4;
+        for (int i = 0; i < LAST_INDEX; i++) {
+            queue.add(o);
+        }
+        Integer I = new Integer(123456);
+        queue.add(I);
+        assertTrue(queue.contains(I));
+        Iterator iter = queue.iterator();
+        for (int i = 0; i < LAST_INDEX; i++) {
+            iter.next();
+        }
+        assertTrue(I == iter.next());
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#addAll(E)
+     */
+    public void test_addAllLE_null() {
+        try {
+            queue.addAll(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#addAll(E)
+     */
+    public void test_addAllLE_with_null() {
+        List list = Arrays.asList("MYTESTSTRING", null, new Float(123.456));
+        try {
+            queue.addAll(list);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#addAll(E)
+     */
+    public void test_addAllLE_full() {
+        List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
+        try {
+            queue.addAll(list);
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#addAll(E)
+     */
+    public void test_addAllLE_this() {
+        try {
+            queue.addAll(queue);
+            fail("should throw IllegalArgumentException ");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#clear()
+     */
+    public void test_clear_empty() {
+        queue.clear();
+        assertTrue(queue.isEmpty());
+        assertNull(queue.peek());
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#clear()
+     */
+    public void test_clear() {
+        List list = Arrays.asList(123.456, "MYTESTSTRING", new Object(), 'c');
+        queue.addAll(list);
+        queue.clear();
+        assertTrue(queue.isEmpty());
+        assertNull(queue.peek());
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#AbstractQueue()
+     */
+    public void test_Constructor() {
+        MockAbstractQueue queue = new MockAbstractQueue();
+        assertNotNull(queue);
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#remove()
+     */
+    public void test_remove_null() {
+        try {
+            queue.remove();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#remove()
+     */
+    public void test_remove() {
+        char c = 'a';
+        queue.add(c);
+        c = 'b';
+        queue.add(c);
+        assertEquals('a', queue.remove());
+        assertEquals('b', queue.remove());
+        try {
+            queue.remove();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#element()
+     */
+    public void test_element_empty() {
+        try {
+            queue.element();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.AbstractQueue#element()
+     */
+    public void test_element() {
+        String s = "MYTESTSTRING_ONE";
+        queue.add(s);
+        s = "MYTESTSTRING_TWO";
+        queue.add(s);
+        assertEquals("MYTESTSTRING_ONE", queue.element());
+        // still the first element
+        assertEquals("MYTESTSTRING_ONE", queue.element());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        queue = new MockAbstractQueue<Object>();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        queue = null;
+    }
+}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java?rev=412643&r1=412642&r2=412643&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java Wed Jun  7 20:59:22 2006
@@ -35,6 +35,7 @@
 		suite.addTestSuite(AbstractCollectionTest.class);
 		suite.addTestSuite(AbstractListTest.class);
 		suite.addTestSuite(AbstractMapTest.class);
+        suite.addTestSuite(AbstractQueueTest.class);
 		suite.addTestSuite(ArrayListTest.class);
 		suite.addTestSuite(ArraysTest.class);
 		suite.addTestSuite(BitSetTest.class);