From ff9488b31ec4a6056f55c32ad3629de31e987ab7 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Wed, 20 Jun 2012 17:53:25 +0000 Subject: [PATCH] [COLLECTIONS-441] Fixed IndexOutOfBoundsException when using ListOrderedMap.putAll. Thanks for Adrian Nistor for reporting. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1352235 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections/map/ListOrderedMap.java | 12 +++++++--- .../collections/map/TestListOrderedMap.java | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java b/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java index 6c491836e..b7606b24a 100644 --- a/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java +++ b/src/main/java/org/apache/commons/collections/map/ListOrderedMap.java @@ -242,8 +242,14 @@ public class ListOrderedMap */ public void putAll(int index, Map map) { for (Map.Entry 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 *

* 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 diff --git a/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java b/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java index 1dfa01e24..90682fdf8 100644 --- a/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java +++ b/src/test/java/org/apache/commons/collections/map/TestListOrderedMap.java @@ -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 extends AbstractTestOrderedMap { assertEquals("testInsert2v", lom.getValue(4)); } + public void testPutAllWithIndexBug441() { + // see COLLECTIONS-441 + resetEmpty(); + ListOrderedMap lom = (ListOrderedMap) map; + + int size = 5; + for (int i = 0; i < size; i++) { + lom.put(i, true); + } + + HashMap map = new HashMap(); + for (int i = 0; i < size; i++) { + map.put(i, true); + } + + lom.putAll(3, map); + + List orderedList = lom.asList(); + for (int i = 0; i < size; i++) { + assertEquals(i, orderedList.get(i).intValue()); + } + } + //----------------------------------------------------------------------- public void testValueList_getByIndex() { resetFull();