[COLLECTIONS-524] ListOrderedSet.listOrderedSet(List) did not remove duplicates from the input list. Thanks to J Goodfellow.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1592910 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2014-05-06 22:12:50 +00:00
parent 320c0acf89
commit 527213decc
3 changed files with 30 additions and 5 deletions

View File

@ -22,6 +22,10 @@
<body>
<release version="4.1" date="TBD" description="">
<action issue="COLLECTIONS-524" dev="tn" type="fix" due-to="J Goodfellow">
"ListOrderedSet#listOrderedSet(List)" did not remove duplicates from the
input list as advertised in the javadoc.
</action>
<action issue="COLLECTIONS-521" dev="tn" type="fix" due-to="Maxime Nay">
"MultiKeyMap" was throwing a "NullPointerException" for various operations
if two key arguments have been used and the second was "null".

View File

@ -24,7 +24,9 @@ import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.OrderedIterator;
import org.apache.commons.collections4.functors.UniquePredicate;
import org.apache.commons.collections4.iterators.AbstractIteratorDecorator;
import org.apache.commons.collections4.list.UnmodifiableList;
@ -60,8 +62,7 @@ public class ListOrderedSet<E>
private final List<E> setOrder;
/**
* Factory method to create an ordered set specifying the list and set to
* use.
* Factory method to create an ordered set specifying the list and set to use.
* <p>
* The list and set must both be empty.
*
@ -102,8 +103,7 @@ public class ListOrderedSet<E>
}
/**
* Factory method to create an ordered set using the supplied list to retain
* order.
* Factory method to create an ordered set using the supplied list to retain order.
* <p>
* A <code>HashSet</code> is used for the set behaviour.
* <p>
@ -120,8 +120,8 @@ public class ListOrderedSet<E>
if (list == null) {
throw new IllegalArgumentException("List must not be null");
}
CollectionUtils.filter(list, UniquePredicate.uniquePredicate());
final Set<E> set = new HashSet<E>(list);
list.retainAll(set);
return new ListOrderedSet<E>(set, list);
}

View File

@ -23,6 +23,8 @@ import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections4.IteratorUtils;
/**
* Extension of {@link AbstractSetTest} for exercising the
* {@link ListOrderedSet} implementation.
@ -227,6 +229,25 @@ public class ListOrderedSetTest<E>
assertTrue(stop - start < 5000);
}
@SuppressWarnings("unchecked")
public void testDuplicates() {
final List<E> list = new ArrayList<E>(10);
list.add((E) Integer.valueOf(1));
list.add((E) Integer.valueOf(2));
list.add((E) Integer.valueOf(3));
list.add((E) Integer.valueOf(1));
final ListOrderedSet<E> orderedSet = ListOrderedSet.listOrderedSet(list);
assertEquals(3, orderedSet.size());
assertEquals(3, IteratorUtils.toArray(orderedSet.iterator()).length);
// insertion order preserved?
assertEquals(Integer.valueOf(1), orderedSet.get(0));
assertEquals(Integer.valueOf(2), orderedSet.get(1));
assertEquals(Integer.valueOf(3), orderedSet.get(2));
}
static class A {
@Override