This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RFC: Patch: java.util.Currency implementation


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hu list,


today I had an idea how to implement java.util.Currency.
Basicly it reads a file with the currency definitions and works with 
this data.

I have attached the class source and an example definition file
for you to comment.

Any comments are appreciated.


Michael
- -- 
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+r+YuWSOgCCdjSDsRAraYAJ97yKVyTU87Qnbn22qtUyJtB6eSZACdG3Kw
vNrlafkJCcicFCg8vZ9UOUc=
=qC4f
-----END PGP SIGNATURE-----
/* Currency.java
   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.FileInputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * @author Michael Koch <konqueror@gmx.de>
 * @since 1.4
 */
public final class Currency implements Serializable
{
  /**
   * Compatible with JDK 1.4+.
   */
  private static final long serialVersionUID = -158308464356906721L;

  private static final String currencyFile = "/home/mkoch/currency.properties";

  private static Properties currencyData;
  
  private String currencyCode;

  static
    {
      try
        {
          currencyData = new Properties ();
          currencyData.load (new FileInputStream (currencyFile));
        }
      catch (IOException e)
        {
        }
    }

  Currency (String currencyCode)
  {
    this.currencyCode = currencyCode;
  }

  public String getCurrencyCode ()
  {
    return currencyCode;
  }

  public int getDefaultFractionDigits ()
  {
    return Integer.parseInt (currencyData.getProperty ("fraction_" +
                                                       currencyCode));
  }

  public static Currency getInstance (Locale locale)
  {
    String currencyCode =
      currencyData.getProperty ("country_" + locale.getCountry ());
    
    if (currencyCode == null)
      throw new IllegalArgumentException
        ("locale has no supported ISO 3166 code");

    return getInstance (currencyCode);
  }

  public static Currency getInstance (String currencyCode)
  {
    if (currencyData.getProperty ("symbol_" + currencyCode) == null)
      throw new IllegalArgumentException
        ("currencyCode is not a supported ISO 4217 code");
    
    return new Currency (currencyCode);
  }

  public String getSymbol ()
  {
    String symbol = currencyData.getProperty ("symbol_" + currencyCode);

    return (symbol != null ? symbol : currencyCode);
  }

  public String getSymbol (Locale locale)
  {
    // FIMXE: not implemented
    
    return currencyCode;
  }

  public String toString ()
  {
    return currencyCode;
  }
}

Attachment: currency.properties
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]