harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r432185 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/EnumMap.java test/java/tests/api/java/util/EnumMapTest.java
Date Thu, 17 Aug 2006 09:04:10 GMT
Author: pyang
Date: Thu Aug 17 02:04:09 2006
New Revision: 432185

URL: http://svn.apache.org/viewvc?rev=432185&view=rev
Log:
Patch applied for HARMONY-1210([classlib][luni] new method 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=432185&r1=432184&r2=432185&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
Thu Aug 17 02:04:09 2006
@@ -176,9 +176,21 @@
      *            the value to be associated with the given key
      * @return the value to which this map maps the given key, or null if this
      *         map has no mapping for the given key.
+     * @throws NullPointerException
+     *             if the given key is null
      */
+    @Override
+    @SuppressWarnings("unchecked")
     public V put(K key, V value) {
-        throw new NotYetImplementedException();
+        validateKeyType(key);
+        int keyOrdinal = key.ordinal();
+        if (!hasMapping[keyOrdinal]) {
+            hasMapping[keyOrdinal] = true;
+            mappingsCount++;
+        }
+        V oldValue = (V) values[keyOrdinal];
+        values[keyOrdinal] = value;
+        return oldValue;
     }
 
     /**
@@ -228,6 +240,15 @@
      */
     public Collection<V> values() {
         throw new NotYetImplementedException();
+    }
+
+    private void validateKeyType(Object key) {
+        if(key == null){
+            throw new NullPointerException();
+        }
+        if(!keyType.isInstance(key)){
+            throw new ClassCastException();
+        }
     }
 
     @SuppressWarnings("unchecked")

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=432185&r1=432184&r2=432185&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
Thu Aug 17 02:04:09 2006
@@ -67,7 +67,52 @@
         assertNotNull(new EnumMap(Size.Middle.getClass()));
         
     }
+    
+    /**
+     * @tests java.util.EnumMap#put(Object,Object)
+     */
+    @SuppressWarnings( { "unchecked", "boxing" })
+    public void test_putLjava_lang_ObjectLjava_lang_Object() {
+        EnumMap enumSizeMap = new EnumMap(Size.class);
+        try {
+            enumSizeMap.put(Color.Red, 2);
+            fail("Expected ClassCastException"); //$NON-NLS-1$
+        } catch (ClassCastException e) {
+            // Expected
+        }
+        assertNull("Return non-null for non mapped key", enumSizeMap.put( //$NON-NLS-1$
+                Size.Small, 1));
 
+        EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class);
+        try {
+            enumColorMap.put(Size.Big, 2);
+            fail("Expected ClassCastException"); //$NON-NLS-1$
+        } catch (ClassCastException e) {
+            // Expected
+        }
+        try {
+            enumColorMap.put(null, 2);
+            fail("Expected NullPointerException"); //$NON-NLS-1$
+        } catch (NullPointerException e) {
+            // Expected
+        }
+        assertNull("Return non-null for non mapped key", enumColorMap.put( //$NON-NLS-1$
+                Color.Green, 2));
+        assertEquals("Return wrong value", 2, enumColorMap.put(Color.Green, //$NON-NLS-1$
+                new Double(4)));
+        assertEquals("Return wrong value", new Double(4), enumColorMap.put( //$NON-NLS-1$
+                Color.Green, new Integer("3")));//$NON-NLS-1$
+        assertEquals("Return wrong value", new Integer("3"), enumColorMap.put( //$NON-NLS-1$//$NON-NLS-2$
+                Color.Green, null));
+        Float f = new Float("3.4");//$NON-NLS-1$
+        assertNull("Return non-null for non mapped key", enumColorMap.put( //$NON-NLS-1$
+                Color.Green, f));
+        assertNull("Return non-null for non mapped key", enumColorMap.put( //$NON-NLS-1$
+                Color.Blue, 2));
+        assertEquals("Return wrong value", 2, enumColorMap.put(Color.Blue, //$NON-NLS-1$
+                new Double(4)));
+    }
+    
     /**
      * Sets up the fixture.
      */



Mime
View raw message