harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r436704 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/EnumMap.java test/java/tests/api/java/util/EnumMapTest.java
Date Fri, 25 Aug 2006 07:57:41 GMT
Author: pyang
Date: Fri Aug 25 00:57:40 2006
New Revision: 436704

URL: http://svn.apache.org/viewvc?rev=436704&view=rev
Log:
Patch applied for HARMONY-1277 ([classlib][luni] new method keySet() in java.util.EnumMap)

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

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java?rev=436704&r1=436703&r2=436704&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/EnumMap.java
Fri Aug 25 00:57:40 2006
@@ -35,6 +35,112 @@
 
     transient int enumSize;
 
+    private static class EnumMapIterator<E, KT extends Enum<KT>, VT> implements
+            Iterator<E> {
+        private int position = 0;
+
+        private int prePosition = -1;
+
+        private final EnumMap<KT, VT> enumMap;
+
+        private final MapEntry.Type<E, KT, VT> type;
+
+        EnumMapIterator(MapEntry.Type<E, KT, VT> value, EnumMap<KT, VT> em) {
+            enumMap = em;
+            type = value;
+        }
+
+        public boolean hasNext() {
+            int length = enumMap.enumSize;
+            for (; position < length; position++) {
+                if (enumMap.hasMapping[position]) {
+                    break;
+                }
+            }
+            return position != length;
+        }
+
+        @SuppressWarnings("unchecked")
+        public E next() {
+            if (!hasNext()) {
+                throw new NoSuchElementException();
+            }
+            prePosition = position++;
+            return type.get(new MapEntry(enumMap.keys[prePosition],
+                    enumMap.values[prePosition]));
+        }
+
+        public void remove() {
+            checkStatus();
+            if (enumMap.hasMapping[prePosition]) {
+                enumMap.remove(enumMap.keys[prePosition]);
+            }
+            prePosition = -1;
+        }
+
+        @Override
+        @SuppressWarnings("unchecked")
+        public String toString() {
+            if (-1 == prePosition) {
+                return super.toString();
+            }
+            return type.get(
+                    new MapEntry(enumMap.keys[prePosition],
+                            enumMap.values[prePosition])).toString();
+        }
+
+        void checkStatus() {
+            if (-1 == prePosition) {
+                throw new IllegalStateException();
+            }
+        }
+    }
+
+    private static class EnumMapKeySet<KT extends Enum<KT>, VT> extends
+            AbstractSet<KT> {
+        private final EnumMap<KT, VT> enumMap;
+
+        EnumMapKeySet(EnumMap<KT, VT> em) {
+            enumMap = em;
+        }
+
+        @Override
+        public void clear() {
+            enumMap.clear();
+        }
+
+        @Override
+        public boolean contains(Object object) {
+            return enumMap.containsKey(object);
+        }
+
+        @Override
+        @SuppressWarnings("unchecked")
+        public Iterator iterator() {
+            return new EnumMapIterator<KT, KT, VT>(
+                    new MapEntry.Type<KT, KT, VT>() {
+                        public KT get(MapEntry<KT, VT> entry) {
+                            return entry.key;
+                        }
+                    }, enumMap);
+        }
+
+        @Override
+        @SuppressWarnings("unchecked")
+        public boolean remove(Object object) {
+            if (contains(object)) {
+                enumMap.remove(object);
+                return true;
+            }
+            return false;
+        }
+
+        @Override
+        public int size() {
+            return enumMap.size();
+        }
+    }
+
     /**
      * Constructs an empty enum map using the given key type.
      * 
@@ -205,8 +311,12 @@
      * 
      * @return a set view of the keys contained in this map.
      */
+    @Override
     public Set<K> keySet() {
-        throw new NotYetImplementedException();
+        if (null == keySet) {
+            keySet = new EnumMapKeySet<K, V>(this);
+        }
+        return keySet;
     }
 
     /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java?rev=436704&r1=436703&r2=436704&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/EnumMapTest.java
Fri Aug 25 00:57:40 2006
@@ -15,9 +15,14 @@
 
 package tests.api.java.util;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.EnumMap;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
 
 import org.apache.harmony.luni.util.NotYetImplementedException;
 
@@ -257,6 +262,163 @@
         enumSizeMap.put(Size.Big, null);
         assertTrue("Returned false for contained value", enumSizeMap //$NON-NLS-1$
                 .containsValue(null));
