< prev index next >

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

Print this page




  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             }
  86         }
  87 
  88         @Override


  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         @SuppressWarnings("serial") // Not statically typed as Serializable
  70         private final Comparator<T> real;
  71 
  72         @SuppressWarnings("unchecked")
  73         NullComparator(boolean nullFirst, Comparator<? super T> real) {
  74             this.nullFirst = nullFirst;
  75             this.real = (Comparator<T>) real;
  76         }
  77 
  78         @Override
  79         public int compare(T a, T b) {
  80             if (a == null) {
  81                 return (b == null) ? 0 : (nullFirst ? -1 : 1);
  82             } else if (b == null) {
  83                 return nullFirst ? 1: -1;
  84             } else {
  85                 return (real == null) ? 0 : real.compare(a, b);
  86             }
  87         }
  88 
  89         @Override
< prev index next >