From bb9952dbedc77f25fffda32d96e6494e2246bab3 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 25 Feb 2013 21:47:48 +0000 Subject: [PATCH] [COLLECTIONS-444] SetUniqueList.set now works correctly when the object to be inserted is already at the same place. Thanks to Thomas Vahrst. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1449914 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 4 ++++ .../collections/list/SetUniqueList.java | 9 +++++---- .../collections/list/SetUniqueListTest.java | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 229405ba7..3fe96ce75 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,10 @@ + + SetUniqueList.set(int, E) now works correctly if the object to be inserted + is already placed at the given position. + MultiKeyMap.clone() now correctly calls super.clone(). diff --git a/src/main/java/org/apache/commons/collections/list/SetUniqueList.java b/src/main/java/org/apache/commons/collections/list/SetUniqueList.java index f217c2c09..4443a3f8c 100644 --- a/src/main/java/org/apache/commons/collections/list/SetUniqueList.java +++ b/src/main/java/org/apache/commons/collections/list/SetUniqueList.java @@ -232,11 +232,12 @@ public class SetUniqueList extends AbstractSerializableListDecorator { // 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.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.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 } diff --git a/src/test/java/org/apache/commons/collections/list/SetUniqueListTest.java b/src/test/java/org/apache/commons/collections/list/SetUniqueListTest.java index 551d4e44e..689edae42 100644 --- a/src/test/java/org/apache/commons/collections/list/SetUniqueListTest.java +++ b/src/test/java/org/apache/commons/collections/list/SetUniqueListTest.java @@ -596,6 +596,24 @@ public class SetUniqueListTest extends AbstractListTest { assertTrue(stop - start < 5000); } + public void testSetCollections444() { + final SetUniqueList lset = new SetUniqueList(new ArrayList(), new HashSet()); + + // Duplicate element + final Integer obj1 = new Integer(1); + final Integer obj2 = new Integer(2); + + lset.add(obj1); + lset.add(obj2); + lset.set(0, obj1); + assertEquals(2, lset.size()); + assertSame(obj1, lset.get(0)); + assertSame(obj2, lset.get(1)); + + assertTrue(lset.contains(obj1)); + assertTrue(lset.contains(obj2)); + } + @SuppressWarnings("serial") class SetUniqueList307 extends SetUniqueList { public SetUniqueList307(final List list, final Set set) {