Backport COLLECTIONS-249 to 3.2.2.

git-svn-id: 
https://svn.apache.org/repos/asf/commons/proper/collections/branches/COLLECTIONS_3_2_X@1713172
 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/commons-collections/commit/16b1e26b
Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/16b1e26b
Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/16b1e26b

Branch: refs/heads/COLLECTIONS_3_2_X
Commit: 16b1e26bd1399779a6bd7177951c0770631a6cc5
Parents: a2ddc7c
Author: Thomas Neidhart <t...@apache.org>
Authored: Sat Nov 7 20:26:30 2015 +0000
Committer: Thomas Neidhart <t...@apache.org>
Committed: Sat Nov 7 20:26:30 2015 +0000

----------------------------------------------------------------------
 src/changes/changes.xml                         |  8 +++---
 .../commons/collections/list/SetUniqueList.java | 10 ++++++--
 .../collections/list/TestSetUniqueList.java     | 26 +++++++++++++++++++-
 3 files changed, 37 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-collections/blob/16b1e26b/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0ae417c..f278a9d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -23,6 +23,10 @@
 
   <release version="3.2.2" date="20XX-XX-XX" description="This is a bugfix 
release.">
 
+    <action issue="COLLECTIONS-249" dev="bayard" type="fix" due-to="Joe Kelly">
+      "SetUniqueList.addAll(int, Collection)" now correctly add the collection 
at the
+      provided index.
+    </action>
     <action issue="COLLECTIONS-219" dev="scolebourne" type="fix" due-to="Tom 
Leccese">
       "CollectionUtils#removeAll" wrongly called "ListUtils#retainAll".
     </action>
@@ -74,10 +78,6 @@
       "Flat3Map#remove(Object)" will now return the correct value mapped to 
the removed key
       if the size of the map is less or equal 3.
     </action>
-    <action issue="COLLECTIONS-249" dev="bayard" type="fix" due-to="Joe Kelly">
-      "SetUniqueList.addAll(int, Collection)" now correctly add the collection 
at the
-      provided index.
-    </action>
     <action issue="COLLECTIONS-228" dev="scolebourne" type="fix">
       "MultiValueMap#put(Object, Object)" and "MultiValueMap#putAll(Object, 
Collection)"
       now correctly return if the map has changed by this operation.

http://git-wip-us.apache.org/repos/asf/commons-collections/blob/16b1e26b/src/java/org/apache/commons/collections/list/SetUniqueList.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/commons/collections/list/SetUniqueList.java 
b/src/java/org/apache/commons/collections/list/SetUniqueList.java
index b075608..3eb5f34 100644
--- a/src/java/org/apache/commons/collections/list/SetUniqueList.java
+++ b/src/java/org/apache/commons/collections/list/SetUniqueList.java
@@ -168,7 +168,8 @@ public class SetUniqueList extends 
AbstractSerializableListDecorator {
     }
 
     /**
-     * Adds a collection of objects to the end of the list avoiding duplicates.
+     * Adds a collection of objects a specific index in the list avoiding 
+     * duplicates. 
      * <p>
      * Only elements that are not already in this list will be added, and
      * duplicates from the specified collection will be ignored.
@@ -187,7 +188,12 @@ public class SetUniqueList extends 
AbstractSerializableListDecorator {
 
         // adds all elements
         for (final Iterator it = coll.iterator(); it.hasNext();) {
-            add(it.next());
+            int sizeBeforeAddNext = size();
+            add(index, it.next());
+            // if it was inserted, then increase the target index
+            if (sizeBeforeAddNext != size()) {
+                index++;
+            }            
         }
 
         // compares sizes to detect if collection changed

http://git-wip-us.apache.org/repos/asf/commons-collections/blob/16b1e26b/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
----------------------------------------------------------------------
diff --git 
a/src/test/org/apache/commons/collections/list/TestSetUniqueList.java 
b/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
index 560a88b..fe08150 100644
--- a/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
+++ b/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
@@ -144,7 +144,31 @@ public class TestSetUniqueList extends AbstractTestList {
         assertEquals("Size should increase after addAll", 
                      size + elements.length, collection.size());
     }
-    
+
+    public void testIntCollectionAddAll() {
+        // make a SetUniqueList with one element
+        List list = new SetUniqueList(new ArrayList(), new HashSet());
+        final Integer existingElement = new Integer(1);
+        list.add(existingElement);
+
+        // add two new unique elements at index 0
+        final Integer firstNewElement = new Integer(2);
+        final Integer secondNewElement = new Integer(3);
+        collection = Arrays.asList(new Integer[] { firstNewElement, 
secondNewElement });
+        list.addAll(0, collection);
+        assertEquals("Unique elements should be added.", 3, list.size());
+        assertEquals("First new element should be at index 0", 
firstNewElement, list.get(0));
+        assertEquals("Second new element should be at index 1", 
secondNewElement, list.get(1));
+        assertEquals("Existing element should shift to index 2", 
existingElement, list.get(2));
+
+        // add a duplicate element and a unique element at index 0
+        final Integer thirdNewElement = new Integer(4);
+        collection = Arrays.asList(new Integer[] { existingElement, 
thirdNewElement });
+        list.addAll(0, collection);
+        assertEquals("Duplicate element should not be added, unique element 
should be added.", 4, list.size());
+        assertEquals("Third new element should be at index 0", 
thirdNewElement, list.get(0));
+    }
+
     public void testListSetByIndex() {
         // override for set behaviour
         resetFull();

Reply via email to