diff options
author | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2004-12-19 20:52:14 +0000 |
---|---|---|
committer | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2004-12-19 20:52:14 +0000 |
commit | 4b98a06faeee93e2dc0292e3c1e0ae6842c2dece (patch) | |
tree | 96da30aeb8aac1c7ce74b249f8076a000f96de3a | |
parent | ece79c7090a11e3054ccb9eb05d01d5b529f7ea8 (diff) | |
download | classpath-4b98a06faeee93e2dc0292e3c1e0ae6842c2dece.tar.gz |
2004-12-19 Andrew John Hughes <gnu_andrew@member.fsf.org>
* java/util/Currency.java
New implementation of this class so as to
use iso4271.properties.
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | java/util/Currency.java | 178 |
2 files changed, 107 insertions, 77 deletions
@@ -1,3 +1,9 @@ +2004-12-19 Andrew John Hughes <gnu_andrew@member.fsf.org> + + * java/util/Currency.java + New implementation of this class so as to + use iso4271.properties. + 2004-12-19 Michael Koch <konqueror@gmx.de> * java/util/Locale.java: Make sure all locales get only created once. diff --git a/java/util/Currency.java b/java/util/Currency.java index 98a599e65..45546273a 100644 --- a/java/util/Currency.java +++ b/java/util/Currency.java @@ -37,9 +37,10 @@ exception statement from your version. */ package java.util; +import java.io.IOException; import java.io.ObjectStreamException; import java.io.Serializable; -import java.text.NumberFormat; +import java.util.Properties; /** * Representation of a currency for a particular locale. Each currency @@ -64,23 +65,14 @@ public final class Currency static final long serialVersionUID = -158308464356906721L; /** - * The locale associated with this currency. - * - * @see #Currency(java.util.Locale) - * @see #getInstance(java.util.Locale) - * @see #getSymbol(java.util.Locale) - * @serial ignored. - */ - private transient Locale locale; - - /** - * The resource bundle which maps the currency to - * a ISO 4217 currency code. + * The set of properties which map a currency to + * the currency information such as the ISO 4217 + * currency code and the number of decimal points. * * @see #getCurrencyCode() * @serial ignored. */ - private transient ResourceBundle res; + private static transient Properties properties; /** * The ISO 4217 currency code associated with this @@ -92,6 +84,15 @@ public final class Currency private String currencyCode; /** + * The number of fraction digits associated with this + * particular instance. + * + * @see #getDefaultFractionDigits() + * @serial the number of fraction digits + */ + private transient int fractionDigits; + + /** * A cache of <code>Currency</code> instances to * ensure the singleton nature of this class. The key * is the locale of the currency. @@ -103,17 +104,29 @@ public final class Currency private static transient Map cache; /** - * Instantiates the cache. + * Instantiates the cache and reads in the properties. */ static { + /* Create a hash map for the cache */ cache = new HashMap(); + /* Create the properties object */ + properties = new Properties(); + /* Try and load the properties from our iso4217.properties resource */ + try + { + properties.load(Currency.class.getResourceAsStream("iso4217.properties")); + } + catch (IOException exception) + { + System.out.println("Failed to load currency resource: " + exception); + } } /** * Default constructor for deserialization */ - private Currency () + private Currency() { } @@ -126,22 +139,43 @@ public final class Currency * a particular country changes. For countries without * a given currency (e.g. Antarctica), the result is null. * - * @param loc the locale for the new currency. + * @param loc the locale for the new currency, or null if + * there is no country code specified or a currency + * for this country. */ - private Currency (Locale loc) + private Currency(Locale loc) { - this.locale = loc; - this.res = ResourceBundle.getBundle ("gnu.java.locale.LocaleInformation", - locale, ClassLoader.getSystemClassLoader()); - /* Retrieve the ISO4217 currency code */ - try + String countryCode; + String currencyKey; + String fractionDigitsKey; + int commaPosition; + + /* Retrieve the country code from the locale */ + countryCode = loc.getCountry(); + /* If there is no country code, return */ + if (countryCode.equals("")) + { + return; + } + /* Construct the key for the currency */ + currencyKey = countryCode + ".currency"; + /* Construct the key for the fraction digits */ + fractionDigitsKey = countryCode + ".fractionDigits"; + /* Retrieve the currency */ + currencyCode = properties.getProperty(currencyKey); + /* Return if the currency code is null */ + if (currencyCode == null) { - currencyCode = res.getString ("intlCurrencySymbol"); + return; } - catch (Exception _) + /* Split off the first currency code (we only use the first for now) */ + commaPosition = currencyCode.indexOf(","); + if (commaPosition != -1) { - currencyCode = null; + currencyCode = currencyCode.substring(0, commaPosition); } + /* Retrieve the fraction digits */ + fractionDigits = Integer.parseInt(properties.getProperty(fractionDigitsKey)); } /** @@ -149,7 +183,7 @@ public final class Currency * * @return a <code>String</code> containing currency code. */ - public String getCurrencyCode () + public String getCurrencyCode() { return currencyCode; } @@ -166,11 +200,9 @@ public final class Currency * * @return the number of digits after the decimal separator for this currency. */ - public int getDefaultFractionDigits () + public int getDefaultFractionDigits() { - NumberFormat currency = NumberFormat.getCurrencyInstance (locale); - - return currency.getMaximumFractionDigits(); + return fractionDigits; } /** @@ -188,7 +220,7 @@ public final class Currency * @throws IllegalArgumentException if the country of * the given locale is not a supported ISO3166 code. */ - public static Currency getInstance (Locale locale) + public static Currency getInstance(Locale locale) { /** * The new instance must be the only available instance @@ -206,8 +238,19 @@ public final class Currency { /* Create the currency for this locale */ newCurrency = new Currency (locale); - /* Cache it */ - cache.put(locale, newCurrency); + /* + * If the currency code is null, then creation failed + * and we return null. + */ + if (newCurrency.getCurrencyCode() == null) + { + return null; + } + else + { + /* Cache it */ + cache.put(locale, newCurrency); + } } /* Return the instance */ return newCurrency; @@ -222,15 +265,26 @@ public final class Currency * @throws IllegalArgumentException if the supplied currency code * is not a supported ISO 4217 code. */ - public static Currency getInstance (String currencyCode) + public static Currency getInstance(String currencyCode) { - Locale[] allLocales = Locale.getAvailableLocales (); - + Locale[] allLocales; + + /* + * Throw a null pointer exception explicitly if currencyCode is null. + * One is not thrown otherwise. It results in an IllegalArgumentException. + */ + if (currencyCode == null) + { + throw new NullPointerException("The supplied currency code is null."); + } + /* Get all locales */ + allLocales = Locale.getAvailableLocales(); + /* Loop through each locale, looking for the code */ for (int i = 0;i < allLocales.length; i++) { Currency testCurrency = getInstance (allLocales[i]); - if (testCurrency.getCurrencyCode() != null && + if (testCurrency != null && testCurrency.getCurrencyCode().equals(currencyCode)) return testCurrency; } @@ -253,15 +307,11 @@ public final class Currency */ public String getSymbol() { - try - { - /* What does this return if there is no mapping? */ - return res.getString ("currencySymbol"); - } - catch (Exception _) - { - return null; - } + /* + We don't currently have the currency symbols, so we always + return the currency code. + */ + return getCurrencyCode(); } /** @@ -291,37 +341,11 @@ public final class Currency */ public String getSymbol(Locale locale) { - // TODO. The behaviour is unclear if locale != this.locale. - // First we need to implement fully LocaleInformation*.java - /* - * FIXME: My reading of how this method works has this implementation - * as wrong. It should return a value relating to how the specified - * locale handles the symbol for this currency. This implementation - * seems to just do a variation of getInstance(locale). - */ - try - { - ResourceBundle localeResource = - ResourceBundle.getBundle ("gnu.java.locale.LocaleInformation", - locale, Currency.class.getClassLoader()); - - if (localeResource.equals(res)) - return localeResource.getString ("currencySymbol"); - else - return localeResource.getString ("intlCurrencySymbol"); - } - catch (Exception e1) - { - try - { - return res.getString ("intlCurrencySymbol"); - } - catch (Exception e2) - { - return null; - } - } + We don't currently have the currency symbols, so we always + return the currency code. + */ + return getCurrencyCode(); } /** |