ListOrderedMap - additional method, put(int,Object,Object)

rfe 37761, from Matt Benson

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@365406 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-01-02 19:34:53 +00:00
parent 0f2b464324
commit 32769fd169
4 changed files with 163 additions and 3 deletions

View File

@ -77,6 +77,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
<li>Transformed*Map - new factory decorateTransform() that transforms any existing entries in the map [30959]</li> <li>Transformed*Map - new factory decorateTransform() that transforms any existing entries in the map [30959]</li>
<li>ListOrderedMap - values can now be accessed as a List using valueList() [37015]</li> <li>ListOrderedMap - values can now be accessed as a List using valueList() [37015]</li>
<li>ListOrderedMap - additional list-like method, setValue(int,Object)</li> <li>ListOrderedMap - additional list-like method, setValue(int,Object)</li>
<li>ListOrderedMap - additional method, put(int,Object,Object)</li>
<li>PriorityBuffer - now Serializable [36163]</li> <li>PriorityBuffer - now Serializable [36163]</li>
</ul> </ul>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
Copyright 2002-2005 The Apache Software Foundation Copyright 2002-2006 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -187,6 +187,9 @@
<contributor> <contributor>
<name>Sebastian Bazley</name> <name>Sebastian Bazley</name>
</contributor> </contributor>
<contributor>
<name>Matt Benson</name>
</contributor>
<contributor> <contributor>
<name>Ola Berg</name> <name>Ola Berg</name>
</contributor> </contributor>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2003-2005 The Apache Software Foundation * Copyright 2003-2006 The Apache Software Foundation
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -65,6 +65,7 @@ import org.apache.commons.collections.list.UnmodifiableList;
* *
* @author Henri Yandell * @author Henri Yandell
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Matt Benson
*/ */
public class ListOrderedMap public class ListOrderedMap
extends AbstractMapDecorator extends AbstractMapDecorator
@ -377,6 +378,44 @@ public class ListOrderedMap
return put(key, value); return put(key, value);
} }
/**
* Puts a key-value mapping into the map at the specified index.
* <p>
* If the map already contains the key, then the original mapping
* is removed and the new mapping added at the specified index.
* The remove may change the effect of the index. The index is
* always calculated relative to the original state of the map.
* <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.
*
* @param index the index at which the mapping should be inserted
* @param key the key
* @param value the value
* @return the value previously mapped to the key
* @throws IndexOutOfBoundsException if the index is out of range
* @since Commons Collections 3.2
*/
public Object put(int index, Object key, Object value) {
Map m = getMap();
if (m.containsKey(key)) {
Object result = m.remove(key);
int pos = insertOrder.indexOf(key);
insertOrder.remove(pos);
if (pos < index) {
index--;
}
insertOrder.add(index, key);
m.put(key, value);
return result;
} else {
insertOrder.add(index, key);
m.put(key, value);
return null;
}
}
/** /**
* Removes the element at the specified index. * Removes the element at the specified index.
* *

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2005 The Apache Software Foundation * Copyright 2001-2006 The Apache Software Foundation
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,6 +35,7 @@ import org.apache.commons.collections.list.AbstractTestList;
* *
* @author Henri Yandell * @author Henri Yandell
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Matt Benson
*/ */
public class TestListOrderedMap extends AbstractTestOrderedMap { public class TestListOrderedMap extends AbstractTestOrderedMap {
@ -182,6 +183,122 @@ public class TestListOrderedMap extends AbstractTestOrderedMap {
} }
} }
public void testPut_intObjectObject() {
resetEmpty();
ListOrderedMap lom = (ListOrderedMap) map;
try {
lom.put(1, "testInsert1", "testInsert1v");
fail("should not be able to insert at pos 1 in empty Map");
} catch (IndexOutOfBoundsException ex) {}
try {
lom.put(-1, "testInsert-1", "testInsert-1v");
fail("should not be able to insert at pos -1 in empty Map");
} catch (IndexOutOfBoundsException ex) {}
// put where key doesn't exist
lom.put(0, "testInsert1", "testInsert1v");
assertEquals("testInsert1v", lom.getValue(0));
lom.put("testInsertPut", "testInsertPutv");
assertEquals("testInsert1v", lom.getValue(0));
assertEquals("testInsertPutv", lom.getValue(1));
lom.put(0, "testInsert0", "testInsert0v");
assertEquals("testInsert0v", lom.getValue(0));
assertEquals("testInsert1v", lom.getValue(1));
assertEquals("testInsertPutv", lom.getValue(2));
lom.put(3, "testInsert3", "testInsert3v");
assertEquals("testInsert0v", lom.getValue(0));
assertEquals("testInsert1v", lom.getValue(1));
assertEquals("testInsertPutv", lom.getValue(2));
assertEquals("testInsert3v", lom.getValue(3));
// put in a full map
resetFull();
lom = (ListOrderedMap) map;
ListOrderedMap lom2 = new ListOrderedMap();
lom2.putAll(lom);
lom2.put(0, "testInsert0", "testInsert0v");
assertEquals("testInsert0v", lom2.getValue(0));
for (int i = 0; i < lom.size(); i++) {
assertEquals(lom2.getValue(i + 1), lom.getValue(i));
}
// put where key does exist
Integer i1 = new Integer(1);
Integer i1b = new Integer(1);
Integer i2 = new Integer(2);
Integer i3 = new Integer(3);
resetEmpty();
lom = (ListOrderedMap) map;
lom.put(i1, "1");
lom.put(i2, "2");
lom.put(i3, "3");
lom.put(0, i1, "One");
assertEquals(3, lom.size());
assertEquals(3, lom.map.size());
assertEquals(3, lom.insertOrder.size());
assertEquals("One", lom.getValue(0));
assertSame(i1, lom.get(0));
resetEmpty();
lom = (ListOrderedMap) map;
lom.put(i1, "1");
lom.put(i2, "2");
lom.put(i3, "3");
lom.put(0, i1b, "One");
assertEquals(3, lom.size());
assertEquals(3, lom.map.size());
assertEquals(3, lom.insertOrder.size());
assertEquals("One", lom.getValue(0));
assertEquals("2", lom.getValue(1));
assertEquals("3", lom.getValue(2));
assertSame(i1b, lom.get(0));
resetEmpty();
lom = (ListOrderedMap) map;
lom.put(i1, "1");
lom.put(i2, "2");
lom.put(i3, "3");
lom.put(1, i1b, "One");
assertEquals(3, lom.size());
assertEquals(3, lom.map.size());
assertEquals(3, lom.insertOrder.size());
assertEquals("One", lom.getValue(0));
assertEquals("2", lom.getValue(1));
assertEquals("3", lom.getValue(2));
resetEmpty();
lom = (ListOrderedMap) map;
lom.put(i1, "1");
lom.put(i2, "2");
lom.put(i3, "3");
lom.put(2, i1b, "One");
assertEquals(3, lom.size());
assertEquals(3, lom.map.size());
assertEquals(3, lom.insertOrder.size());
assertEquals("2", lom.getValue(0));
assertEquals("One", lom.getValue(1));
assertEquals("3", lom.getValue(2));
resetEmpty();
lom = (ListOrderedMap) map;
lom.put(i1, "1");
lom.put(i2, "2");
lom.put(i3, "3");
lom.put(3, i1b, "One");
assertEquals(3, lom.size());
assertEquals(3, lom.map.size());
assertEquals(3, lom.insertOrder.size());
assertEquals("2", lom.getValue(0));
assertEquals("3", lom.getValue(1));
assertEquals("One", lom.getValue(2));
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public void testValueList_getByIndex() { public void testValueList_getByIndex() {
resetFull(); resetFull();