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 2015/06/04 22:36:25 UTC

svn commit: r1683631 - in /commons/proper/collections/trunk/src: changes/changes.xml main/java/org/apache/commons/collections4/IteratorUtils.java test/java/org/apache/commons/collections4/IteratorUtilsTest.java

Author: tn
Date: Thu Jun  4 20:36:25 2015
New Revision: 1683631

URL: http://svn.apache.org/r1683631
Log:
[COLLECTIONS-566] Use natural ordering in IteratorUtils#collate when the provided comparator is null.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.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=1683631&r1=1683630&r2=1683631&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Thu Jun  4 20:36:25 2015
@@ -22,6 +22,10 @@
   <body>
 
   <release version="4.1" date="TBD" description="">
+    <action issue="COLLECTIONS-566" dev="tn" type="fix">
+      "IteratorUtils#collate(...)" methods did not use natural ordering when a
+      null comparator was provided.
+    </action>
     <action issue="COLLECTIONS-557" dev="tn" type="add" due-to="Philippe Mouawad">
       Added support to specify the initial size of a "LRUMap".
     </action>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java?rev=1683631&r1=1683630&r2=1683631&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/IteratorUtils.java Thu Jun  4 20:36:25 2015
@@ -602,7 +602,9 @@ public class IteratorUtils {
     public static <E> Iterator<E> collatedIterator(final Comparator<? super E> comparator,
                                                    final Iterator<? extends E> iterator1,
                                                    final Iterator<? extends E> iterator2) {
-        return new CollatingIterator<E>(comparator, iterator1, iterator2);
+        @SuppressWarnings("unchecked")
+        final Comparator<E> comp = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
+        return new CollatingIterator<E>(comp, iterator1, iterator2);
     }
 
     /**
@@ -623,7 +625,9 @@ public class IteratorUtils {
      */
     public static <E> Iterator<E> collatedIterator(final Comparator<? super E> comparator,
                                                    final Iterator<? extends E>... iterators) {
-        return new CollatingIterator<E>(comparator, iterators);
+        @SuppressWarnings("unchecked")
+        final Comparator<E> comp = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
+        return new CollatingIterator<E>(comp, iterators);
     }
 
     /**
@@ -644,8 +648,10 @@ public class IteratorUtils {
      * @throws ClassCastException if the iterators collection contains the wrong object type
      */
     public static <E> Iterator<E> collatedIterator(final Comparator<? super E> comparator,
-            final Collection<Iterator<? extends E>> iterators) {
-        return new CollatingIterator<E>(comparator, iterators);
+                                                   final Collection<Iterator<? extends E>> iterators) {
+        @SuppressWarnings("unchecked")
+        final Comparator<E> comp = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator;
+        return new CollatingIterator<E>(comp, iterators);
     }
 
     // Object Graph

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java?rev=1683631&r1=1683630&r2=1683631&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java Thu Jun  4 20:36:25 2015
@@ -23,6 +23,8 @@ import static org.junit.Assert.*;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
@@ -50,8 +52,20 @@ public class IteratorUtilsTest {
      */
     private List<Integer> collectionA = null;
 
+    /**
+     * Collection of even {@link Integer}s
+     */
+    private List<Integer> collectionEven = null;
+
+    /**
+     * Collection of odd {@link Integer}s
+     */
+    private List<Integer> collectionOdd = null;
+
     private Iterable<Integer> iterableA = null;
 
+    private Collection<Integer> emptyCollection = new ArrayList<Integer>(1);
+
     @Before
     public void setUp() {
         collectionA = new ArrayList<Integer>();
@@ -67,6 +81,9 @@ public class IteratorUtilsTest {
         collectionA.add(4);
 
         iterableA = collectionA;
+
+        collectionEven = Arrays.asList(2, 4, 6, 8, 10, 12);
+        collectionOdd = Arrays.asList(1, 3, 5, 7, 9, 11);
     }
 
     @Test
@@ -900,6 +917,57 @@ public class IteratorUtilsTest {
         };
     }
 
+    /**
+     * Tests methods collatedIterator(...)
+     */
+    @Test
+    public void testCollatedIterator() {
+        try {
+            IteratorUtils.collatedIterator(null, collectionOdd.iterator(), null);
+            fail("expecting NullPointerException");
+        } catch (NullPointerException npe) {
+            // expected
+        }
+
+        try {
+            IteratorUtils.collatedIterator(null, null, collectionEven.iterator());
+            fail("expecting NullPointerException");
+        } catch (NullPointerException npe) {
+            // expected
+        }
+
+        // natural ordering
+        Iterator<Integer> it = 
+                IteratorUtils.collatedIterator(null, collectionOdd.iterator(), collectionEven.iterator());
+
+        List<Integer> result = IteratorUtils.toList(it);
+        assertEquals(12, result.size());
+
+        List<Integer> combinedList = new ArrayList<Integer>();
+        combinedList.addAll(collectionOdd);
+        combinedList.addAll(collectionEven);
+        Collections.sort(combinedList);
+
+        assertEquals(combinedList, result);
+
+        it = IteratorUtils.collatedIterator(null, collectionOdd.iterator(), emptyCollection.iterator());
+        result = IteratorUtils.toList(it);
+        assertEquals(collectionOdd, result);
+
+        final Comparator<Integer> reverseComparator =
+                ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator());
+
+        Collections.reverse((List<Integer>) collectionOdd);
+        Collections.reverse((List<Integer>) collectionEven);
+        Collections.reverse(combinedList);
+
+        it = IteratorUtils.collatedIterator(reverseComparator,
+                                            collectionOdd.iterator(),
+                                            collectionEven.iterator());
+        result = IteratorUtils.toList(it);
+        assertEquals(combinedList, result);
+    }
+
     // -----------------------------------------------------------------------
     @Test
     public void apply() {