Applying Bjorn Townsend's unit test and my fix for COLLECTIONS-304 - fixing SetUniqueList so the set method doesn't let the uniqueness get out of sync

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@711591 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2008-11-05 15:47:43 +00:00
parent 0725e476d4
commit 0122245f02
2 changed files with 35 additions and 6 deletions

View File

@ -220,14 +220,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 uniq 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
}

View File

@ -443,4 +443,31 @@ public class TestSetUniqueList extends AbstractTestList {
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SetUniqueList.fullCollection.version3.1.obj");
// }
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());
}
}