This is the mail archive of the java-patches@sourceware.cygnus.com 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]

java.text patches


Here's three micellanious patches for the java.text format classes.

1) a tenative fix for libgcj/38 - this fixes the circular initializer
dependency by delaying the construction nonLocalizedSymbols until it is
actually needed. This seems to work but it does add some runtime
overhead, and may not really be the best place for the fix - I'm open to
any other suggestions.

2) The (String pattern) constructor for MessageFormat just calls
applyPattern, which assumes that a locale has been set. It needs to set
the default locale before calling applyPattern, or a
NullPointerException occurs.

3) NumberFormat throws a ClassCastException if the object being
formatted is not actually a Number. It should throw an
IllegialArgumentException. Verfified against JDK behaviour.

regards

  [ bryce ]



1999-09-16  Bryce McKinlay  <bryce@albatross.co.nz>
        * java/text/DecimalFormat.java: Remove nonLocalizedSymbols
        initializer. Fix for PR libgcj/38.
        (checkNonLocalizedSymbols): New method.
        (toPattern): Call checkNonLocalizedSymbols().
        (applyPattern): ditto.
        * java/text/MessageFormat.java (MessageFormat(String)): Set the
        default locale.
        * java/text/NumberFormat.java: Check that object is a Number. If

        not, throw IllegialArgumentException.

Index: DecimalFormat.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/text/DecimalFormat.java,v
retrieving revision 1.2
diff -u -r1.2 DecimalFormat.java
--- DecimalFormat.java 1999/06/10 23:53:37 1.2
+++ DecimalFormat.java 1999/09/16 06:01:18
@@ -328,6 +328,7 @@
       // contradicts this.  Empirical tests with patterns of "0,###.0"
       // and "#.#.#" corroborate the p. 629 statement that an
       // IllegalArgumentException is thrown.
+      checkNonLocalizedSymbols();
       applyPatternWithSymbols (pattern, nonLocalizedSymbols);
     }

@@ -964,8 +965,22 @@

   public String toPattern ()
     {
+      checkNonLocalizedSymbols();
       return computePattern (nonLocalizedSymbols);
     }
+
+  // creating an instance of DecimalFormatSymbols eventually requires a

+  // DecimalFormat. This workaround prevents obvious problems that
occur when
+  // this is placed in a static initializer.
+  private static void checkNonLocalizedSymbols()
+  {
+    if (nonLocalizedSymbols == null)
+      {
+ // The locale-independent pattern symbols happen to be the same as
+ // the US symbols.
+ nonLocalizedSymbols = new DecimalFormatSymbols (Locale.US);
+      }
+  }

   // These names are fixed by the serialization spec.
   private boolean decimalSeparatorAlwaysShown;
@@ -979,8 +994,5 @@
   private DecimalFormatSymbols symbols;
   private boolean useExponentialNotation;

-  // The locale-independent pattern symbols happen to be the same as
-  // the US symbols.
-  private static final DecimalFormatSymbols nonLocalizedSymbols
-    = new DecimalFormatSymbols (Locale.US);
+  private static DecimalFormatSymbols nonLocalizedSymbols;
 }
Index: MessageFormat.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/text/MessageFormat.java,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 MessageFormat.java
--- MessageFormat.java 1999/04/07 14:52:41 1.1.1.1
+++ MessageFormat.java 1999/09/16 06:01:18
@@ -400,6 +400,7 @@

   public MessageFormat (String pattern)
     {
+      locale = Locale.getDefault();
       applyPattern (pattern);
     }

Index: NumberFormat.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/text/NumberFormat.java,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 NumberFormat.java
--- NumberFormat.java 1999/04/07 14:52:41 1.1.1.1
+++ NumberFormat.java 1999/09/16 06:01:18
@@ -37,7 +37,11 @@
   public final StringBuffer format (Object obj, StringBuffer sbuf,
         FieldPosition pos)
     {
-      return format(((Number) obj).doubleValue(), sbuf, pos);
+      if (obj instanceof Number)
+        return format(((Number) obj).doubleValue(), sbuf, pos);
+      else
+        throw new IllegalArgumentException
+  ("Cannot format given Object as a Number");
     }

   public abstract StringBuffer format (double number,



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