Hi,

All primitive type classes should implement the Comparable interface.
The following patch adds this for the classes that did not yet implement
it. It also adds compareTo and friends to String.

2001-01-07  Mark Wielaard  <[EMAIL PROTECTED]>
    * java/lang/Float.java: implements Comparable, new method compareTo()
    * java/lang/Integer.java: Ditto
    * java/lang/Long.java: Ditto
    * java/lang/Short.java: Ditto
    * java/lang/String.java: Ditto
    (CASE_INSENSITIVE_ORDER): new field
    (compareToIgnoreCase): new method

I am checking this in.

Cheers,

Mark
Index: java/lang/Float.java
===================================================================
RCS file: /cvs/classpath/java/lang/Float.java,v
retrieving revision 1.13
diff -u -u -r1.13 Float.java
--- java/lang/Float.java        2000/03/16 23:31:24     1.13
+++ java/lang/Float.java        2001/01/07 22:26:30
@@ -1,5 +1,5 @@
 /* java.lang.Float
-   Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -37,7 +37,7 @@
  * @author Paul Fisher
  * @since JDK 1.0
  */
-public final class Float extends Number 
+public final class Float extends Number implements Comparable
 {
     /**
      * The minimum positive value a <code>float</code> may represent
@@ -387,4 +387,58 @@
      */
     public native static float parseFloat(String s)
     throws NumberFormatException, NullPointerException;
+
+    /**
+     * Returns 0 if the <code>float</code> value of the argument is 
+     * equal to the value of this <code>Float</code>.  Returns a number
+     * less than zero if the value of this <code>Float</code> is less 
+     * than the <code>Float</code> value of the argument, and returns a 
+     * number greater than zero if the value of this <code>Float</code> 
+     * is greater than the <code>float</code> value of the argument.
+     * <br>
+     * <code>Float.NaN</code> is greater than any number other than itself, 
+     * even <code>Float.POSITIVE_INFINITY</code>.
+     * <br>
+     * <code>0.0</code> is greater than <code>-0.0</code>.
+     *
+     * @param f the Float to compare to.
+     * @return  0 if the <code>Float</code>s are the same, &lt; 0 if this
+     *          <code>Float</code> is less than the <code>Float</code> in
+     *          in question, or &gt; 0 if it is greater.
+     *
+     * @since 1.2
+     */
+    public int compareTo(Float f)
+    {
+        float x = f.floatValue();
+
+        if (value == NaN)
+            return (x == NaN) ? 0 : 1;
+        if ((value == 0.0) && (x == -0.0))
+            return 1;
+        if ((value == -0.0) && (x == 0.0))
+            return -1;
+
+        return ((value - x) > 0) ? 1 : -1;
+    }
+    
+    /**
+     * Compares the specified <code>Object</code> to this <code>Float</code>
+     * if and only if the <code>Object</code> is an instanceof 
+     * <code>Float</code>.
+     * Otherwise it throws a <code>ClassCastException</code>
+     *
+     * @param o the Object to compare to.
+     * @return  0 if the <code>Float</code>s are the same, &lt; 0 if this
+     *          <code>Float</code> is less than the <code>Float</code> in
+     *          in question, or &gt; 0 if it is greater.
+     * @throws ClassCastException if the argument is not a <code>Float</code>
+     *
+     * @since 1.2
+     */
+    public int compareTo(Object o)
+    {
+        return compareTo((Float)o);
+    }
+
 }
Index: java/lang/Integer.java
===================================================================
RCS file: /cvs/classpath/java/lang/Integer.java,v
retrieving revision 1.15
diff -u -u -r1.15 Integer.java
--- java/lang/Integer.java      2000/03/16 23:31:28     1.15
+++ java/lang/Integer.java      2001/01/07 22:26:30
@@ -1,5 +1,5 @@
 /* java.lang.Integer
-   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -38,7 +38,7 @@
  * @author John Keiser
  * @since JDK 1.0
  */
