mirror of
https://github.com/apache/commons-collections.git
synced 2025-02-07 18:49:32 +00:00
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:
parent
0725e476d4
commit
0122245f02
@ -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
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user