Index: ChangeLog =================================================================== RCS file: /cvsroot/classpath/classpath/ChangeLog,v retrieving revision 1.2902 diff -u -3 -p -u -r1.2902 ChangeLog --- ChangeLog 14 Dec 2004 07:39:33 -0000 1.2902 +++ ChangeLog 15 Dec 2004 02:43:14 -0000 @@ -1,3 +1,9 @@ +2004-12-14 Andrew John Hughes + + * java/util/Currency.java + New implementation of this class so as to + use iso4271.properties. + 2004-12-14 Michael Koch * javax/swing/JTree.java Index: java/util/Currency.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/util/Currency.java,v retrieving revision 1.6 diff -u -3 -p -u -r1.6 Currency.java --- java/util/Currency.java 22 Oct 2004 18:02:06 -0000 1.6 +++ java/util/Currency.java 15 Dec 2004 02:43:15 -0000 @@ -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 Currency 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. - */ - private Currency (Locale loc) - { - this.locale = loc; - this.res = ResourceBundle.getBundle ("gnu.java.locale.LocaleInformation", - locale, ClassLoader.getSystemClassLoader()); - /* Retrieve the ISO4217 currency code */ - try + * @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) + { + 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("")) { - currencyCode = res.getString ("intlCurrencySymbol"); + return; } - catch (Exception _) + /* 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 = null; + return; } + /* Split off the first currency code (we only use the first for now) */ + commaPosition = currencyCode.indexOf(","); + if (commaPosition != -1) + { + 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 String 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(); } /** Index: resource/java/util/Makefile.am =================================================================== RCS file: /cvsroot/classpath/classpath/resource/java/util/Makefile.am,v retrieving revision 1.3 diff -u -3 -p -u -r1.3 Makefile.am --- resource/java/util/Makefile.am 13 Sep 2004 16:43:12 -0000 1.3 +++ resource/java/util/Makefile.am 15 Dec 2004 02:43:19 -0000 @@ -11,4 +11,6 @@ iso639-a3.properties \ iso639_de.properties \ iso639.properties \ iso639_fr.properties \ -iso639_ga.properties +iso639_ga.properties \ +iso4217.properties +