changeset 8885:0ca783c9c628

NullComparator supports all non-null values to be equal
author henryjen
date Thu, 20 Jun 2013 12:34:36 -0700
parents a051767ef42b
children ba022e75320a
files src/share/classes/java/util/Comparator.java src/share/classes/java/util/Comparators.java
diffstat 2 files changed, 13 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Comparator.java	Thu Jun 20 13:49:58 2013 -0400
+++ b/src/share/classes/java/util/Comparator.java	Thu Jun 20 12:34:36 2013 -0700
@@ -341,7 +341,8 @@
     }
 
     /**
-     * Returns a comparator compares {@link Comparable} type in natural order.
+     * Returns a comparator that compares {@link Comparable} type in natural
+     * order.
      *
      * <p>The returned comparator is serializable and throws {@link
      * NullPointerException} when comparing {@code null}.
--- a/src/share/classes/java/util/Comparators.java	Thu Jun 20 13:49:58 2013 -0400
+++ b/src/share/classes/java/util/Comparators.java	Thu Jun 20 12:34:36 2013 -0700
@@ -56,37 +56,38 @@
     /**
      * Null-friendly comparators
      */
-    static class NullComparator<T> implements Comparator<T>, Serializable {
+    final static class NullComparator<T> implements Comparator<T>, Serializable {
         private static final long serialVersionUID = -7569533591570686392L;
-        private final boolean nullsFirst;
+        private final boolean nullFirst;
+        // if null, non-null Ts are considered equal
         private final Comparator<T> real;
 
         @SuppressWarnings("unchecked")
-        NullComparator(boolean nullsFirst, Comparator<? super T> real) {
-            this.nullsFirst = nullsFirst;
-            this.real = (Comparator<T>) Objects.requireNonNull(real);
+        NullComparator(boolean nullFirst, Comparator<? super T> real) {
+            this.nullFirst = nullFirst;
+            this.real = (Comparator<T>) real;
         }
 
         @Override
         public int compare(T a, T b) {
             if (a == null) {
-                return (b == null) ? 0 : (nullsFirst ? -1 : 1);
+                return (b == null) ? 0 : (nullFirst ? -1 : 1);
             } else if (b == null) {
-                return nullsFirst ? 1: -1;
+                return nullFirst ? 1: -1;
             } else {
-                return real.compare(a, b);
+                return (real == null) ? 0 : real.compare(a, b);
             }
         }
 
         @Override
         public Comparator<T> thenComparing(Comparator<? super T> other) {
             Objects.requireNonNull(other);
-            return new NullComparator(nullsFirst, real.thenComparing(other));
+            return new NullComparator(nullFirst, real == null ? null : real.thenComparing(other));
         }
 
         @Override
         public Comparator<T> reversed() {
-            return new NullComparator(!nullsFirst, real.reversed());
+            return new NullComparator(!nullFirst, real == null ? null : real.reversed());
         }
     }
 }