From a90f7efbd5f42ebfad32d4b58293ce49c8bd26ec Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Wed, 8 May 2002 23:36:37 +0000 Subject: [PATCH] ICU-1097 initial version of Currency object X-SVN-Rev: 8575 --- icu4j/src/com/ibm/icu/text/DecimalFormat.java | 101 +++++++++++++++--- .../ibm/icu/text/DecimalFormatSymbols.java | 30 +++++- 2 files changed, 111 insertions(+), 20 deletions(-) diff --git a/icu4j/src/com/ibm/icu/text/DecimalFormat.java b/icu4j/src/com/ibm/icu/text/DecimalFormat.java index 8b83c5138c..e9aba6be29 100755 --- a/icu4j/src/com/ibm/icu/text/DecimalFormat.java +++ b/icu4j/src/com/ibm/icu/text/DecimalFormat.java @@ -5,13 +5,14 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/DecimalFormat.java,v $ - * $Date: 2002/03/10 19:40:16 $ - * $Revision: 1.15 $ + * $Date: 2002/05/08 23:36:37 $ + * $Revision: 1.16 $ * ***************************************************************************************** */ package com.ibm.icu.text; +import com.ibm.icu.util.Currency; import java.text.Format; import java.text.ParsePosition; import java.text.FieldPosition; @@ -394,6 +395,7 @@ public class DecimalFormat extends NumberFormat { String pattern = getPattern(def, 0); // Always applyPattern after the symbols are set this.symbols = new DecimalFormatSymbols(def); + currency = Currency.getInstance(def); applyPattern(pattern, false); } @@ -416,7 +418,9 @@ public class DecimalFormat extends NumberFormat { */ public DecimalFormat(String pattern) { // Always applyPattern after the symbols are set - this.symbols = new DecimalFormatSymbols( Locale.getDefault() ); + Locale def = Locale.getDefault(); + this.symbols = new DecimalFormatSymbols(def); + currency = Currency.getInstance(def); applyPattern( pattern, false ); } @@ -442,7 +446,7 @@ public class DecimalFormat extends NumberFormat { */ public DecimalFormat (String pattern, DecimalFormatSymbols symbols) { // Always applyPattern after the symbols are set - this.symbols = (DecimalFormatSymbols)symbols.clone(); + _setDecimalFormatSymbols(symbols); applyPattern( pattern, false ); } @@ -1385,17 +1389,35 @@ public class DecimalFormat extends NumberFormat { * @see DecimalFormatSymbols */ public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) { - try { - // don't allow multiple references - symbols = (DecimalFormatSymbols) newSymbols.clone(); - /*Bug 4212072 - Update the affix strings accroding to symbols in order to keep - the affix strings up to date. - [Richard/GCL] - */ - expandAffixes(); - } catch (Exception foo) { - // should never happen + _setDecimalFormatSymbols(newSymbols); + expandAffixes(); + } + + private void _setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) { + // don't allow multiple references + symbols = (DecimalFormatSymbols) newSymbols.clone(); + /*Bug 4212072 + Update the affix strings accroding to symbols in order to keep + the affix strings up to date. + [Richard/GCL] + */ + + // With the introduction of the Currency object, the currency + // symbols in the DFS object are ignored. For backward + // compatibility, we check any explicitly set DFS object. If it + // is a default symbols object for its locale, we change the + // currency object to one for that locale. If it is custom, + // we create a CompatibilityCurrency object and use that. + DecimalFormatSymbols def = + new DecimalFormatSymbols(symbols.getLocale()); + + if (symbols.getCurrencySymbol().equals( + def.getCurrencySymbol()) && + symbols.getInternationalCurrencySymbol().equals( + def.getInternationalCurrencySymbol())) { + currency = Currency.getInstance(symbols.getLocale()); + } else { + currency = Currency.getCompatibilityInstance(symbols); } } @@ -1997,12 +2019,14 @@ public class DecimalFormat extends NumberFormat { // c = pattern.charAt(i++); switch (c) { case CURRENCY_SIGN: + // As of ICU 2.2 we use the currency object, and + // ignore the currency symbols in the DFS. if (iCurrency object used to display currency + * amounts. This takes effect immediately, if this format is a + * currency format. If this format is not a currency format, then + * the currency object is used if and when this object becomes a + * currency format through the application of a new pattern. + * @since ICU 2.2 + */ + public void setCurrency(Currency theCurrency) { + // If we are a currency format, then modify our affixes to + // encode the currency symbol for the given currency in our + // locale, and adjust the decimal digits and rounding for the + // given currency. + + currency = theCurrency; + + if (isCurrencyFormat) { + setRoundingIncrement(currency.getRoundingIncrement()); + + int d = currency.getDefaultFractionDigits(); + setMinimumFractionDigits(d); + setMaximumFractionDigits(d); + + expandAffixes(); + } + } + + /** + * Gets the Currency object used to display currency + * amounts. + * @since ICU 2.2 + */ + public Currency getCurrency() { + return currency; + } + /** * Sets the maximum number of digits allowed in the fraction portion of a * number. This override limits the fraction digit count to 340. @@ -3078,6 +3138,13 @@ public class DecimalFormat extends NumberFormat { */ private int padPosition = PAD_BEFORE_PREFIX; + /** + * Currency object used to format currencies. Only used if + * isCurrencyFormat is true. + * @since ICU 2.2 + */ + private Currency currency; + //---------------------------------------------------------------------- static final int currentSerialVersion = 2; diff --git a/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java b/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java index dcf99aa9e4..ecd8d285bd 100755 --- a/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java +++ b/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/DecimalFormatSymbols.java,v $ - * $Date: 2002/03/10 19:40:16 $ - * $Revision: 1.5 $ + * $Date: 2002/05/08 23:36:37 $ + * $Revision: 1.6 $ * ***************************************************************************************** */ @@ -307,6 +307,13 @@ final public class DecimalFormatSymbols implements Cloneable, Serializable { padEscape = c; } + /** + * Returns the locale for which this object was constructed. + */ + public Locale getLocale() { + return locale; + } + /** * Standard override. */ @@ -361,6 +368,7 @@ final public class DecimalFormatSymbols implements Cloneable, Serializable { * cleaned up. */ private void initialize( Locale locale ) { + this.locale = locale; /* try the cache first */ String[][] data = (String[][]) cachedLocaleData.get(locale); String[] numberElements; @@ -441,6 +449,14 @@ final public class DecimalFormatSymbols implements Cloneable, Serializable { // notation isn't supported by the old classes, even though the // symbol is there. } + if (serialVersionOnStream < 3) { + // Resurrected objects from old streams will have no + // locale. There is no 100% fix for this. A + // 90% fix is to construct a mapping of data back to + // locale, perhaps a hash of all our members. This is + // expensive and doesn't seem worth it. + locale = Locale.getDefault(); + } serialVersionOnStream = currentSerialVersion; } @@ -581,6 +597,13 @@ final public class DecimalFormatSymbols implements Cloneable, Serializable { */ private char plusSign; + /** + * The locale for which this object was constructed. Set to the + * default locale for objects resurrected from old streams. + * @since ICU 2.2 + */ + private Locale locale; + // Proclaim JDK 1.1 FCS compatibility static final long serialVersionUID = 5772796243397350300L; @@ -590,7 +613,8 @@ final public class DecimalFormatSymbols implements Cloneable, Serializable { // monetarySeparator and exponential. // - 2 for version from AlphaWorks, which includes 3 new fields: // padEscape, exponentSeparator, and plusSign. - private static final int currentSerialVersion = 2; + // - 3 for ICU 2.2, which includes the locale field + private static final int currentSerialVersion = 3; /** * Describes the version of DecimalFormatSymbols present on the stream.