changeset 7826:cb2c69a86f0f

make sort consistent with other sort methods with null comparators Reviewed-by: psandoz
author akhil
date Fri, 05 Apr 2013 10:50:31 -0700
parents adc363b47e78
children c537f01643b4
files src/share/classes/java/util/ArrayList.java src/share/classes/java/util/List.java src/share/classes/java/util/Vector.java src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java test/java/util/CollectionExtensionMethods/ListExtensionMethodsTest.java
diffstat 5 files changed, 11 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/ArrayList.java	Fri Apr 05 12:09:30 2013 -0400
+++ b/src/share/classes/java/util/ArrayList.java	Fri Apr 05 10:50:31 2013 -0700
@@ -1239,7 +1239,6 @@
     @Override
     @SuppressWarnings("unchecked")
     public void sort(Comparator<? super E> c) {
-        Objects.requireNonNull(c);
         final int expectedModCount = modCount;
         Arrays.sort((E[]) elementData, 0, size, c);
         if (modCount != expectedModCount) {
--- a/src/share/classes/java/util/List.java	Fri Apr 05 12:09:30 2013 -0400
+++ b/src/share/classes/java/util/List.java	Fri Apr 05 10:50:31 2013 -0700
@@ -417,9 +417,10 @@
      * The default implementation is equivalent to, for this {@code list}:
      * <pre>Collections.sort(list, c)</pre>
      *
-     * @param c the {@code Comparator} used to compare list elements
+     * @param c the {@code Comparator} used to compare list elements.
+     *          A {@code null} value indicates that the elements'
+     *          {@linkplain Comparable natural ordering} should be used.
      * @since 1.8
-     * @throws NullPointerException if the specified comparator is null
      * @throws ClassCastException if the list contains elements that are not
      *         <i>mutually comparable</i> using the specified comparator.
      * @throws UnsupportedOperationException if the list's list-iterator does
@@ -428,7 +429,6 @@
      *         found to violate the {@link Comparator} contract
      */
     default void sort(Comparator<? super E> c) {
-        Objects.requireNonNull(c);
         Collections.sort(this, c);
     }
 
--- a/src/share/classes/java/util/Vector.java	Fri Apr 05 12:09:30 2013 -0400
+++ b/src/share/classes/java/util/Vector.java	Fri Apr 05 10:50:31 2013 -0700
@@ -1315,7 +1315,6 @@
     @Override
     @SuppressWarnings("unchecked")
     public synchronized void sort(Comparator<? super E> c) {
-        Objects.requireNonNull(c);
         final int expectedModCount = modCount;
         Arrays.sort((E[]) elementData, 0, elementCount, c);
         if (modCount != expectedModCount) {
--- a/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Fri Apr 05 12:09:30 2013 -0400
+++ b/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Fri Apr 05 10:50:31 2013 -0700
@@ -1445,7 +1445,6 @@
 
     @Override
     public void sort(Comparator<? super E> c) {
-        Objects.requireNonNull(c);
         final ReentrantLock lock = this.lock;
         lock.lock();
         try {
--- a/test/java/util/CollectionExtensionMethods/ListExtensionMethodsTest.java	Fri Apr 05 12:09:30 2013 -0400
+++ b/test/java/util/CollectionExtensionMethods/ListExtensionMethodsTest.java	Fri Apr 05 10:50:31 2013 -0700
@@ -337,6 +337,14 @@
             CollectionAsserts.assertContents(list, original);
 
             CollectionSupplier.shuffle(list);
+            list.sort(null);
+            CollectionAsserts.assertSorted(list, Comparators.<Integer>naturalOrder());
+            if (test.name.startsWith("reverse")) {
+                Collections.reverse(list);
+            }
+            CollectionAsserts.assertContents(list, original);
+
+            CollectionSupplier.shuffle(list);
             list.sort(Comparators.<Integer>naturalOrder());
             CollectionAsserts.assertSorted(list, Comparators.<Integer>naturalOrder());
             if (test.name.startsWith("reverse")) {