harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r565261 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/ test/api/common/tests/api/java/util/
Date Mon, 13 Aug 2007 07:56:26 GMT
Author: leoli
Date: Mon Aug 13 00:56:25 2007
New Revision: 565261

URL: http://svn.apache.org/viewvc?view=rev&rev=565261
Log:
Apply patch for HARMONY-4603 ( [classlib][util] LinkedHashMap.clone() has problem when override
the removeEldestEntry method
).

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/HashMapTest.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/LinkedHashMapTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java?view=diff&rev=565261&r1=565260&r2=565261
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java Mon
Aug 13 00:56:25 2007
@@ -331,11 +331,16 @@
     public Object clone() {
         try {
             HashMap<K, V> map = (HashMap<K, V>) super.clone();
+            map.elementCount = 0;
             map.elementData = newElementArray(elementData.length);
             Entry<K, V> entry;
             for (int i = 0; i < elementData.length; i++) {
-                if ((entry = elementData[i]) != null) {
-                    map.elementData[i] = (Entry<K, V>) entry.clone();
+                if ((entry = elementData[i]) != null){
+                    map.putImpl(entry.getKey(), entry.getValue());
+                    while (entry.next != null){
+                        entry = entry.next;
+                        map.putImpl(entry.getKey(), entry.getValue());
+                    }
                 }
             }
             return map;
@@ -526,7 +531,7 @@
         return putImpl(key, value);
     }
 
-    private V putImpl(K key, V value) {
+    V putImpl(K key, V value) {
         Entry<K,V> entry;
         if(key == null) {
             entry = findNullKeyEntry();

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java?view=diff&rev=565261&r1=565260&r2=565261
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
Mon Aug 13 00:56:25 2007
@@ -332,7 +332,20 @@
      */
     @Override
     public V put(K key, V value) {
+        V result = putImpl(key,value);
+
+        if (removeEldestEntry(head)) {
+            remove(head.key);
+        }
+
+        return result;
+    }
+    
+    V putImpl(K key, V value){
         LinkedHashMapEntry<K, V> m;
+        if (elementCount == 0){
+            head = tail = null;
+        }
         if (key == null) {
             m = (LinkedHashMapEntry<K, V>)findNullKeyEntry();
             if (m == null) {
@@ -352,7 +365,7 @@
             int hash = key.hashCode();
             int index = (hash & 0x7FFFFFFF) % elementData.length;
             m = (LinkedHashMapEntry<K, V>)findNonNullKeyEntry(key, index, hash);
-            if (m == null) {
+            if (m == null) {                
                 modCount++;
                 if (++elementCount > threshold) {
                     rehash();
@@ -366,11 +379,6 @@
 
         V result = m.value;
         m.value = value;
-
-        if (removeEldestEntry(head)) {
-            remove(head.key);
-        }
-
         return result;
     }
 
@@ -583,21 +591,4 @@
         head = tail = null;
     }
 
-    /**
-     * Answers a new HashMap with the same mappings and size as this HashMap.
-     * 
-     * @return a shallow copy of this HashMap
-     * 
-     * @see java.lang.Cloneable
-     */
-    @Override
-    @SuppressWarnings("unchecked")
-    public Object clone() {
-        LinkedHashMap<K, V> map = (LinkedHashMap<K, V>) super.clone();
-        map.clear();
-        for (Map.Entry<K, V> entry : entrySet()) {
-            map.put(entry.getKey(), entry.getValue());
-        }
-        return map;
-    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/HashMapTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/HashMapTest.java?view=diff&rev=565261&r1=565260&r2=565261
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/HashMapTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/HashMapTest.java
Mon Aug 13 00:56:25 2007
@@ -187,6 +187,17 @@
 		assertTrue("keySet() is identical", key2 != keys);
 		assertEquals("keySet() was not cloned", 
 				"key2", key2.iterator().next());
+        
+        // regresion test for HARMONY-4603
+        HashMap hashmap = new HashMap();
+        MockClonable mock = new MockClonable(1);
+        hashmap.put(1, mock);
+        assertEquals(1, ((MockClonable) hashmap.get(1)).i);
+        HashMap hm3 = (HashMap)hashmap.clone();
+        assertEquals(1, ((MockClonable) hm3.get(1)).i);
+        mock.i = 0;
+        assertEquals(0, ((MockClonable) hashmap.get(1)).i);
+        assertEquals(0, ((MockClonable) hm3.get(1)).i);
 	}
 
 	/**
@@ -477,6 +488,19 @@
 	    expected += key.hashCode() ^ val.hashCode();
 	    assertEquals(expected, map.hashCode());
 	} 
+    
+    class MockClonable implements Cloneable{
+        public int i;
+        
+        public MockClonable(int i) {
+            this.i = i;
+        }
+        
+        @Override
+        protected Object clone() throws CloneNotSupportedException {
+            return new MockClonable(i);
+        }
+    }
 	
 	/**
 	 * Sets up the fixture, for example, open a network connection. This method

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/LinkedHashMapTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/LinkedHashMapTest.java?view=diff&rev=565261&r1=565260&r2=565261
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/LinkedHashMapTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/LinkedHashMapTest.java
Mon Aug 13 00:56:25 2007
@@ -371,6 +371,33 @@
 		assertEquals("keySet() was not cloned", 
 				"key2", key2.iterator().next());
 	}
+    
+    // regresion test for HARMONY-4603
+    public void test_clone_Mock() {
+        LinkedHashMap hashMap = new MockMap();
+        String value = "value a";
+        hashMap.put("key", value);
+        MockMap cloneMap = (MockMap) hashMap.clone();
+        assertEquals(value, cloneMap.get("key"));
+        assertEquals(hashMap, cloneMap);
+        assertEquals(1, cloneMap.num);
+
+        hashMap.put("key", "value b");
+        assertFalse(hashMap.equals(cloneMap));
+    }
+
+    class MockMap extends LinkedHashMap {
+        int num;
+
+        public Object put(Object k, Object v) {
+            num++;
+            return super.put(k, v);
+        }
+
+        protected boolean removeEldestEntry(Map.Entry e) {
+            return num > 1;
+        }
+    } 
 
 	/**
 	 * @tests java.util.LinkedHashMap#containsKey(java.lang.Object)



Mime
View raw message