You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2014/05/07 00:12:51 UTC
svn commit: r1592910 - in /commons/proper/collections/trunk/src:
changes/changes.xml
main/java/org/apache/commons/collections4/set/ListOrderedSet.java
test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java
Author: tn
Date: Tue May 6 22:12:50 2014
New Revision: 1592910
URL: http://svn.apache.org/r1592910
Log:
[COLLECTIONS-524] ListOrderedSet.listOrderedSet(List) did not remove duplicates from the input list. Thanks to J Goodfellow.
Modified:
commons/proper/collections/trunk/src/changes/changes.xml
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/set/ListOrderedSet.java
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java
Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1592910&r1=1592909&r2=1592910&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Tue May 6 22:12:50 2014
@@ -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".
Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/set/ListOrderedSet.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/set/ListOrderedSet.java?rev=1592910&r1=1592909&r2=1592910&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/set/ListOrderedSet.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/set/ListOrderedSet.java Tue May 6 22:12:50 2014
@@ -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);
}
Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java?rev=1592910&r1=1592909&r2=1592910&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java Tue May 6 22:12:50 2014
@@ -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