Backport COLLECTIONS-304 to 3.2.2. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/branches/COLLECTIONS_3_2_X@1713298 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/7f947f93 Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/7f947f93 Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/7f947f93 Branch: refs/heads/COLLECTIONS_3_2_X Commit: 7f947f938d92eced2988d971e38810e74608467d Parents: 81af3aa Author: Thomas Neidhart <t...@apache.org> Authored: Sun Nov 8 21:27:35 2015 +0000 Committer: Thomas Neidhart <t...@apache.org> Committed: Sun Nov 8 21:27:35 2015 +0000 ---------------------------------------------------------------------- src/changes/changes.xml | 6 +++--- .../commons/collections/list/SetUniqueList.java | 14 ++++++++------ .../collections/list/TestSetUniqueList.java | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-collections/blob/7f947f93/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index a415a91..1445d73 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -42,6 +42,9 @@ "SetUniqueList#subList()#contains(Object)" will now correctly check the subList rather than the parent list. </action> + <action issue="COLLECTIONS-304" dev="bayard" type="fix" due-to="RafaÅ Figas,Bjorn Townsend"> + "SetUniqueList#set(int, Object)" will now correctly enforce the uniqueness constraint. + </action> <action issue="COLLECTIONS-294" dev="bayard" type="fix" due-to="Benjamin Bentmann"> "CaseInsensitiveMap" will now convert input strings to lower-case in a locale-independent manner. @@ -86,9 +89,6 @@ <action issue="COLLECTIONS-330" dev="mbenson" type="fix" due-to="Joerg Schaible"> "LRUMap#keySet()#remove(Object)" will not throw a "ConcurrentModificationException" anymore. </action> - <action issue="COLLECTIONS-304" dev="bayard" type="fix" due-to="RafaÅ Figas,Bjorn Townsend"> - "SetUniqueList#set(int, Object)" will now correctly enforce the uniqueness constraint. - </action> --> </release> </body> http://git-wip-us.apache.org/repos/asf/commons-collections/blob/7f947f93/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 c9f0148..13a870e 100644 --- a/src/java/org/apache/commons/collections/list/SetUniqueList.java +++ b/src/java/org/apache/commons/collections/list/SetUniqueList.java @@ -216,14 +216,16 @@ public class SetUniqueList extends AbstractSerializableListDecorator { public Object set(int index, Object object) { int pos = indexOf(object); Object removed = super.set(index, object); - if (pos == -1 || pos == index) { - return removed; + + if (pos != -1 && pos != index) { + // the object is already in the unique list + // (and it hasn't been swapped with itself) + super.remove(pos); // remove the duplicate by index } - - // the object is already in the uniq list - // (and it hasn't been swapped with itself) - super.remove(pos); // remove the duplicate by index + + set.add(object); // add the new item to the unique set set.remove(removed); // remove the item deleted by the set + return removed; // return the item deleted by the set } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/7f947f93/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 519cd59..086027c 100644 --- a/src/test/org/apache/commons/collections/list/TestSetUniqueList.java +++ b/src/test/org/apache/commons/collections/list/TestSetUniqueList.java @@ -468,6 +468,25 @@ public class TestSetUniqueList extends AbstractTestList { } } + public void testCollections304() { + List list = new LinkedList(); + SetUniqueList decoratedList = SetUniqueList.decorate(list); + String s1 = "Apple"; + String s2 = "Lemon"; + String s3 = "Orange"; + String s4 = "Strawberry"; + decoratedList.add(s1); + decoratedList.add(s2); + decoratedList.add(s3); + assertEquals(3, decoratedList.size()); + decoratedList.set(1, s4); + assertEquals(3, decoratedList.size()); + decoratedList.add(1, s4); + assertEquals(3, decoratedList.size()); + decoratedList.add(1, s2); + assertEquals(4, decoratedList.size()); + } + //----------------------------------------------------------------------- public String getCompatibilityVersion() { return "3.1";