classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] [generics] Patch: FYI: 2 new Collections methods


From: Tom Tromey
Subject: [cp-patches] [generics] Patch: FYI: 2 new Collections methods
Date: 13 Aug 2005 18:52:18 -0600

I'm checking this in on the generics branch.

This adds a couple new 1.5 methods to Collections.

Tom

Index: ChangeLog
from  Tom Tromey  <address@hidden>
        * java/util/Collections.java (reverseOrder): New method.
        (frequency): Likewise.
        (ReverseComparator): No longer final.

Index: java/util/Collections.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Collections.java,v
retrieving revision 1.28.2.13
diff -u -r1.28.2.13 Collections.java
--- java/util/Collections.java 2 Aug 2005 20:12:29 -0000 1.28.2.13
+++ java/util/Collections.java 14 Aug 2005 00:54:48 -0000
@@ -1151,6 +1151,33 @@
   }
 
   /**
+   * Get a comparator that implements the reverse of the ordering
+   * specified by the given Comparator. If the Comparator is null,
+   * this is equivalent to address@hidden #reverseOrder()}.  The return value
+   * of this method is Serializable, if the specified Comparator is
+   * either Serializable or null.
+   *
+   * @param c the comparator to invert
+   * @return a comparator that imposes reverse ordering
+   * @see Comparable
+   * @see Serializable
+   *
+   * @since 1.5
+   */
+  public static <T> Comparator<T> reverseOrder(final Comparator<T> c)
+  {
+    if (c == null)
+      return (Comparator<T>) rcInstance;
+    return new ReverseComparator<T> ()
+    {
+      public int compare(T a, T b)
+      {
+       return - c.compare(a, b);
+      }
+    };
+  }
+
+  /**
    * Get a comparator that implements the reverse of natural ordering. In
    * other words, this sorts Comparable objects opposite of how their
    * compareTo method would sort. This makes it easy to sort into reverse
@@ -1177,7 +1204,7 @@
    *
    * @author Eric Blake (address@hidden)
    */
-  private static final class ReverseComparator<T>
+  private static class ReverseComparator<T>
     implements Comparator<T>, Serializable
   {
     /**
@@ -1375,6 +1402,17 @@
       }
   }
 
+  /** @since 1.5 */
+  public static int frequency (Collection<?> c, Object o)
+  {
+    int result = 0;
+    for (Object v : c)
+      {
+       if (AbstractCollection.equals(o, v))
+         ++result;
+      }
+    return result;
+  }
 
   /**
    * Obtain an immutable Set consisting of a single element. The return value




reply via email to

[Prev in Thread] Current Thread [Next in Thread]