This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Collections: reverse order comparator


Collections.reverseComparator() was returning a new comparator
instance each time, which is kind of unneccessary since the class has
no state. This is more correct too, since equals() wouldn't have been
useful previously, and the anonymous class could not implement
serializable as required by the spec.

regards

  [ bryce ]


2001-02-15  Bryce McKinlay  <bryce@albatross.co.nz>

	* java/util/Collections.java (ReverseComparator): New static class.
	(reverseOrder): Return static instance of ReverseComparator.

Index: Collections.java
===================================================================
RCS file: /cvs/gcc/egcs/libjava/java/util/Collections.java,v
retrieving revision 1.3
diff -u -r1.3 Collections.java
--- Collections.java	2000/12/11 03:47:47	1.3
+++ Collections.java	2001/02/15 03:56:23
@@ -447,7 +447,6 @@
     // Create a minimal implementation of List
     return new AbstractList()
     {
-
       public int size()
       {
 	return n;
@@ -487,22 +486,25 @@
       }
   }
 
+  static class ReverseComparator implements Comparator, Serializable
+  {
+    public int compare(Object a, Object b)
+    {
+      return -((Comparable) a).compareTo(b);
+    }
+  }
+  
+  static ReverseComparator rcInstance = new ReverseComparator();
+  
   /**
    * Get a comparator that implements the reverse of natural ordering. This is
    * intended to make it easy to sort into reverse order, by simply passing
    * Collections.reverseOrder() to the sort method. The return value of this
    * method is Serializable.
    */
-  // The return value isn't Serializable, because the spec is broken.
   public static Comparator reverseOrder()
   {
-    return new Comparator()
-    {
-      public int compare(Object a, Object b)
-      {
-	return -((Comparable) a).compareTo(b);
-      }
-    };
+    return rcInstance;
   }
 
   /**

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]