Adding a putAll method to ListOrderedMap as per COLLECTIONS-226 and Dave Meikle's patch

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@637512 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2008-03-16 02:37:19 +00:00
parent eb9cff359c
commit 1ef83170fe
2 changed files with 42 additions and 0 deletions

View File

@ -66,6 +66,7 @@ import org.apache.commons.collections.list.UnmodifiableList;
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Matt Benson * @author Matt Benson
* @author Dave Meikle
*/ */
public class ListOrderedMap public class ListOrderedMap
extends AbstractMapDecorator 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) { public Object remove(Object key) {
Object result = getMap().remove(key); Object result = getMap().remove(key);
insertOrder.remove(key); insertOrder.remove(key);

View File

@ -299,6 +299,32 @@ public class TestListOrderedMap extends AbstractTestOrderedMap {
assertEquals("One", lom.getValue(2)); 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() { public void testValueList_getByIndex() {
resetFull(); resetFull();