This fixes a bug in DecimalFormat where the currency instance is
changed but the actual output still reflects the old currency.
This is because we precompute the prefixes and suffixes and store
them.  The patch ensures they are updated when the currency changes.

ChangeLog:

2008-08-17  Andrew John Hughes  <[EMAIL PROTECTED]>

        PR classpath/31895:
        * java/text/DecimalFormat.java:
        (setCurrency(Currency)): Update prefixes and
        suffixes when currency changes.
        * java/text/DecimalFormatSymbols.java:
        (DecimalFormatSymbols(Locale)): Set locale earlier
        so it can be used by setCurrency(Currency).
        (setCurrency(Currency)): Set the symbol correctly using
        the locale of the instance.
        * java/util/Currency.java:
        Throw error instead of just printing a message.

-- 
Andrew :)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net
PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint = F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8
Index: java/text/DecimalFormat.java
===================================================================
RCS file: /sources/classpath/classpath/java/text/DecimalFormat.java,v
retrieving revision 1.36
diff -u -u -r1.36 DecimalFormat.java
--- java/text/DecimalFormat.java        6 May 2008 22:20:41 -0000       1.36
+++ java/text/DecimalFormat.java        17 Aug 2008 22:11:32 -0000
@@ -73,7 +73,7 @@
  * Generally, to get an instance of DecimalFormat you should call the factory
  * methods in the <code>NumberFormat</code> base class.
  * 
- * @author Mario Torre <[EMAIL PROTECTED]>
+ * @author Mario Torre ([EMAIL PROTECTED])
  * @author Tom Tromey ([EMAIL PROTECTED])
  * @author Andrew John Hughes ([EMAIL PROTECTED])
  */
@@ -801,7 +801,30 @@
    */
   public void setCurrency(Currency currency)
   {
-    symbols.setCurrency(currency);
+    Currency current = symbols.getCurrency();
+    if (current != currency)
+      {
+       String oldSymbol = symbols.getCurrencySymbol();
+       int len = oldSymbol.length();
+       symbols.setCurrency(currency);
+       String newSymbol = symbols.getCurrencySymbol();
+       int posPre = positivePrefix.indexOf(oldSymbol);
+       if (posPre != -1)
+         positivePrefix = positivePrefix.substring(0, posPre) +
+           newSymbol + positivePrefix.substring(posPre+len);
+       int negPre = negativePrefix.indexOf(oldSymbol);
+       if (negPre != -1)
+         negativePrefix = negativePrefix.substring(0, negPre) +
+           newSymbol + negativePrefix.substring(negPre+len);
+       int posSuf = positiveSuffix.indexOf(oldSymbol);
+       if (posSuf != -1)
+         positiveSuffix = positiveSuffix.substring(0, posSuf) +
+           newSymbol + positiveSuffix.substring(posSuf+len);
+       int negSuf = negativeSuffix.indexOf(oldSymbol);
+       if (negSuf != -1)
+         negativeSuffix = negativeSuffix.substring(0, negSuf) +
+           newSymbol + negativeSuffix.substring(negSuf+len);
+      }
   }
   
   /**
Index: java/text/DecimalFormatSymbols.java
===================================================================
RCS file: /sources/classpath/classpath/java/text/DecimalFormatSymbols.java,v
retrieving revision 1.24
diff -u -u -r1.24 DecimalFormatSymbols.java
--- java/text/DecimalFormatSymbols.java 8 Jan 2007 00:41:24 -0000       1.24
+++ java/text/DecimalFormatSymbols.java 17 Aug 2008 22:11:32 -0000
@@ -170,6 +170,7 @@
       {
        res = null;
       }
+    locale = loc;
     currency = Currency.getInstance("XXX");
     currencySymbol = "?";
     intlCurrencySymbol = "XXX";
@@ -204,7 +205,6 @@
     percent = safeGetChar (res, "percent", '%');
     perMill = safeGetChar (res, "perMill", '\u2030');
     zeroDigit = safeGetChar (res, "zeroDigit", '0');
-    locale = loc;
   }
 
   /**
@@ -430,7 +430,7 @@
   public void setCurrency (Currency currency)
   {
     intlCurrencySymbol = currency.getCurrencyCode();
-    currencySymbol = currency.getSymbol();
+    currencySymbol = currency.getSymbol(locale);
     this.currency = currency;
   }
 
Index: java/util/Currency.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/Currency.java,v
retrieving revision 1.19
diff -u -u -r1.19 Currency.java
--- java/util/Currency.java     22 Jun 2007 18:28:00 -0000      1.19
+++ java/util/Currency.java     17 Aug 2008 22:11:32 -0000
@@ -139,7 +139,7 @@
       }
     catch (IOException exception)
       {
-        System.out.println("Failed to load currency resource: " + exception);
+        throw new InternalError("Failed to load currency resource: " + 
exception);
       }
   }
 

Reply via email to