changeset 8888:197698000f78

Bug fix and clarify javadoc for null-friendly comparator
author henryjen
date Thu, 20 Jun 2013 15:58:00 -0700
parents 205011975bf4
children 8c51cc1a84e6
files src/share/classes/java/util/Comparator.java src/share/classes/java/util/Comparators.java test/java/util/Comparator/BasicTest.java
diffstat 3 files changed, 32 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Comparator.java	Thu Jun 20 16:43:48 2013 -0400
+++ b/src/share/classes/java/util/Comparator.java	Thu Jun 20 15:58:00 2013 -0700
@@ -362,7 +362,8 @@
      * Returns a null-friendly comparator that considers {@code null} to be
      * less than non-null. When both are {@code null}, they are considered
      * equal. If both are non-null, the specified {@code Comparator} is used
-     * to determine the order.
+     * to determine the order. If the specified comparator is {@code null},
+     * then the returned comparator considers all non-null values to be equal.
      *
      * <p>The returned comparator is serializable if the specified comparator
      * is serializable.
@@ -380,7 +381,8 @@
      * Returns a null-friendly comparator that considers {@code null} to be
      * greater than non-null. When both are {@code null}, they are considered
      * equal. If both are non-null, the specified {@code Comparator} is used
-     * to determine the order.
+     * to determine the order. If the specified comparator is {@code null},
+     * then the returned comparator considers all non-null values to be equal.
      *
      * <p>The returned comparator is serializable if the specified comparator
      * is serializable.
--- a/src/share/classes/java/util/Comparators.java	Thu Jun 20 16:43:48 2013 -0400
+++ b/src/share/classes/java/util/Comparators.java	Thu Jun 20 15:58:00 2013 -0700
@@ -82,7 +82,7 @@
         @Override
         public Comparator<T> thenComparing(Comparator<? super T> other) {
             Objects.requireNonNull(other);
-            return new NullComparator(nullFirst, real == null ? null : real.thenComparing(other));
+            return new NullComparator(nullFirst, real == null ? other : real.thenComparing(other));
         }
 
         @Override
--- a/test/java/util/Comparator/BasicTest.java	Thu Jun 20 16:43:48 2013 -0400
+++ b/test/java/util/Comparator/BasicTest.java	Thu Jun 20 15:58:00 2013 -0700
@@ -248,6 +248,7 @@
         assertComparison(strcmp, null, "abc");
         assertComparison(strcmp, "ab", "abc");
         assertComparison(strcmp, "abc", "def");
+        assertEquals(0, strcmp.compare("abc", "ABC"));
 
         // Ensure reverse still handle null properly
         Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
@@ -255,6 +256,19 @@
         assertComparison(strcmp2, "abc", "ab");
         assertComparison(strcmp2, "def", "abc");
         assertComparison(strcmp2, "ABC", "abc");
+
+        // Considering non-null values to be equal
+        Comparator<String> blind = Comparator.nullsFirst(null);
+        assertComparison(blind, null, "abc");
+        assertEquals(0, blind.compare("abc", "def"));
+        // reverse still consider non-null values to be equal
+        strcmp = blind.reversed();
+        assertComparison(strcmp, "abc", null);
+        assertEquals(0, strcmp.compare("abc", "def"));
+        // chain with another comparator to compare non-nulls
+        strcmp = blind.thenComparing(Comparator.naturalOrder());
+        assertComparison(strcmp, null, "abc");
+        assertComparison(strcmp, "abc", "def");
     }
 
     public void testNullsLast() {
@@ -279,6 +293,19 @@
         assertComparison(strcmp2, "abc", "ab");
         assertComparison(strcmp2, "def", "abc");
         assertComparison(strcmp2, "ABC", "abc");
+
+        // Considering non-null values to be equal
+        Comparator<String> blind = Comparator.nullsLast(null);
+        assertComparison(blind, "abc", null);
+        assertEquals(0, blind.compare("abc", "def"));
+        // reverse still consider non-null values to be equal
+        strcmp = blind.reversed();
+        assertComparison(strcmp, null, "abc");
+        assertEquals(0, strcmp.compare("abc", "def"));
+        // chain with another comparator to compare non-nulls
+        strcmp = blind.thenComparing(Comparator.naturalOrder());
+        assertComparison(strcmp, "abc", null);
+        assertComparison(strcmp, "abc", "def");
     }
 
     public void testComposeComparator() {