harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r433513 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/EnumMap.java test/java/tests/api/java/util/EnumMapTest.java
Date Tue, 22 Aug 2006 04:51:19 GMT
Author: pyang
Date: Mon Aug 21 21:51:19 2006
New Revision: 433513

URL: http://svn.apache.org/viewvc?rev=433513&view=rev
Log:
Patch applied for HARMONY-1246 ([classlib][luni] new methods 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=433513&r1=433512&r2=433513&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 21 21:51:19 2006
@@ -57,7 +57,7 @@
      *             if the map is null
      */
     public EnumMap(EnumMap<K, ? extends V> map) {
-        throw new NotYetImplementedException();
+        initialization(map);
     }
 
     /**
@@ -75,7 +75,22 @@
      *             if the map is null
      */
     public EnumMap(Map<K, ? extends V> map) {
-        throw new NotYetImplementedException();
+        if (map instanceof EnumMap) {
+            initialization((EnumMap<K, V>) map);
+        } else {
+            if (0 == map.size()) {
+                throw new IllegalArgumentException();
+            }
+            Iterator<K> iter = map.keySet().iterator();
+            K enumKey = iter.next();
+            Class clazz=enumKey.getClass();
+            if(clazz.isEnum()){
+                initialization(clazz);
+            }else{
+                initialization(clazz.getSuperclass());
+            }
+            putAllImpl(map);
+        }
     }
 
     /**
@@ -188,21 +203,7 @@
     @Override
     @SuppressWarnings("unchecked")
     public V put(K key, V value) {
-        if (null == key) {
-            throw new NullPointerException();
-        }
-
-        if (!isValidKeyType(key)) {
-            throw new ClassCastException();
-        }
-        int keyOrdinal = key.ordinal();
-        if (!hasMapping[keyOrdinal]) {
-            hasMapping[keyOrdinal] = true;
-            mappingsCount++;
-        }
-        V oldValue = (V) values[keyOrdinal];
-        values[keyOrdinal] = value;
-        return oldValue;
+        return putImpl(key,value);
     }
 
     /**
@@ -219,11 +220,7 @@
     @Override
     @SuppressWarnings("unchecked")
     public void putAll(Map<? extends K, ? extends V> map) {
-        Iterator iter = map.entrySet().iterator();
-        while (iter.hasNext()) {
-            Map.Entry entry = (Map.Entry) iter.next();
-            put((K) entry.getKey(), (V) entry.getValue());
-        }
+        putAllImpl(map);
     }
 
     /**
@@ -268,12 +265,48 @@
     }
 
     @SuppressWarnings("unchecked")
+    private void initialization(EnumMap enumMap){
+        keyType = enumMap.keyType;
+        keys = enumMap.keys;
+        enumSize = enumMap.enumSize;
+        values = enumMap.values.clone();
+        hasMapping = enumMap.hasMapping.clone();
+        mappingsCount = enumMap.mappingsCount;
+    }
+    
     private void initialization(Class<K> type) {
         keyType = type;
         keys = keyType.getEnumConstants();
         enumSize = keys.length;
         values = new Object[enumSize];
         hasMapping = new boolean[enumSize];
+    }
+    
+    @SuppressWarnings("unchecked")
+    private void putAllImpl(Map map){
+        Iterator iter = map.entrySet().iterator();
+        while (iter.hasNext()) {
+            Map.Entry entry = (Map.Entry) iter.next();
+            putImpl((K) entry.getKey(), (V) entry.getValue());
+        }
+    }
+  
+    @SuppressWarnings("unchecked")
+    private V putImpl(K key, V value) {
+        if (null == key) {
+            throw new NullPointerException();
+        }
+        if (!isValidKeyType(key)) {
+            throw new ClassCastException();
+        }
+        int keyOrdinal = key.ordinal();
+        if (!hasMapping[keyOrdinal]) {
+            hasMapping[keyOrdinal] = true;
+            mappingsCount++;
+        }
+        V oldValue = (V) values[keyOrdinal];
+        values[keyOrdinal] = value;
+        return oldValue;
     }
 
 }

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=433513&r1=433512&r2=433513&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 21 21:51:19 2006
@@ -17,6 +17,7 @@
 
 import java.util.EnumMap;
 import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.harmony.luni.util.NotYetImplementedException;
 
@@ -96,6 +97,98 @@
                 enumSizeMap.get(Size.Small));
         try {
             enumSizeMap.put(Color.Red, 2);
+            fail("Expected ClassCastException"); //$NON-NLS-1$
+        } catch (ClassCastException e) {
+            // Expected
+        }
+    }
+    
+    /**
+     * @tests java.util.EnumMap#EnumMap(EnumMap)
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void test_ConstructorLjava_util_EnumMap() {
+        EnumMap enumMap;
+        EnumMap enumColorMap = null;
+        try {
+            enumMap = new EnumMap(enumColorMap);
+            fail("Expected NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // Expected
+        }
+
+        enumColorMap = new EnumMap<Color, Double>(Color.class);
+        Double double1 = new Double(1);
+        enumColorMap.put(Color.Green, 2);
+        enumColorMap.put(Color.Blue, double1);
+        
+        enumMap = new EnumMap(enumColorMap);
+        assertEquals("Constructor fails", 2, enumMap.get(Color.Green)); //$NON-NLS-1$
+        assertSame("Constructor fails", double1, enumMap.get(Color.Blue)); //$NON-NLS-1$
+        assertNull("Constructor fails", enumMap.get(Color.Red)); //$NON-NLS-1$
+        enumMap.put(Color.Red, 1);
+        assertEquals("Wrong value", 1, enumMap.get(Color.Red)); //$NON-NLS-1$
+
+        try {
+            enumMap.put(Size.Middle, 2);
+            fail("Expected ClassCastException"); //$NON-NLS-1$
+        } catch (ClassCastException e) {
+            // Expected
+        }
+    }
+
+    /**
+     * @tests java.util.EnumMap#EnumMap(Map)
+     */
+    @SuppressWarnings({ "unchecked", "boxing" })
+    public void test_ConstructorLjava_util_Map() {
+        EnumMap enumMap;
+        Map enumColorMap = null;
+        try {
+            enumMap = new EnumMap(enumColorMap);
+            fail("Expected NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // Expected
+        }
+        enumColorMap = new EnumMap<Color, Double>(Color.class);
+        enumMap = new EnumMap(enumColorMap);
+        enumColorMap.put(Color.Blue, 3);
+        enumMap = new EnumMap(enumColorMap);
+
+        HashMap hashColorMap = null;
+        try {
+            enumMap = new EnumMap(hashColorMap);
+            fail("Expected NullPointerException");//$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // Expected
+        }
+
+        hashColorMap = new HashMap();
+        try {
+            enumMap = new EnumMap(hashColorMap);
+            fail("Expected IllegalArgumentException"); //$NON-NLS-1$
+        } catch (IllegalArgumentException e) {
+            // Expected
+        }
+
+        hashColorMap.put(Color.Green, 2);
+        enumMap = new EnumMap(hashColorMap);
+        assertEquals("Constructor fails", 2, enumMap.get(Color.Green)); //$NON-NLS-1$
+        assertNull("Constructor fails", enumMap.get(Color.Red)); //$NON-NLS-1$
+        enumMap.put(Color.Red, 1);
+        assertEquals("Wrong value", 1, enumMap.get(Color.Red)); //$NON-NLS-1$
+        hashColorMap.put(Size.Big, 3);
+        try {
+            enumMap = new EnumMap(hashColorMap);
+            fail("Expected ClassCastException"); //$NON-NLS-1$
+        } catch (ClassCastException e) {
+            // Expected
+        }
+
+        hashColorMap = new HashMap();
+        hashColorMap.put(new Integer(1), 1);
+        try {
+            enumMap = new EnumMap(hashColorMap);
             fail("Expected ClassCastException"); //$NON-NLS-1$
         } catch (ClassCastException e) {
             // Expected



Mime
View raw message