8171826: Comparator.reverseOrder(c) mishandles singleton comparators

Reviewed-by: rriggs
This commit is contained in:
Paul Sandoz 2017-12-12 09:33:35 -08:00
parent f1212e26c3
commit 72e7a31529
2 changed files with 46 additions and 16 deletions

View file

@ -24,9 +24,10 @@
*/
package java.util;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@ -5164,14 +5165,19 @@ public class Collections {
* specified comparator.
* @since 1.5
*/
@SuppressWarnings("unchecked")
public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) {
if (cmp == null)
return reverseOrder();
if (cmp instanceof ReverseComparator2)
return ((ReverseComparator2<T>)cmp).cmp;
return new ReverseComparator2<>(cmp);
if (cmp == null) {
return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
} else if (cmp == ReverseComparator.REVERSE_ORDER) {
return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
} else if (cmp == Comparators.NaturalOrderComparator.INSTANCE) {
return (Comparator<T>) ReverseComparator.REVERSE_ORDER;
} else if (cmp instanceof ReverseComparator2) {
return ((ReverseComparator2<T>) cmp).cmp;
} else {
return new ReverseComparator2<>(cmp);
}
}
/**