Hi,

The following patch adds new 1.4 functionality to the Boolean, Double
and Float classes. It also makes Boolean a little bit more efficient
by always returning the predefined TRUE or FALSE instances when
possible.

2001-07-10  Mark Wielaard <[EMAIL PROTECTED]>
    * java/lang/Boolean.java (valueOf boolean): new 1.4 method
    (toString boolean): idem
    (valueOf String): return one of the predefined Boolean instances
    * java/lang/Double.java (compare double double): new 1.4 method
    (compareTo Double): call new method
    * java/lang/Float.java (compare float float): new 1.4 method
    (compareTo Float): call new method

Is there any reason the primitive classes have not been merged with
libgcj? Some of those classes, like Boolean, are pure java. The only
difference seems to be the way they define the TYPE instance variable.
We use VMClassLoader.getPrimitiveClass("boolean") and libgcj seems to
use some compiler magic to set this field. Maybe we could just add a
VMClassLoader class to libgcj that does the magic?

Cheers,

Mark
-- 
Stuff to read:
    <http://www.toad.com/gnu/whatswrong.html>
  What's Wrong with Copy Protection, by John Gilmore
Index: java/lang/Boolean.java
===================================================================
RCS file: /cvs/classpath/java/lang/Boolean.java,v
retrieving revision 1.14
diff -u -u -r1.14 Boolean.java
--- java/lang/Boolean.java      2000/03/16 23:31:19     1.14
+++ java/lang/Boolean.java      2001/07/09 23:07:36
@@ -1,5 +1,5 @@
 /* Boolean.java -- object wrapper for boolean
-   Copyright (C) 1998 Free Software Foundation, Inc.
+   Copyright (C) 1998, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -42,13 +42,17 @@
     
     /**
      * This field is a <code>Boolean</code> object representing the
-     * primitive value <code>true</code>.
+     * primitive value <code>true</code>. This instance is returned
+     * by the static <code>valueOf()</code> methods if they return
+     * a <code>Boolean</code> representing <code>true</code>.
      */
     public static final Boolean TRUE  = new Boolean(true);
     
     /**
      * This field is a <code>Boolean</code> object representing the 
-     * primitive value <code>false</code>.
+     * primitive value <code>false</code>. This instance is returned
+     * by the static <code>valueOf()</code> methods if they return
+     * a <code>Boolean</code> representing <code>false</code>.
      */
      public static final Boolean FALSE = new Boolean(false);
 
@@ -62,7 +66,9 @@
     
     /**
      * Create a <code>Boolean</code> object representing the value of the 
-     * argument <code>value</code>
+     * argument <code>value</code>. In general the use of the static
+     * method <code>valueof(boolean)</code> is more efficient since it will
+     * not create a new object.
      *
      * @param value the primitive value of this <code>Boolean</code>
      */    
@@ -74,7 +80,9 @@
      * Creates a <code>Boolean</code> object representing the primitive 
      * <code>true</code> if and only if <code>s</code> matches 
      * the string "true" ignoring case, otherwise the object will represent 
-     * the primitive <code>false</code>.
+     * the primitive <code>false</code>. In general the use of the static
+     * method <code>valueof(String)</code> is more efficient since it will
+     * not create a new object.
      *
      * @param s the <code>String</code> representation of <code>true</code>
      *   or false
