Author: tn
Date: Sun Nov  8 21:27:35 2015
New Revision: 1713298

URL: http://svn.apache.org/viewvc?rev=1713298&view=rev
Log:
Backport COLLECTIONS-304 to 3.2.2.

Modified:
    
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/changes/changes.xml
    
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/java/org/apache/commons/collections/list/SetUniqueList.java
    
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/test/org/apache/commons/collections/list/TestSetUniqueList.java

Modified: 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/branches/COLLECTIONS_3_2_X/src/changes/changes.xml?rev=1713298&r1=1713297&r2=1713298&view=diff
==============================================================================
--- 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/changes/changes.xml 
(original)
+++ 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/changes/changes.xml 
Sun Nov  8 21:27:35 2015
@@ -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>

Modified: 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/java/org/apache/commons/collections/list/SetUniqueList.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/branches/COLLECTIONS_3_2_X/src/java/org/apache/commons/collections/list/SetUniqueList.java?rev=1713298&r1=1713297&r2=1713298&view=diff
==============================================================================
--- 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/java/org/apache/commons/collections/list/SetUniqueList.java
 (original)
+++ 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/java/org/apache/commons/collections/list/SetUniqueList.java
 Sun Nov  8 21:27:35 2015
@@ -216,14 +216,16 @@ public class SetUniqueList extends Abstr
     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
     }
 

Modified: 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/branches/COLLECTIONS_3_2_X/src/test/org/apache/commons/collections/list/TestSetUniqueList.java?rev=1713298&r1=1713297&r2=1713298&view=diff
==============================================================================
--- 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
 (original)
+++ 
commons/proper/collections/branches/COLLECTIONS_3_2_X/src/test/org/apache/commons/collections/list/TestSetUniqueList.java
 Sun Nov  8 21:27:35 2015
@@ -468,6 +468,25 @@ public class TestSetUniqueList extends A
         }
     }
 
+    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";


Reply via email to