[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
This commit is contained in:
Thomas Neidhart 2012-06-20 17:53:25 +00:00
parent 37da8fcbc2
commit ff9488b31e
2 changed files with 33 additions and 3 deletions

View File

@ -242,8 +242,14 @@ public class ListOrderedMap<K, V>
*/ */
public void putAll(int index, Map<? extends K, ? extends V> map) { public void putAll(int index, Map<? extends K, ? extends V> map) {
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) { for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
put(index, entry.getKey(), entry.getValue()); V old = put(index, entry.getKey(), entry.getValue());
index++; 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> * <p>
* Thus the steps are: (1) remove the existing key-value mapping, * Thus the steps are: (1) remove the existing key-value mapping,
* then (2) insert the new key-value mapping at the position it * 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 index the index at which the mapping should be inserted
* @param key the key * @param key the key

View File

@ -25,6 +25,7 @@ import junit.framework.Test;
import org.apache.commons.collections.BulkTest; import org.apache.commons.collections.BulkTest;
import org.apache.commons.collections.MapIterator; import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.OrderedMapIterator;
import org.apache.commons.collections.list.AbstractTestList; import org.apache.commons.collections.list.AbstractTestList;
/** /**
@ -331,6 +332,29 @@ public class TestListOrderedMap<K, V> extends AbstractTestOrderedMap<K, V> {
assertEquals("testInsert2v", lom.getValue(4)); 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() { public void testValueList_getByIndex() {
resetFull(); resetFull();