@@ -92,11 +100,23 @@
     }
 
     /**
-     * Calls <code>Boolean(String)</code> to create the new object.
-     * @see #Boolean(java.lang.String)
+     * Returns the Boolean <code>TRUE</code> if the given boolean is
+     * <code>true</code>, otherwise it will return the Boolean
+     * <code>FALSE</code>.
+     *
+     * @since 1.4
+     */
+    public static Boolean valueOf(boolean b) {
+       return b ? TRUE : FALSE;
+    }
+
+    /**
+     * Returns the Boolean <code>TRUE</code> if and only if the given
+     * String is equal, ignoring case, to the the String "true", otherwise
+     * it will return the Boolean <code>FALSE</code>.
      */
     public static Boolean valueOf(String s) {
-       return new Boolean(s);
+       return "true".equalsIgnoreCase(s) ? TRUE : FALSE;
     }
 
     /**
@@ -132,6 +152,17 @@
        return (val != null && val.equalsIgnoreCase("true"));
     }
     
+    /**
+     * Returns "true" if the value of the give boolean is <code>true</code> and
+     * returns "false" if the value of the given boolean is <code>false</code>.
+     *
+     * @since 1.4
+     */
+    public static String toString(boolean b)
+    {
+       return b ? "true" : "false";
+    }
+
     /**
      * Returns "true" if the value of this object is <code>true</code> and
      * returns "false" if the value of this object is <code>false</code>.
Index: java/lang/Double.java
===================================================================
RCS file: /cvs/classpath/java/lang/Double.java,v
retrieving revision 1.16
diff -u -u -r1.16 Double.java
--- java/lang/Double.java       2001/06/25 04:43:56     1.16
+++ java/lang/Double.java       2001/07/09 23:07:36
@@ -1,5 +1,5 @@
 /* Double.java -- object wrapper for double primitive
-   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.
 
@@ -305,20 +305,43 @@
    */
   public int compareTo(Double d)
   {
-    double x = d.doubleValue();
-
-    if (isNaN (value))
-      return isNaN(x) ? 0 : 1;
-    if ((value == 0.0d) && (x == -0.0d))
+    return compare(value, d.value);
+  }
+    
+  /**
+   * Returns 0 if the first argument is equal to the second argument.
+   * Returns a number less than zero if the first argument is less than the
+   * second argument, and returns a number greater than zero if the first
+   * argument is greater than the second argument.
+   * <br>
+   * <code>Double.NaN</code> is greater than any number other than itself, 
+   * even <code>Double.POSITIVE_INFINITY</code>.
+   * <br>
+   * <code>0.0d</code> is greater than <code>-0.0d</code>.
+   *
+   * @param x the first double to compare.
+   * @param y the second double to compare.
+   * @return  0 if the arguments are the same, &lt; 0 if the
+   *          first argument is less than the second argument in
+   *          in question, or &gt; 0 if it is greater.
+   * @since 1.4
+   */
+  public static int compare(double x, double y)
+  {
+    if (isNaN (x))
+      return isNaN(y) ? 0 : 1;
+    if (isNaN (y))
+      return -1;
+    if ((x == 0.0d) && (y == -0.0d))
       return 1;
-    if ((value == -0.0d) && (x == 0.0d))
+    if ((x == -0.0d) && (y == 0.0d))
       return -1;
-    if (value == x)
+    if (x == y)
       return 0;
 
-    return (value > x) ? 1 : -1;
+    return (x > y) ? 1 : -1;
   }
-    
+
   /**
    * Compares the specified <code>Object</code> to this <code>Double</code>
    * if and only if the <code>Object</code> is an instanceof 
Index: java/lang/Float.java
===================================================================
RCS file: /cvs/classpath/java/lang/Float.java,v
retrieving revision 1.15
diff -u -u -r1.15 Float.java
--- java/lang/Float.java        2001/06/25 04:43:56     1.15
+++ java/lang/Float.java        2001/07/09 23:07:36
@@ -425,16 +425,41 @@
      */
     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 compare(value, f.value);
+    }
+    
+   /**
+    * Returns 0 if the first argument is equal to the second argument.
+    * Returns a number less than zero if the first argument is less than the
+    * second argument, and returns a number greater than zero if the first
+    * argument is greater than the second 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 x the first float to compare.
+    * @param y the second float to compare.
+    * @return  0 if the arguments are the same, &lt; 0 if the
+    *          first argument is less than the second argument in
+    *          in question, or &gt; 0 if it is greater.
+    * @since 1.4
+    */
+    public static int compare(float x, float y)
+    {
+        if (isNaN (x))
+            return isNaN (y) ? 0 : 1;
+        if (isNaN (y))
+            return -1;
+        if ((x == 0.0) && (y == -0.0))
             return 1;
-        if ((value == -0.0) && (x == 0.0))
+        if ((x == -0.0) && (y == 0.0))
             return -1;
+        if (x == y)
+            return 0;
 
-        return ((value - x) > 0) ? 1 : -1;
+        return (x > y) ? 1 : -1;
     }
     
     /**

Reply via email to