You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2012/06/20 19:53:26 UTC

svn commit: r1352235 - in /commons/proper/collections/trunk/src: main/java/org/apache/commons/collections/map/ListOrderedMap.java test/java/org/apache/commons/collections/map/TestListOrderedMap.java

Author: tn
Date: Wed Jun 20 17:53:25 2012
New Revision: 1352235

URL: http://svn.apache.org/viewvc?rev=1352235&view=rev
Log:
[COLLECTIONS-441] Fixed IndexOutOfBoundsException when using ListOrderedMap.putAll. Thanks for Adrian Nistor for reporting.

Modified:
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java?rev=1352235&r1=1352234&r2=1352235&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java Wed Jun 20 17:53:25 2012
@@ -242,8 +242,14 @@ public class ListOrderedMap<K, V>
      */
     public void putAll(int index, Map<? extends K, ? extends V> map) {
         for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
-            put(index, entry.getKey(), entry.getValue());
-            index++;
+            V old = put(index, entry.getKey(), entry.getValue());
+            if (old == null) {
+                // if no key was replaced, increment the index
+                index++;
+            } else {
+                // otherwise put the next item after the currently inserted key
+                index = indexOf(entry.getKey()) + 1;
+            }
         }
     }
 
@@ -416,7 +422,7 @@ public class ListOrderedMap<K, V>
      * <p>
      * Thus the steps are: (1) remove the existing key-value mapping,
      * then (2) insert the new key-value mapping at the position it
-     * would have been inserted had the remove not ocurred.
+     * would have been inserted had the remove not occurred.
      *
      * @param index  the index at which the mapping should be inserted
      * @param key  the key

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java?rev=1352235&r1=1352234&r2=1352235&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java Wed Jun 20 17:53:25 2012
@@ -25,6 +25,7 @@ import junit.framework.Test;
 
 import org.apache.commons.collections.BulkTest;
 import org.apache.commons.collections.MapIterator;
+import org.apache.commons.collections.OrderedMapIterator;
 import org.apache.commons.collections.list.AbstractTestList;
 
 /**
@@ -331,6 +332,29 @@ public class TestListOrderedMap<K, V> ex
         assertEquals("testInsert2v", lom.getValue(4));
     }
 
+    public void testPutAllWithIndexBug441() {
+        // see COLLECTIONS-441
+        resetEmpty();
+        ListOrderedMap<Integer, Boolean> lom = (ListOrderedMap<Integer, Boolean>) map;
+
+        int size = 5;
+        for (int i = 0; i < size; i++) {
+            lom.put(i, true);
+        }
+
+        HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
+        for (int i = 0; i < size; i++) {
+            map.put(i, true);
+        }
+
+        lom.putAll(3, map);
+        
+        List<Integer> orderedList = lom.asList();
+        for (int i = 0; i < size; i++) {
+            assertEquals(i, orderedList.get(i).intValue());
+        }
+    }
+    
     //-----------------------------------------------------------------------
     public void testValueList_getByIndex() {
         resetFull();