[kaffe] CVS kaffe (guilhem): New "Java 1.4" java.text methods + java.util.Currency implementation

Kaffe CVS cvs-commits at kaffe.org
Wed Aug 27 07:35:02 PDT 2003


PatchSet 3987 
Date: 2003/08/27 14:32:09
Author: guilhem
Branch: HEAD
Tag: (none) 
Log:
New "Java 1.4" java.text methods + java.util.Currency implementation

Members: 
	libraries/javalib/Makefile.am:1.133->1.134 
	libraries/javalib/Makefile.in:1.180->1.181 
	libraries/javalib/essential.files:1.23->1.24 
	libraries/javalib/java/text/DecimalFormat.java:1.19->1.20 
	libraries/javalib/java/text/DecimalFormatSymbols.java:1.11->1.12 
	libraries/javalib/java/text/MessageFormat.java:1.19->1.20 
	libraries/javalib/java/text/NumberFormat.java:1.17->1.18 
	libraries/javalib/java/util/Currency.java:INITIAL->1.1 

Index: kaffe/libraries/javalib/Makefile.am
diff -u kaffe/libraries/javalib/Makefile.am:1.133 kaffe/libraries/javalib/Makefile.am:1.134
--- kaffe/libraries/javalib/Makefile.am:1.133	Mon Aug 25 06:14:47 2003
+++ kaffe/libraries/javalib/Makefile.am	Wed Aug 27 14:32:09 2003
@@ -1543,6 +1543,7 @@
 	java/util/Collections.java \
 	java/util/Comparator.java \
 	java/util/ConcurrentModificationException.java \
+	java/util/Currency.java \
 	java/util/Date.java \
 	java/util/Dictionary.java \
 	java/util/EmptyStackException.java \
Index: kaffe/libraries/javalib/Makefile.in
diff -u kaffe/libraries/javalib/Makefile.in:1.180 kaffe/libraries/javalib/Makefile.in:1.181
--- kaffe/libraries/javalib/Makefile.in:1.180	Mon Aug 25 06:14:47 2003
+++ kaffe/libraries/javalib/Makefile.in	Wed Aug 27 14:32:09 2003
@@ -1842,6 +1842,7 @@
 	java/util/Collections.java \
 	java/util/Comparator.java \
 	java/util/ConcurrentModificationException.java \
+	java/util/Currency.java \
 	java/util/Date.java \
 	java/util/Dictionary.java \
 	java/util/EmptyStackException.java \
Index: kaffe/libraries/javalib/essential.files
diff -u kaffe/libraries/javalib/essential.files:1.23 kaffe/libraries/javalib/essential.files:1.24
--- kaffe/libraries/javalib/essential.files:1.23	Mon Aug 25 06:30:12 2003
+++ kaffe/libraries/javalib/essential.files	Wed Aug 27 14:32:09 2003
@@ -248,6 +248,7 @@
 java/util/Calendar.java
 java/util/Collection.java
 java/util/Collections.java
+java/util/Currency.java
 java/util/Comparator.java
 java/util/ConcurrentModificationException.java
 java/util/Date.java
Index: kaffe/libraries/javalib/java/text/DecimalFormat.java
diff -u kaffe/libraries/javalib/java/text/DecimalFormat.java:1.19 kaffe/libraries/javalib/java/text/DecimalFormat.java:1.20
--- kaffe/libraries/javalib/java/text/DecimalFormat.java:1.19	Tue Aug 26 15:03:10 2003
+++ kaffe/libraries/javalib/java/text/DecimalFormat.java	Wed Aug 27 14:32:10 2003
@@ -736,6 +736,11 @@
     return dest;
   }
 
+  public Currency getCurrency()
+  {
+    return symbols.getCurrency();
+  }
+
   public DecimalFormatSymbols getDecimalFormatSymbols ()
   {
     return symbols;
@@ -953,6 +958,11 @@
     pos.setIndex(index + suffix.length());
 
     return result;
+  }
+
+  public void setCurrency(Currency currency)
+  {
+    symbols.setCurrency(currency);
   }
 
   public void setDecimalFormatSymbols (DecimalFormatSymbols newSymbols)
Index: kaffe/libraries/javalib/java/text/DecimalFormatSymbols.java
diff -u kaffe/libraries/javalib/java/text/DecimalFormatSymbols.java:1.11 kaffe/libraries/javalib/java/text/DecimalFormatSymbols.java:1.12
--- kaffe/libraries/javalib/java/text/DecimalFormatSymbols.java:1.11	Sat Aug 16 11:03:47 2003
+++ kaffe/libraries/javalib/java/text/DecimalFormatSymbols.java	Wed Aug 27 14:32:10 2003
@@ -39,6 +39,7 @@
 package java.text;
 
 import java.io.Serializable;
