diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0ae417cce..f278a9d63 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -23,6 +23,10 @@ + + "SetUniqueList.addAll(int, Collection)" now correctly add the collection at the + provided index. + "CollectionUtils#removeAll" wrongly called "ListUtils#retainAll". @@ -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. - - "SetUniqueList.addAll(int, Collection)" now correctly add the collection at the - provided index. - "MultiValueMap#put(Object, Object)" and "MultiValueMap#putAll(Object, Collection)" now correctly return if the map has changed by this operation. diff --git a/src/java/org/apache/commons/collections/list/SetUniqueList.java b/src/java/org/apache/commons/collections/list/SetUniqueList.java index b07560835..3eb5f344c 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. *

* 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 diff --git a/src/test/org/apache/commons/collections/list/TestSetUniqueList.java b/src/test/org/apache/commons/collections/list/TestSetUniqueList.java index 560a88bd7..fe08150e9 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();