commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r365406 - in /jakarta/commons/proper/collections/trunk: RELEASE-NOTES.html project.xml src/java/org/apache/commons/collections/map/ListOrderedMap.java src/test/org/apache/commons/collections/map/TestListOrderedMap.java
Date Mon, 02 Jan 2006 19:35:01 GMT
Author: scolebourne
Date: Mon Jan  2 11:34:53 2006
New Revision: 365406

URL: http://svn.apache.org/viewcvs?rev=365406&view=rev
Log:
ListOrderedMap - additional method, put(int,Object,Object)
rfe 37761, from Matt Benson

Modified:
    jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
    jakarta/commons/proper/collections/trunk/project.xml
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
    jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java

Modified: jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html?rev=365406&r1=365405&r2=365406&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html (original)
+++ jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html Mon Jan  2 11:34:53 2006
@@ -77,6 +77,7 @@
 <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 - additional list-like method, setValue(int,Object)</li>
+<li>ListOrderedMap - additional method, put(int,Object,Object)</li>
 <li>PriorityBuffer - now Serializable [36163]</li>
 </ul>
 

Modified: jakarta/commons/proper/collections/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/project.xml?rev=365406&r1=365405&r2=365406&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/project.xml (original)
+++ jakarta/commons/proper/collections/trunk/project.xml Mon Jan  2 11:34:53 2006
@@ -1,6 +1,6 @@
 <?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");
    you may not use this file except in compliance with the License.
@@ -186,6 +186,9 @@
     </contributor>
     <contributor>
       <name>Sebastian Bazley</name>
+    </contributor>
+    <contributor>
+      <name>Matt Benson</name>
     </contributor>
     <contributor>
       <name>Ola Berg</name>

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java?rev=365406&r1=365405&r2=365406&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
Mon Jan  2 11:34:53 2006
@@ -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");
  *  you may not use this file except in compliance with the License.
@@ -65,6 +65,7 @@
  * 
  * @author Henri Yandell
  * @author Stephen Colebourne
+ * @author Matt Benson
  */
 public class ListOrderedMap
         extends AbstractMapDecorator
@@ -375,6 +376,44 @@
     public Object setValue(int index, Object value) {
         Object key = insertOrder.get(index);
         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;
+        }
     }
 
     /**

Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java?rev=365406&r1=365405&r2=365406&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java
Mon Jan  2 11:34:53 2006
@@ -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");
  *  you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
  * 
  * @author Henri Yandell
  * @author Stephen Colebourne
+ * @author Matt Benson
  */
 public class TestListOrderedMap extends AbstractTestOrderedMap {
 
@@ -180,6 +181,122 @@
             list.remove(i);
             assertEquals(false, lom.containsKey(key));
         }
+    }
+
+    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));
     }
 
     //-----------------------------------------------------------------------



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message