+import java.util.Currency;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
@@ -157,6 +158,7 @@
     percent = safeGetChar (res, "percent", '%');
     perMill = safeGetChar (res, "perMill", '\u2030');
     zeroDigit = safeGetChar (res, "zeroDigit", '0');
+    locale = loc;
   }
 
   /**
@@ -195,6 +197,18 @@
   }
 
   /**
+   * Returns the currency corresponding to the currency symbol stored
+   * in the instance of <code>DecimalFormatSymbols</code>.
+   *
+   * @return A new instance of <code>Currency</code> if
+   * the currency code matches a known one.
+   */
+  public Currency getCurrency ()
+  {
+    return Currency.getInstance (currencySymbol);
+  }
+
+  /**
    * This method returns the currency symbol in local format.  For example,
    * "$" for Canadian dollars.
    *
@@ -354,6 +368,16 @@
   }
 
   /**
+   * This method sets the currency to the specified value.
+   *
+   * @param currency The new currency
+   */
+  public void setCurrency (Currency currency)
+  {
+    setCurrencySymbol (currency.getSymbol());
+  }
+
+  /**
    * This method sets the currency symbol to the specified value.
    *
    * @param currencySymbol The new currency symbol
@@ -556,14 +580,20 @@
   private char perMill;
   /**
    * @serial This value represents the type of object being de-serialized.
-   * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later.
+   * 0 indicates a pre-Java 1.1.6 version, 1 indicates 1.1.6 or later,
+   * 2 indicates 1.4 or later
    */
-  private int serialVersionOnStream = 1;
+  private int serialVersionOnStream = 2;
   /**
    * @serial This is the character used to represent 0.
    */
   private char zeroDigit;
 
+  /**
+   * @serial the locale of these currency symbols.
+   */
+  private Locale locale;
+
   private static final long serialVersionUID = 5772796243397350300L;
 
   private void readObject(ObjectInputStream stream)
@@ -574,7 +604,11 @@
       {
         monetarySeparator = decimalSeparator;
 	exponential = 'E';
-	serialVersionOnStream = 1;
       }
+    if (serialVersionOnStream < 2)
+      {
+	locale = Locale.getDefault();
+      }
+    serialVersionOnStream = 2;
   }
 }
Index: kaffe/libraries/javalib/java/text/MessageFormat.java
diff -u kaffe/libraries/javalib/java/text/MessageFormat.java:1.19 kaffe/libraries/javalib/java/text/MessageFormat.java:1.20
--- kaffe/libraries/javalib/java/text/MessageFormat.java:1.19	Tue Aug 26 15:03:10 2003
+++ kaffe/libraries/javalib/java/text/MessageFormat.java	Wed Aug 27 14:32:10 2003
@@ -453,6 +453,8 @@
 		else
 		  formatter.format(thisArg, appendBuf, ignore);
 	      }
+
+	    elements[i].format = formatter;
 	  }
 
 	if (output_iterator != null)
@@ -547,16 +549,38 @@
 
   /**
    * Creates a new MessageFormat object with
-   * the specified pattern
+   * the specified pattern. The locale resource bundle 
+   * is taken as the system default one.
    *
-   * @param aPattern The Pattern
+   * @param pattern The Pattern
    */
   public MessageFormat (String pattern)
   {
-    locale = Locale.getDefault();
+    this(pattern, Locale.getDefault());
+  }
+
+  /**
+   * Creates a new MessageFormat object with
+   * the specified pattern and Locale context.
+   *
+   * @param pattern the pattern
+   * @param locale Locale environment to use
+   */
+  public MessageFormat (String pattern, Locale locale)
+  {
+    this.locale = locale;
     applyPattern (pattern);
   }
 
