< prev index next >

src/java.base/share/classes/java/util/Comparators.java

Print this page

        

*** 20,62 **** * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ ! package java.util; import java.io.Serializable; ! import java.util.function.BinaryOperator; ! import java.util.function.Function; ! import java.util.function.ToDoubleFunction; ! import java.util.function.ToIntFunction; ! import java.util.function.ToLongFunction; /** * Package private supporting class for {@link Comparator}. */ class Comparators { private Comparators() { throw new AssertionError("no instances"); } /** ! * Compares {@link Comparable} objects in natural order. * * @see Comparable */ ! enum NaturalOrderComparator implements Comparator<Comparable<Object>> { ! INSTANCE; @Override ! public int compare(Comparable<Object> c1, Comparable<Object> c2) { ! return c1.compareTo(c2); } @Override ! public Comparator<Comparable<Object>> reversed() { ! return Comparator.reverseOrder(); } } /** * Null-friendly comparators --- 20,109 ---- * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ ! package javany.util; import java.io.Serializable; ! import java.util.HashMap; ! import java.util.Map; ! import java.util.Objects; /** * Package private supporting class for {@link Comparator}. */ class Comparators { private Comparators() { throw new AssertionError("no instances"); } + // natural order comparisons (package-private - exposed via Comparator.naturalOrder()) + + @SuppressWarnings("unchecked") + static <any T> Comparator<T> naturalOrder() { + return (Comparator<T>) comparators.get(Comparator<T>.class); + } + + private static final Map<Class<?>, Object> comparators = new HashMap<>(); + + @SuppressWarnings("unchecked") + private static <any T> void putComparator(Comparator<T> comparator) { + comparators.put(Comparator<T>.class, comparator); + } + + static { + putComparator((Comparator<boolean>) Boolean::compare); + putComparator((Comparator<byte>) Byte::compare); + putComparator((Comparator<short>) Short::compare); + putComparator((Comparator<char>) Character::compare); + putComparator((Comparator<int>) Integer::compare); + putComparator((Comparator<long>) Long::compare); + putComparator((Comparator<float>) Float::compare); + putComparator((Comparator<double>) Double::compare); + putComparator(new Comparator<Object>() { + @Override + public int compare(Object a, Object b) { + if (a instanceof Comparable && b instanceof Comparable) { + @SuppressWarnings("unchecked") + Comparable<Object> c = (Comparable<Object>)a; + try { + return c.compareTo(b); + } catch (Exception e) { + throw new IllegalArgumentException( + "Can't compare mutually non-Comparable objects", e); + } + } else { + throw new IllegalArgumentException( + "Can't compare non-Comparable objects"); + } + } + }); + } + /** ! * Reverses the order of given Comparator. * * @see Comparable */ ! static class ReverseComparator<any T> implements Comparator<T> { ! ! private final Comparator<T> comparator; ! ! ReverseComparator(Comparator<T> comparator) { ! this.comparator = comparator; ! } @Override ! public int compare(T c1, T c2) { ! ! return comparator.compare(c2, c1); } @Override ! public Comparator<T> reversed() { ! return comparator; } } /** * Null-friendly comparators
< prev index next >