< prev index next >

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

Print this page




  45      * @see Comparable
  46      */
  47     enum NaturalOrderComparator implements Comparator<Comparable<Object>> {
  48         INSTANCE;
  49 
  50         @Override
  51         public int compare(Comparable<Object> c1, Comparable<Object> c2) {
  52             return c1.compareTo(c2);
  53         }
  54 
  55         @Override
  56         public Comparator<Comparable<Object>> reversed() {
  57             return Comparator.reverseOrder();
  58         }
  59     }
  60 
  61     /**
  62      * Null-friendly comparators
  63      */
  64     static final class NullComparator<T> implements Comparator<T>, Serializable {

  65         private static final long serialVersionUID = -7569533591570686392L;
  66         private final boolean nullFirst;
  67         // if null, non-null Ts are considered equal
  68         private final Comparator<T> real;
  69 
  70         @SuppressWarnings("unchecked")
  71         NullComparator(boolean nullFirst, Comparator<? super T> real) {
  72             this.nullFirst = nullFirst;
  73             this.real = (Comparator<T>) real;
  74         }
  75 
  76         @Override
  77         public int compare(T a, T b) {
  78             if (a == null) {
  79                 return (b == null) ? 0 : (nullFirst ? -1 : 1);
  80             } else if (b == null) {
  81                 return nullFirst ? 1: -1;
  82             } else {
  83                 return (real == null) ? 0 : real.compare(a, b);
  84             }


  45      * @see Comparable
  46      */
  47     enum NaturalOrderComparator implements Comparator<Comparable<Object>> {
  48         INSTANCE;
  49 
  50         @Override
  51         public int compare(Comparable<Object> c1, Comparable<Object> c2) {
  52             return c1.compareTo(c2);
  53         }
  54 
  55         @Override
  56         public Comparator<Comparable<Object>> reversed() {
  57             return Comparator.reverseOrder();
  58         }
  59     }
  60 
  61     /**
  62      * Null-friendly comparators
  63      */
  64     static final class NullComparator<T> implements Comparator<T>, Serializable {
  65         @java.io.Serial
  66         private static final long serialVersionUID = -7569533591570686392L;
  67         private final boolean nullFirst;
  68         // if null, non-null Ts are considered equal
  69         private final Comparator<T> real;
  70 
  71         @SuppressWarnings("unchecked")
  72         NullComparator(boolean nullFirst, Comparator<? super T> real) {
  73             this.nullFirst = nullFirst;
  74             this.real = (Comparator<T>) real;
  75         }
  76 
  77         @Override
  78         public int compare(T a, T b) {
  79             if (a == null) {
  80                 return (b == null) ? 0 : (nullFirst ? -1 : 1);
  81             } else if (b == null) {
  82                 return nullFirst ? 1: -1;
  83             } else {
  84                 return (real == null) ? 0 : real.compare(a, b);
  85             }
< prev index next >