+  /**
+   * Parse a string <code>sourceStr</code> against the pattern specified
+   * to the MessageFormat constructor.
+   *
+   * @param sourceStr the string to be parsed.
+   * @param pos the current parse position (and eventually the error position).
+   * @return the array of parsed objects sorted according to their argument number
+   * in the pattern.
+   */ 
   public Object[] parse (String sourceStr, ParsePosition pos)
   {
     // Check initial text.
@@ -704,6 +728,72 @@
   public String toPattern ()
   {
     return pattern;
+  }
+
+  /**
+   * Return the formatters used sorted by argument index. It uses the
+   * internal table to fill in this array: if a format has been
+   * set using <code>setFormat</code> or <code>setFormatByArgumentIndex</code>
+   * then it returns it at the right index. If not it uses the detected
+   * formatters during a <code>format</code> call. If nothing is known
+   * about that argument index it just puts null at that position.
+   * To get useful informations you may have to call <code>format</code>
+   * at least once.
+   *
+   * @return an array of formatters sorted by argument index.
+   */
+  public Format[] getFormatsByArgumentIndex()
+  {
+    int argNumMax = 0;
+    // First, find the greatest argument number.
+    for (int i=0;i<elements.length;i++)
+      if (elements[i].argNumber > argNumMax)
+	argNumMax = elements[i].argNumber;
+
+    Format[] formats = new Format[argNumMax];
+    for (int i=0;i<elements.length;i++)
+      {
+	if (elements[i].setFormat != null)
+	  formats[elements[i].argNumber] = elements[i].setFormat;
+	else if (elements[i].format != null)
+	  formats[elements[i].argNumber] = elements[i].format;
+      }
+    return formats;
+  }
+
+  /**
+   * Set the format to used using the argument index number.
+   *
+   * @param argumentIndex the argument index.
+   * @param newFormat the format to use for this argument.
+   */
+  public void setFormatByArgumentIndex(int argumentIndex,
+				       Format newFormat)
+  {
+    for (int i=0;i<elements.length;i++)
+      {
+	if (elements[i].argNumber == argumentIndex)
+	  elements[i].setFormat = newFormat;
+      }
+  }
+
+  /**
+   * Set the format for argument using a specified array of formatters
+   * which is sorted according to the argument index. If the number of
+   * elements in the array is fewer than the number of arguments only
+   * the arguments specified by the array are touched.
+   *
+   * @param newFormats array containing the new formats to set.
+   *
+   * @throws NullPointerException if newFormats is null
+   */
+  public void setFormatsByArgumentIndex(Format[] newFormats)
+  {
+    for (int i=0;i<newFormats.length;i++)
+      {
+	// Nothing better than that can exist here.
+	setFormatByArgumentIndex(i, newFormats[i]);
+      }
   }
 
   // The pattern string.
Index: kaffe/libraries/javalib/java/text/NumberFormat.java
diff -u kaffe/libraries/javalib/java/text/NumberFormat.java:1.17 kaffe/libraries/javalib/java/text/NumberFormat.java:1.18
--- kaffe/libraries/javalib/java/text/NumberFormat.java:1.17	Tue Aug 26 15:03:10 2003
+++ kaffe/libraries/javalib/java/text/NumberFormat.java	Wed Aug 27 14:32:10 2003
@@ -41,6 +41,7 @@
 import java.util.Locale;
 import java.util.ResourceBundle;
 import java.util.MissingResourceException;
+import java.util.Currency;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.IOException;
@@ -316,6 +317,16 @@
     return computeInstance (loc, "currencyFormat", "$#,##0.00;($#,##0.00)");
   }
 
+  public Currency getCurrency()
+  {
+    throw new UnsupportedOperationException("getCurrency");
+  }
+
+  public void setCurrency(Currency currency)
+  {
+    throw new UnsupportedOperationException("setCurrency");    
+  }
+
   /**
    * This method returns a default instance for the default locale. This
    * will be a concrete subclass of <code>NumberFormat</code>, but the 
@@ -396,8 +407,6 @@
    * will be a concrete subclass of <code>NumberFormat</code>, but the 
    * actual class returned is dependent on the locale.
    *
-   * @param locale The desired locale.
-   *
    * @return An instance of the default <code>NumberFormat</code> class.
    */
   public static final NumberFormat getNumberInstance ()
@@ -411,11 +420,41 @@
    * <code>NumberFormat</code>, but the actual class returned is dependent
    * on the locale.
    *
