[COLLECTIONS-566] Use natural ordering in IteratorUtils#collate when the provided comparator is null.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1683631 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2015-06-04 20:36:25 +00:00
parent 7c99c6234c
commit 73d69dfe86
3 changed files with 82 additions and 4 deletions

View File

@ -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>

View File

@ -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);
}
/**
@ -645,7 +649,9 @@ public class IteratorUtils {
*/
public static <E> Iterator<E> collatedIterator(final Comparator<? super E> comparator,
final Collection<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);
}
// Object Graph

View File

@ -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() {