[kaffe] CVS kaffe (robilad): Resynced with GNU classpath: new implementation of Currency

Kaffe CVS cvs-commits at kaffe.org
Mon Dec 20 19:51:22 PST 2004


PatchSet 5690 
Date: 2004/12/21 03:47:04
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU classpath: new implementation of Currency

Members: 
	ChangeLog:1.3236->1.3237 
	libraries/javalib/java/util/Currency.java:1.7->1.8 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.3236 kaffe/ChangeLog:1.3237
--- kaffe/ChangeLog:1.3236	Tue Dec 21 03:18:54 2004
+++ kaffe/ChangeLog	Tue Dec 21 03:47:04 2004
@@ -1,5 +1,16 @@
 2004-12-21  Dalibor Topic  <robilad at kaffe.org>
 
+	* libraries/javalib/java/util/Currency.java:
+	Resynced with GNU Classpath.
+
+	2004-12-19  Andrew John Hughes  <gnu_andrew at member.fsf.org>
+
+        * java/util/Currency.java
+        New implementation of this class so as to
+        use iso4271.properties.
+
+2004-12-21  Dalibor Topic  <robilad at kaffe.org>
+
 	* libraries/javalib/javax/swing/JComboBox.java,
 	libraries/javalib/javax/swing/DefaultComboBoxModel.java,
 	libraries/javalib/javax/swing/plaf/basic/BasicComboBoxUI.java:
Index: kaffe/libraries/javalib/java/util/Currency.java
diff -u kaffe/libraries/javalib/java/util/Currency.java:1.7 kaffe/libraries/javalib/java/util/Currency.java:1.8
--- kaffe/libraries/javalib/java/util/Currency.java:1.7	Sat Oct 23 17:15:40 2004
+++ kaffe/libraries/javalib/java/util/Currency.java	Tue Dec 21 03:47:05 2004
@@ -37,9 +37,10 @@
 
 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 @@
   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 @@
   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 @@
   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 @@
    * 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,26 +183,9 @@
    *
    * @return a <code>String</code> containing currency code.
    */
-  public String getCurrencyCode ()
+  public String getCurrencyCode()
   {
-    try
-      {
-	ResourceBundle bundle =
-	  ResourceBundle.getBundle("gnu.java.locale.iso4217", locale);
-	
-	String currencyCode = bundle.getString (locale.getCountry());
-	// If currencyCode is null we get a NPE which will make the function return a null.
-	int sep = currencyCode.indexOf(',');
-	
-	if (sep > 0)
-	  return currencyCode.substring(0, sep);
-
-	return currencyCode;
-      }
-    catch (Exception _)
-      {
-	return null;
-      }
+    return currencyCode;
   }
 
   /**
@@ -183,11 +200,9 @@
    *
    * @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;
   }
     
   /**
@@ -205,7 +220,7 @@
    * @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
@@ -223,8 +238,19 @@
       {
         /* 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;
@@ -239,15 +265,26 @@
    * @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;
       }
@@ -270,15 +307,11 @@
    */
   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();
   }
 
   /**
@@ -308,37 +341,11 @@
    */
   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();
   }
 
   /**




More information about the kaffe mailing list