-   * @return An instance of a generic number formatter for the default locale.
+   * @param loc the desired locale.
+   *
+   * @return An instance of a generic number formatter for the specified locale.
    */
   public static NumberFormat getNumberInstance (Locale loc)
   {
     return computeInstance (loc, "numberFormat", "#,##0.###");
+  }
+
+  /**
+   * This method returns an integer formatting and parsing class for the
+   * default locale. This will be a concrete subclass of <code>NumberFormat</code>,
+   * but the actual class returned is dependent on the locale.
+   *
+   * @return An instance of an integer number formatter for the default locale.
+   */
+  public static final NumberFormat getIntegerInstance()
+  {
+    return getIntegerInstance (Locale.getDefault());
+  }
+
+  /**
+   * This method returns an integer formatting and parsing class for the
+   * default locale. This will be a concrete subclass of <code>NumberFormat</code>,
+   * but the actual class returned is dependent on the locale.
+   *
+   * @param loc the desired locale.
+   *
+   * @return An instance of an integer number formatter for the default locale.
+   */
+  public static NumberFormat getIntegerInstance(Locale loc)
+  {
+    NumberFormat format = computeInstance (loc, "numberFormat", "#,##0");
+    format.setParseIntegerOnly (true);
+    return format;
   }
 
   /**
===================================================================
Checking out kaffe/libraries/javalib/java/util/Currency.java
RCS:  /home/cvs/kaffe/kaffe/libraries/javalib/java/util/Currency.java,v
VERS: 1.1
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/libraries/javalib/java/util/Currency.java	Wed Aug 27 14:34:00 2003
@@ -0,0 +1,189 @@
+/* Currency.java -- Representation of a currency
+   Copyright (C) 2003 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+ 
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+package java.util;
+
+import java.io.Serializable;
+import java.util.ResourceBundle;
+import java.util.Locale;
+import java.text.NumberFormat;
+
+public final class Currency implements Serializable
+{
+  static final long serialVersionUID = -158308464356906721L;
+
+  private Locale locale;
+  private ResourceBundle res;
+
+  // For deserialization
+  private Currency ()
+  {
+  }
+
+  private Currency (Locale loc)
+  {
+    this.locale = loc;
+    this.res = ResourceBundle.getBundle ("gnu.java.locale.LocaleInformation", locale);
+  }
+
+  /**
+   * Returns the ISO4217 currency code of this currency.
+   *
+   * @return a <code>String</code> containing currency code.
+   */
+  public String getCurrencyCode ()
+  {
+    try
+      {
+	return res.getString ("intlCurrencySymbol");
+      }
+    catch (Exception _)
+      {
+	return null;
+      }
+  }
+
+  /**
+   * @return number of digits after decimal separator for this currency.
+   */   
+  public int getDefaultFractionDigits ()
+  {
+    NumberFormat currency = NumberFormat.getCurrencyInstance (locale);
+    
+    return currency.getMaximumFractionDigits();
+  }
+    
+  /**
+   * Builds a new currency instance for this locale.
+   *
+   * @param locale a <code>Locale</code> instance.
+   * 
+   * @return a new <code>Currency</code> instance.
+   */ 
+  public static Currency getInstance (Locale locale)
+  {
+    return new Currency (locale);
+  }
+
+  /**
+   * Builds the currency corresponding to the specified currency code.
+   *
+   * @param currencyCode a string representing a currency code.
+   *
+   * @return a new <code>Currency</code> instance.
+   */
+  public static Currency getInstance (String currencyCode)
+  {
+    Locale[] all_locales = Locale.getAvailableLocales ();
+    
+    for (int i=0;i<all_locales.length;i++)
+      {
+	Currency test_currency = getInstance (all_locales[i]);
+	
+	if (test_currency.getCurrencyCode() != null &&
+	    test_currency.getCurrencyCode().equals(currencyCode))
+	  return test_currency;
+      }
+    
+    return null;
+  }
+
+  /**
+   * This method returns the currency symbol.
+   *
+   * @return the currency symbol.
+   */
+  public String getSymbol()
+  {
+    try
+      {
+	return res.getString ("currencySymbol");
+      }
+    catch (Exception _)
+      {
+	return null;
+      }
+  }
+
+  /**
+   * This methods returns the currency symbol expressed in the specified locale.
+   *
+   * @param locale the locale to express the symbol in.
+   * @return the currency symbol.
+   */
+  public String getSymbol(Locale locale)
+  {
+    // TODO. The behaviour is unclear if locale != this.locale.
+    // First we need to implement fully LocaleInformation*.java
+    try
+      {
+	ResourceBundle res = ResourceBundle.getBundle ("gnu.java.locale.LocaleInformation", locale);
+
+	if (res.equals(this.res))
+	  return res.getString ("currencySymbol");
+	else
+	  return res.getString ("intlCurrencySymbol");
+      }
+    catch (Exception e1)
+      {
+	try
+	  {
+	    return res.getString ("intlCurrencySymbol");
+	  }
+	catch (Exception e2)
+	  {
+	    return null;
+	  }
+      }
+  }
+
+  /**
+   * Returns the international ISO4217 currency code of this currency.
+   *
+   * @return a <code>String</code> containing the ISO4217 currency code.
+   */
+  public String toString()
+  {
+    try
+      {
+	return res.getString ("intlCurrencySymbol");
+      }
+    catch (Exception _)
+      {
+	return "(unknown currency)";
+      }
+  }
+}




More information about the kaffe mailing list