harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r437598 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/EnumMap.java test/java/tests/api/java/util/EnumMapTest.java
Date Mon, 28 Aug 2006 08:30:07 GMT
Author: pyang
Date: Mon Aug 28 01:30:06 2006
New Revision: 437598

URL: http://svn.apache.org/viewvc?rev=437598&view=rev
Log:
Patch applied for HARMONY-1287 ([classlib][luni] new method values() 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=437598&r1=437597&r2=437598&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
Mon Aug 28 01:30:06 2006
@@ -141,6 +141,62 @@
         }
     }
 
+    private static class EnumMapValueCollection<KT extends Enum<KT>, VT> extends
+            AbstractCollection<VT> {
+        private final EnumMap<KT, VT> enumMap;
+
+        EnumMapValueCollection(EnumMap<KT, VT> em) {
+            enumMap = em;
+        }
+
+        @Override
+        public void clear() {
+            enumMap.clear();
+        }
+
+        @Override
+        public boolean contains(Object object) {
+            return enumMap.containsValue(object);
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public Iterator iterator() {
+            return new EnumMapIterator<VT, KT, VT>(
+                    new MapEntry.Type<VT, KT, VT>() {
+                        public VT get(MapEntry<KT, VT> entry) {
+                            return entry.value;
+                        }
+                    }, enumMap);
+        }
+
+        @Override
+        public boolean remove(Object object) {
+            if (null == object) {
+                for (int i = 0; i < enumMap.enumSize; i++) {
+                    if (enumMap.hasMapping[i] && null == enumMap.values[i]) {
+                        enumMap.remove(enumMap.keys[i]);
+                        return true;
+                    }
+                }
+            } else {
+                for (int i = 0; i < enumMap.enumSize; i++) {
+                    if (enumMap.hasMapping[i]
+                            && object.equals(enumMap.values[i])) {
+                        enumMap.remove(enumMap.keys[i]);
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+
+        @Override
+        public int size() {
+            return enumMap.size();
+        }
+    }
+
     /**
      * Constructs an empty enum map using the given key type.
      * 
@@ -398,8 +454,12 @@
      * 
      * @return a collection view of the mappings contained in this map.
      */
+    @Override
     public Collection<V> values() {
-        throw new NotYetImplementedException();
+        if (null == valuesCollection) {
+            valuesCollection = new EnumMapValueCollection<K, V>(this);
+        }
+        return valuesCollection;
     }
 
     private boolean isValidKeyType(Object key) {

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=437598&r1=437597&r2=437598&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
Mon Aug 28 01:30:06 2006
@@ -295,9 +295,9 @@
                 .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$
+        assertFalse("Returned true when the object can not be removed", set //$NON-NLS-1$
                 .remove(Size.Big));
-        assertFalse("Returned true when the object cann't be removed", set //$NON-NLS-1$
+        assertFalse("Returned true when the object can not 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
@@ -660,7 +660,163 @@
         EnumMap enumEmptyMap = new EnumMap<Empty, Double>(Empty.class);
         assertEquals("Wrong size", 0, enumEmptyMap.size()); //$NON-NLS-1$
     }
-    
+
+    /**
+     * @tests java.util.EnumMap#values()
+     */
+    @SuppressWarnings( { "unchecked", "boxing" })
+    public void test_values() {
+        EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class);
+        enumColorMap.put(Color.Red, 1);
+        enumColorMap.put(Color.Blue, null);
+        Collection collection = enumColorMap.values();
+
+        Collection collection1 = enumColorMap.values();
+        assertSame("Should be same", collection1, collection); //$NON-NLS-1$
+        try {
+            collection.add(new Integer(1));
+            fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+
+        assertTrue("Returned false for contained object", collection//$NON-NLS-1$
+                .contains(1));
+        assertTrue("Returned false for contained object", collection//$NON-NLS-1$
+                .contains(null));
+        assertFalse("Returned true for uncontained object", collection //$NON-NLS-1$
+                .contains(2));
+
+        assertTrue("Returned false when the object can be removed", collection //$NON-NLS-1$
+                .remove(null));
+        assertFalse("Returned true for uncontained object", collection //$NON-NLS-1$
+                .contains(null));
+        assertFalse("Returned true when the object can not be removed", //$NON-NLS-1$
+                collection.remove(null));
+
+        // The set is backed by the map so changes to one are reflected by the
+        // other.
+        enumColorMap.put(Color.Blue, 3);
+        assertTrue("Returned false for contained object", collection//$NON-NLS-1$
+                .contains(3));
+        enumColorMap.remove(Color.Blue);
+        assertFalse("Returned true for uncontained object", collection//$NON-NLS-1$
+                .contains(3));
+
+        assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$
+        collection.clear();
+        assertEquals("Wrong size", 0, collection.size()); //$NON-NLS-1$
+
+        enumColorMap = new EnumMap<Color, Double>(Color.class);
+        enumColorMap.put(Color.Red, 1);
+        enumColorMap.put(Color.Blue, null);
+        collection = enumColorMap.values();
+        Collection c = new ArrayList();
+        c.add(new Integer(1));
+        assertTrue("Should return true", collection.containsAll(c)); //$NON-NLS-1$
+        c.add(new Double(3.4));
+        assertFalse("Should return false", collection.containsAll(c)); //$NON-NLS-1$
+        assertTrue("Should return true", collection.removeAll(c)); //$NON-NLS-1$
+        assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$
+        assertFalse("Should return false", collection.removeAll(c)); //$NON-NLS-1$
+        assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$
+        try {
+            collection.addAll(c);
+            fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+
+        enumColorMap.put(Color.Red, 1);
+        assertEquals("Wrong size", 2, collection.size()); //$NON-NLS-1$
+        assertTrue("Should return true", collection.retainAll(c)); //$NON-NLS-1$
+        assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$
+        assertFalse("Should return false", collection.retainAll(c)); //$NON-NLS-1$
+        assertEquals(1, collection.size());
+        Object[] array = collection.toArray();
+        assertEquals("Wrong length", 1, array.length); //$NON-NLS-1$
+        assertEquals("Wrong key", 1, array[0]); //$NON-NLS-1$
+
+        enumColorMap = new EnumMap<Color, Double>(Color.class);
+        enumColorMap.put(Color.Red, 1);
+        enumColorMap.put(Color.Blue, null);
+        collection = enumColorMap.values();
+
+        assertEquals("Wrong size", 2, collection.size()); //$NON-NLS-1$
+        assertFalse("Returned true when the object can not be removed", //$NON-NLS-1$
+                collection.remove(new Integer("10"))); //$NON-NLS-1$
+
+        Iterator iter = enumColorMap.values().iterator();
+        Object value = iter.next();
+        assertTrue("Returned false for contained object", collection //$NON-NLS-1$
+                .contains(value));
+        value = iter.next();
+        assertTrue("Returned false for contained object", collection //$NON-NLS-1$
+                .contains(value));
+
+        enumColorMap.put(Color.Green, 1);
+        enumColorMap.remove(Color.Blue);
+        assertFalse("Returned true for uncontained object", collection //$NON-NLS-1$
+                .contains(value));
+        iter.remove();
+        try {
+            iter.remove();
+            fail("Should throw IllegalStateException"); //$NON-NLS-1$
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        assertFalse("Returned true for uncontained object", collection //$NON-NLS-1$
+                .contains(value));
+
+        iter = enumColorMap.values().iterator();
+        value = iter.next();
+        assertTrue("Returned false for contained object", collection //$NON-NLS-1$
+                .contains(value));
+        enumColorMap.put(Color.Green, 3);
+        assertTrue("Returned false for contained object", collection //$NON-NLS-1$
+                .contains(value));
+        assertTrue("Returned false for contained object", collection //$NON-NLS-1$
+                .remove(new Integer("1"))); //$NON-NLS-1$
+        assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$
+        collection.clear();
+        assertEquals("Wrong size", 0, collection.size()); //$NON-NLS-1$
+
+        enumColorMap = new EnumMap<Color, Double>(Color.class);
+        Integer integer1 = new Integer(1);
+        enumColorMap.put(Color.Green, integer1);
+        enumColorMap.put(Color.Blue, null);
+        collection = enumColorMap.values();
+        iter = enumColorMap.values().iterator();
+        try {
+            iter.remove();
+            fail("Should throw IllegalStateException"); //$NON-NLS-1$
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        value = iter.next();
+        assertEquals("Wrong value", integer1, value); //$NON-NLS-1$
+        assertSame("Wrong value", integer1, value); //$NON-NLS-1$
+        assertFalse("Returned true for unequal object", iter.equals(value)); //$NON-NLS-1$
+        iter.remove();
+        assertFalse("Returned true for unequal object", iter.equals(value)); //$NON-NLS-1$
+        try {
+            iter.remove();
+            fail("Should throw IllegalStateException"); //$NON-NLS-1$
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$
+        value = iter.next();
+        assertFalse("Returned true for unequal object", iter.equals(value)); //$NON-NLS-1$
+        iter.remove();
+        try {
+            iter.next();
+            fail("Should throw NoSuchElementException"); //$NON-NLS-1$
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+    }
+
     /**
      * Sets up the fixture.
      */



Mime
View raw message