diff --git a/src/java/org/apache/commons/collections/map/ListOrderedMap.java b/src/java/org/apache/commons/collections/map/ListOrderedMap.java index bd957d594..f32714b68 100644 --- a/src/java/org/apache/commons/collections/map/ListOrderedMap.java +++ b/src/java/org/apache/commons/collections/map/ListOrderedMap.java @@ -66,6 +66,7 @@ import org.apache.commons.collections.list.UnmodifiableList; * * @author Stephen Colebourne * @author Matt Benson + * @author Dave Meikle */ public class ListOrderedMap extends AbstractMapDecorator @@ -223,6 +224,21 @@ public class ListOrderedMap } } + /** + * Puts the values contained in a supplied Map into the Map starting at + * the specified index. + * + * @param index the index in the Map to start at. + * @param map the Map containing the values to be added. + */ + public void putAll(int index, Map map) { + for (Iterator it = map.entrySet().iterator(); it.hasNext();) { + Map.Entry entry = (Map.Entry) it.next(); + put(index, entry.getKey(), entry.getValue()); + index++; + } + } + public Object remove(Object key) { Object result = getMap().remove(key); insertOrder.remove(key); diff --git a/src/test/org/apache/commons/collections/map/TestListOrderedMap.java b/src/test/org/apache/commons/collections/map/TestListOrderedMap.java index 971f2a62e..c6040df58 100644 --- a/src/test/org/apache/commons/collections/map/TestListOrderedMap.java +++ b/src/test/org/apache/commons/collections/map/TestListOrderedMap.java @@ -299,6 +299,32 @@ public class TestListOrderedMap extends AbstractTestOrderedMap { assertEquals("One", lom.getValue(2)); } + public void testPutAllWithIndex() { + resetEmpty(); + ListOrderedMap lom = (ListOrderedMap) map; + + // Create Initial Data + lom.put("testInsert0", "testInsert0v"); + lom.put("testInsert1", "testInsert1v"); + lom.put("testInsert2", "testInsert2v"); + assertEquals("testInsert0v", lom.getValue(0)); + assertEquals("testInsert1v", lom.getValue(1)); + assertEquals("testInsert2v", lom.getValue(2)); + + // Create New Test Map and Add using putAll(int, Object, Object) + Map values = new HashMap(); + values.put("NewInsert0", "NewInsert0v"); + values.put("NewInsert1", "NewInsert1v"); + lom.putAll(1, values); + + // Perform Asserts + assertEquals("testInsert0v", lom.getValue(0)); + assertEquals("NewInsert0v", lom.getValue(1)); + assertEquals("NewInsert1v", lom.getValue(2)); + assertEquals("testInsert1v", lom.getValue(3)); + assertEquals("testInsert2v", lom.getValue(4)); + } + //----------------------------------------------------------------------- public void testValueList_getByIndex() { resetFull();