diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 3d01a826b..e69dda33e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -65,7 +65,7 @@ Added "LazyIteratorChain" iterator. - + SetUniqueList.set(int, E) now works correctly if the object to be inserted is already placed at the given position. diff --git a/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java b/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java index f151a341f..bd6e6c180 100644 --- a/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java +++ b/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java @@ -214,7 +214,7 @@ public class SetUniqueList extends AbstractSerializableListDecorator { * Sets the value at the specified index avoiding duplicates. *

* The object is set into the specified index. Afterwards, any previous - * duplicate is removed If the object is not already in the list then a + * duplicate is removed. If the object is not already in the list then a * normal set occurs. If it is present, then the old version is removed. * * @param index @@ -229,15 +229,14 @@ public class SetUniqueList extends AbstractSerializableListDecorator { final E removed = super.set(index, object); if (pos != -1 && pos != index) { - // the object is already in the uniq list + // the object is already in the unique list // (and it hasn't been swapped with itself) super.remove(pos); // remove the duplicate by index - set.remove(removed); // remove the item deleted by the set - } else if (pos == -1) { - set.add(object); // add the new item to the unique set - set.remove(removed); // remove the item deleted by the set } - + + set.remove(removed); // remove the item deleted by the set + set.add(object); // add the new item to the unique set + return removed; // return the item deleted by the set } diff --git a/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java b/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java index 6f995d3da..1ae473c71 100644 --- a/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java @@ -613,7 +613,6 @@ public class SetUniqueListTest extends AbstractListTest { assertTrue(lset.contains(obj2)); } - @SuppressWarnings("serial") class SetUniqueList307 extends SetUniqueList { /** * Generated serial version ID.