+    }
+
+    /**
+     * @tests java.util.EnumMap#keySet()
+     */
+    @SuppressWarnings( { "unchecked", "boxing" })
+    public void test_keySet() {
+        EnumMap enumSizeMap = new EnumMap(Size.class);
+        enumSizeMap.put(Size.Middle, 2);
+        enumSizeMap.put(Size.Big, null);
+        Set set = enumSizeMap.keySet();
+
+        Set set1 = enumSizeMap.keySet();
+        assertSame("Should be same", set1, set); //$NON-NLS-1$
+        try {
+            set.add(Size.Big);
+            fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+
+        assertTrue("Returned false for contained object", set//$NON-NLS-1$
+                .contains(Size.Middle));
+        assertTrue("Returned false for contained object", set//$NON-NLS-1$
+                .contains(Size.Big));
+        assertFalse("Returned true for uncontained object", set //$NON-NLS-1$
+                .contains(Size.Small));
+        assertFalse("Returned true for uncontained object", set //$NON-NLS-1$
+                .contains(new Integer(1)));
+        assertTrue("Returned false when the object can be removed", set //$NON-NLS-1$
+                .remove(Size.Big));
+        assertFalse("Returned true for uncontained object", set //$NON-NLS-1$
+                .contains(Size.Big));
+        assertFalse("Returned true when the object cann't be removed", set //$NON-NLS-1$
+                .remove(Size.Big));
+        assertFalse("Returned true when the object cann't be removed", set //$NON-NLS-1$
+                .remove(new Integer(1)));
+
+        // The set is backed by the map so changes to one are reflected by the
+        // other.
+        enumSizeMap.put(Size.Big, 3);
+        assertTrue("Returned false for contained object", set//$NON-NLS-1$
+                .contains(Size.Big));
+        enumSizeMap.remove(Size.Big);
+        assertFalse("Returned true for uncontained object", set //$NON-NLS-1$
+                .contains(Size.Big));
+
+        assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$
+        set.clear();
+        assertEquals("Wrong size", 0, set.size()); //$NON-NLS-1$
+
+        enumSizeMap = new EnumMap(Size.class);
+        enumSizeMap.put(Size.Middle, 1);
+        enumSizeMap.put(Size.Big, null);
+        set = enumSizeMap.keySet();
+        Collection c = new ArrayList();
+        c.add(Size.Big);
+        assertTrue("Should return true", set.containsAll(c)); //$NON-NLS-1$
+        c.add(Size.Small);
+        assertFalse("Should return false", set.containsAll(c)); //$NON-NLS-1$
+        assertTrue("Should return true", set.removeAll(c)); //$NON-NLS-1$
+        assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$
+        assertFalse("Should return false", set.removeAll(c)); //$NON-NLS-1$
+        assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$
+        try {
+            set.addAll(c);
+            fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+
+        enumSizeMap.put(Size.Big, null);
+        assertEquals("Wrong size", 2, set.size()); //$NON-NLS-1$
+        assertTrue("Should return true", set.retainAll(c)); //$NON-NLS-1$
+        assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$
+        assertFalse("Should return false", set.retainAll(c)); //$NON-NLS-1$
+        assertEquals(1, set.size());
+        Object[] array = set.toArray();
+        assertEquals("Wrong length", 1, array.length); //$NON-NLS-1$
+        assertEquals("Wrong key", Size.Big, array[0]); //$NON-NLS-1$
+
+        enumSizeMap = new EnumMap(Size.class);
+        enumSizeMap.put(Size.Middle, 1);
+        enumSizeMap.put(Size.Big, null);
+        set = enumSizeMap.keySet();
+        c = new ArrayList();
+        c.add(Color.Blue);
+        assertFalse("Should return false", set.remove(c)); //$NON-NLS-1$
+        assertEquals("Wrong size", 2, set.size()); //$NON-NLS-1$
+        assertTrue("Should return true", set.retainAll(c)); //$NON-NLS-1$
+        assertEquals("Wrong size", 0, set.size()); //$NON-NLS-1$
+
+        enumSizeMap = new EnumMap(Size.class);
+        enumSizeMap.put(Size.Middle, 1);
+        enumSizeMap.put(Size.Big, null);
+        set = enumSizeMap.keySet();
+
+        Iterator iter = set.iterator();
+        Enum enumKey = (Enum) iter.next();
+        assertTrue("Returned false for contained object", set.contains(enumKey)); //$NON-NLS-1$
+        enumKey = (Enum) iter.next();
+        assertTrue("Returned false for contained object", set.contains(enumKey)); //$NON-NLS-1$
+
+        enumSizeMap.remove(Size.Big);
+        assertFalse("Returned true for uncontained object", set //$NON-NLS-1$
+                .contains(enumKey));
+        iter.remove();
+        try {
+            iter.remove();
+            fail("Should throw IllegalStateException"); //$NON-NLS-1$
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        assertFalse("Returned true for uncontained object", set //$NON-NLS-1$
+                .contains(enumKey));
+
+        iter = set.iterator();
+        enumKey = (Enum) iter.next();
+        assertTrue("Returned false for contained object", set.contains(enumKey)); //$NON-NLS-1$
+        enumSizeMap.put(Size.Middle, 3);
+        assertTrue("Returned false for contained object", set.contains(enumKey)); //$NON-NLS-1$
+
+        enumSizeMap = new EnumMap(Size.class);
+        enumSizeMap.put(Size.Middle, 1);
+        enumSizeMap.put(Size.Big, null);
+        set = enumSizeMap.keySet();
+        iter = set.iterator();
+        try {
+            iter.remove();
+            fail("Should throw IllegalStateException"); //$NON-NLS-1$
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        enumKey = (Enum) iter.next();
+        assertEquals("Wrong key", Size.Middle, enumKey); //$NON-NLS-1$
+        assertSame("Wrong key", Size.Middle, enumKey); //$NON-NLS-1$
+        assertFalse("Returned true for unequal object", iter.equals(enumKey)); //$NON-NLS-1$
+        iter.remove();
+        assertFalse("Returned true for uncontained object", set //$NON-NLS-1$
+                .contains(enumKey));
+        try {
+            iter.remove();
+            fail("Should throw IllegalStateException"); //$NON-NLS-1$
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+
+        assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$
+        enumKey = (Enum) iter.next();
+        assertEquals("Wrong key", Size.Big, enumKey); //$NON-NLS-1$
+        iter.remove();
+        try {
+            iter.next();
+            fail("Should throw NoSuchElementException"); //$NON-NLS-1$
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
     }
 
     /**



Mime
View raw message