-public final class Integer extends Number {
+public final class Integer extends Number implements Comparable {
 
   // compatible with JDK 1.0.2+
   static final long serialVersionUID = 1360826667806852920L;
@@ -456,4 +456,36 @@
   public double doubleValue() {
     return value;
   }
+
+    /**
+     * Compare two Integers numerically by comparing their
+     * <code>int</code> values.
+     * @return a positive value if this <code>Integer</code> is greater
+     * in value than the argument <code>Integer</code>; a negative value
+     * if this <code>Integer</code> is smaller in value than the argument
+     * <code>Integer</code>; and <code>0</code>, zero, if this
+     * <code>Integer</code> is equal in value to the argument
+     * <code>Integer</code>.  
+     *
+     * @since 1.2
+     */
+    public int compareTo(Integer i)
+    {
+        return (value - i.intValue());
+    }
+    
+    /**
+     * Behaves like <code>compareTo(java.lang.Integer)</code> unless the Object
+     * is not a <code>Integer</code>.  Then it throws a 
+     * <code>ClassCastException</code>.
+     * @exception ClassCastException if the argument is not a
+     * <code>Integer</code>.
+     *
+     * @since 1.2
+     */
+    public int compareTo(Object o)
+    {
+        return compareTo((Integer)o);
+    }
+
 }
Index: java/lang/Long.java
===================================================================
RCS file: /cvs/classpath/java/lang/Long.java,v
retrieving revision 1.8
diff -u -u -r1.8 Long.java
--- java/lang/Long.java 2000/03/16 23:31:30     1.8
+++ java/lang/Long.java 2001/01/07 22:26:31
@@ -1,5 +1,5 @@
 /* java.lang.Long
-   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -38,7 +38,7 @@
  * @author John Keiser
  * @since JDK 1.0
  */
-public final class Long extends Number {
+public final class Long extends Number implements Comparable {
 
   // compatible with JDK 1.0.2+
   static final long serialVersionUID = 4290774380558885855L;
@@ -442,4 +442,36 @@
   public double doubleValue() {
     return value;
   }
+
+    /**
+     * Compare two Longs numerically by comparing their
+     * <code>long</code> values.
+     * @return a positive value if this <code>Long</code> is greater
+     * in value than the argument <code>Long</code>; a negative value
+     * if this <code>Long</code> is smaller in value than the argument
+     * <code>Long</code>; and <code>0</code>, zero, if this
+     * <code>Long</code> is equal in value to the argument
+     * <code>Long</code>.  
+     *
+     * @since 1.2
+     */
+    public int compareTo(Long l)
+    {
+        return (int)(value - l.longValue());
+    }
+    
+    /**
+     * Behaves like <code>compareTo(java.lang.Long)</code> unless the Object
+     * is not a <code>Long</code>.  Then it throws a 
+     * <code>ClassCastException</code>.
+     * @exception ClassCastException if the argument is not a
+     * <code>Long</code>.
+     *
+     * @since 1.2
+     */
+    public int compareTo(Object o)
+    {
+        return compareTo((Long)o);
+    }
+
 }
Index: java/lang/Short.java
===================================================================
RCS file: /cvs/classpath/java/lang/Short.java,v
retrieving revision 1.7
diff -u -u -r1.7 Short.java
--- java/lang/Short.java        2000/03/16 23:31:38     1.7
+++ java/lang/Short.java        2001/01/07 22:26:31
@@ -1,5 +1,5 @@
 /* java.lang.Short
-   Copyright (C) 1998 Free Software Foundation, Inc.
+   Copyright (C) 1998, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -38,7 +38,7 @@
  * @author John Keiser
  * @since JDK 1.0
  */
-public final class Short extends Number {
+public final class Short extends Number implements Comparable {
 
   static final long serialVersionUID = 7515723908773894738L;
 
@@ -335,4 +335,36 @@
   public double doubleValue() {
     return value;
   }
+
+    /**
+     * Compare two Shorts numerically by comparing their
+     * <code>short</code> values.
+     * @return a positive value if this <code>Short</code> is greater
+     * in value than the argument <code>Short</code>; a negative value
+     * if this <code>Short</code> is smaller in value than the argument
+     * <code>Short</code>; and <code>0</code>, zero, if this
+     * <code>Short</code> is equal in value to the argument
+     * <code>Short</code>.  
+     *
+     * @since 1.2
+     */
+    public int compareTo(Short s)
+    {
+        return (value - s.shortValue());
+    }
+    
+    /**
+     * Behaves like <code>compareTo(java.lang.Short)</code> unless the Object
+     * is not a <code>Short</code>.  Then it throws a 
+     * <code>ClassCastException</code>.
+     * @exception ClassCastException if the argument is not a
+     * <code>Short</code>.
+     *
+     * @since 1.2
+     */
+    public int compareTo(Object o)
+    {
+        return compareTo((Short)o);
+    }
+
 }
Index: java/lang/String.java
===================================================================
RCS file: /cvs/classpath/java/lang/String.java,v
retrieving revision 1.29
diff -u -u -r1.29 String.java
--- java/lang/String.java       2000/07/28 17:22:03     1.29
+++ java/lang/String.java       2001/01/07 22:26:31
@@ -1,5 +1,5 @@
 /* java.lang.String
-   Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -27,6 +27,7 @@
 
 package java.lang;
 
+import java.util.Comparator;
 import java.util.Hashtable;
 import java.util.Locale;
 import gnu.java.io.EncodingManager;
@@ -39,7 +40,7 @@
  * @author Paul N. Fisher
  * @since JDK1.0
  */
-public final class String {
+public final class String implements Comparable {
   /**
    * Holds the references for each intern()'d String.
    * Once a String has been intern()'d it cannot be GC'd.
@@ -78,6 +79,18 @@
   private int cachedHashCode;
 
   /**
+   * A Comparator that uses <code>String.compareToIgnoreCase(String)</code>.
+   *
+   * @since 1.2
+   */
+  public static final Comparator CASE_INSENSITIVE_ORDER
+      = new Comparator() {
+              public int compare(Object o1, Object o2) {
+                  return ((String)o1).compareToIgnoreCase((String)o2);
+              }
+          };
+
+  /**
    * Creates an empty String (length 0)
    */
   public String() {
@@ -519,6 +532,43 @@
        return result;
     }
     return count-anotherString.count;
+  }
+
+  /**
+   * Behaves like <code>compareTo(java.lang.String)</code> unless the Object
+   * is not a <code>String</code>.  Then it throws a 
+   * <code>ClassCastException</code>.
+   * @exception ClassCastException if the argument is not a
+   * <code>String</code>.
+   *
+   * @since 1.2
+   */
+  public int compareTo(Object o)
+  {
+    return compareTo((String)o);
+  }
+
+  /**
+   * Compares this String and another String (case insensitive).
+   *
+   * @return returns an integer less than, equal to, or greater than
+   *   zero, if this String is found, respectively, to be less than,
+   *   to match, or be greater than the given String.
+   *
+   * @since 1.2
+   */
+  public int compareToIgnoreCase(String s)
+  {
+    int min = Math.min(count, s.count);
+    for (int i = 0; i < min; i++)
+    {
+      char c1 = Character.toLowerCase(Character.toUpperCase(value[i]));
+      char c2 = Character.toLowerCase(Character.toUpperCase(s.value[i]));
+      int result = c1 - c2;
+      if (result != 0) 
+        return result;
+    }
+    return count-s.count;
   }
 
   /**

Reply via email to