From be67139289c7167ed393537543b29041f88ca086 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Sun, 8 Nov 2015 21:35:53 +0000 Subject: [PATCH] Backport COLLECTIONS-444 to 3.2.2. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/branches/COLLECTIONS_3_2_X@1713299 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 8 ++++---- .../commons/collections/list/SetUniqueList.java | 10 +++++----- .../collections/list/TestSetUniqueList.java | 17 ++++++++++++++++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1445d7381..88e572494 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -29,6 +29,10 @@ permission to read system properties, the "File#separator" field will be used instead. + + SetUniqueList.set(int, Object) now works correctly if the object to be inserted + is already placed at the given position. + Removed debug output in "MapUtils#getNumber(Map)". @@ -78,10 +82,6 @@ Tree traversal with a TreeListIterator will not be affected anymore by the removal of an element directly after a call to previous(). - - SetUniqueList.set(int, E) now works correctly if the object to be inserted - is already placed at the given position. - "ListUtils#intersection(List, List)" will now also work correctly if there are duplicate elements in the provided lists. diff --git a/src/java/org/apache/commons/collections/list/SetUniqueList.java b/src/java/org/apache/commons/collections/list/SetUniqueList.java index 13a870e8c..27ad6eed7 100644 --- a/src/java/org/apache/commons/collections/list/SetUniqueList.java +++ b/src/java/org/apache/commons/collections/list/SetUniqueList.java @@ -216,17 +216,17 @@ 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) { // the object is already in the unique list // (and it hasn't been swapped with itself) - super.remove(pos); // remove the duplicate by index + 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 + 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 + return removed; // return the item deleted by the set } public boolean remove(Object object) { diff --git a/src/test/org/apache/commons/collections/list/TestSetUniqueList.java b/src/test/org/apache/commons/collections/list/TestSetUniqueList.java index 086027c00..b8bfdcce9 100644 --- a/src/test/org/apache/commons/collections/list/TestSetUniqueList.java +++ b/src/test/org/apache/commons/collections/list/TestSetUniqueList.java @@ -486,7 +486,22 @@ public class TestSetUniqueList extends AbstractTestList { decoratedList.add(1, s2); assertEquals(4, decoratedList.size()); } - + + 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)); + } + //----------------------------------------------------------------------- public String getCompatibilityVersion() { return "3.1";