Author: bayard
Date: Sun Jul 18 07:43:15 2010
New Revision: 965173

URL: http://svn.apache.org/viewvc?rev=965173&view=rev
Log:
Applying Mark Shead's patch to COLLECTIONS-359. The intersection method was not 
handling duplicates correctly. 

Modified:
    
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
    
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java

Modified: 
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java?rev=965173&r1=965172&r2=965173&view=diff
==============================================================================
--- 
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
 (original)
+++ 
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
 Sun Jul 18 07:43:15 2010
@@ -70,10 +70,12 @@ public class ListUtils {
      */
     public static <E> List<E> intersection(final List<? extends E> list1, 
final List<? extends E> list2) {
         final List<E> result = new ArrayList<E>();
+        final ArrayList<E> copyOfList1 = new ArrayList<E>(list1);
 
         for (E e : list2) {
-            if (list1.contains(e)) {
+            if (copyOfList1.contains(e)) {
                 result.add(e);
+                copyOfList1.remove(e);
             }
         }
         return result;

Modified: 
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java?rev=965173&r1=965172&r2=965173&view=diff
==============================================================================
--- 
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java
 (original)
+++ 
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java
 Sun Jul 18 07:43:15 2010
@@ -110,6 +110,21 @@ public class TestListUtils extends BulkT
         assertEquals(fullList, ListUtils.intersection(fullList, fullList));
     }
 
+    /**
+     * Tests intersecting two lists in different orders.
+     */
+    public void testIntersectionOrderInsensitivity() {
+               List one = new ArrayList();
+               List two = new ArrayList();
+               one.add("a");
+               one.add("b");
+               two.add("a");
+               two.add("a");
+               two.add("b");
+               two.add("b");
+               
assertEquals(ListUtils.intersection(one,two),ListUtils.intersection(two, one));
+    }
+
     public void testPredicatedList() {
         Predicate<Object> predicate = new Predicate<Object>() {
             public boolean evaluate(Object o) {


Reply via email to