src/java.base/share/classes/java/util/Currency.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 53120 de9fd809bb47
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    53  * Represents a currency. Currencies are identified by their ISO 4217 currency
    53  * Represents a currency. Currencies are identified by their ISO 4217 currency
    54  * codes. Visit the <a href="http://www.iso.org/iso/home/standards/currency_codes.htm">
    54  * codes. Visit the <a href="http://www.iso.org/iso/home/standards/currency_codes.htm">
    55  * ISO web site</a> for more information.
    55  * ISO web site</a> for more information.
    56  * <p>
    56  * <p>
    57  * The class is designed so that there's never more than one
    57  * The class is designed so that there's never more than one
    58  * <code>Currency</code> instance for any given currency. Therefore, there's
    58  * {@code Currency} instance for any given currency. Therefore, there's
    59  * no public constructor. You obtain a <code>Currency</code> instance using
    59  * no public constructor. You obtain a {@code Currency} instance using
    60  * the <code>getInstance</code> methods.
    60  * the {@code getInstance} methods.
    61  * <p>
    61  * <p>
    62  * Users can supersede the Java runtime currency data by means of the system
    62  * Users can supersede the Java runtime currency data by means of the system
    63  * property {@systemProperty java.util.currency.data}. If this system property is
    63  * property {@systemProperty java.util.currency.data}. If this system property is
    64  * defined then its value is the location of a properties file, the contents of
    64  * defined then its value is the location of a properties file, the contents of
    65  * which are key/value pairs of the ISO 3166 country codes and the ISO 4217
    65  * which are key/value pairs of the ISO 3166 country codes and the ISO 4217
   111  * @see java.math.BigDecimal
   111  * @see java.math.BigDecimal
   112  * @since 1.4
   112  * @since 1.4
   113  */
   113  */
   114 public final class Currency implements Serializable {
   114 public final class Currency implements Serializable {
   115 
   115 
       
   116     @java.io.Serial
   116     private static final long serialVersionUID = -158308464356906721L;
   117     private static final long serialVersionUID = -158308464356906721L;
   117 
   118 
   118     /**
   119     /**
   119      * ISO 4217 currency code for this currency.
   120      * ISO 4217 currency code for this currency.
   120      *
   121      *
   271     private static final int SYMBOL = 0;
   272     private static final int SYMBOL = 0;
   272     private static final int DISPLAYNAME = 1;
   273     private static final int DISPLAYNAME = 1;
   273 
   274 
   274 
   275 
   275     /**
   276     /**
   276      * Constructs a <code>Currency</code> instance. The constructor is private
   277      * Constructs a {@code Currency} instance. The constructor is private
   277      * so that we can insure that there's never more than one instance for a
   278      * so that we can insure that there's never more than one instance for a
   278      * given currency.
   279      * given currency.
   279      */
   280      */
   280     private Currency(String currencyCode, int defaultFractionDigits, int numericCode) {
   281     private Currency(String currencyCode, int defaultFractionDigits, int numericCode) {
   281         this.currencyCode = currencyCode;
   282         this.currencyCode = currencyCode;
   282         this.defaultFractionDigits = defaultFractionDigits;
   283         this.defaultFractionDigits = defaultFractionDigits;
   283         this.numericCode = numericCode;
   284         this.numericCode = numericCode;
   284     }
   285     }
   285 
   286 
   286     /**
   287     /**
   287      * Returns the <code>Currency</code> instance for the given currency code.
   288      * Returns the {@code Currency} instance for the given currency code.
   288      *
   289      *
   289      * @param currencyCode the ISO 4217 code of the currency
   290      * @param currencyCode the ISO 4217 code of the currency
   290      * @return the <code>Currency</code> instance for the given currency code
   291      * @return the {@code Currency} instance for the given currency code
   291      * @exception NullPointerException if <code>currencyCode</code> is null
   292      * @throws    NullPointerException if {@code currencyCode} is null
   292      * @exception IllegalArgumentException if <code>currencyCode</code> is not
   293      * @throws    IllegalArgumentException if {@code currencyCode} is not
   293      * a supported ISO 4217 code.
   294      * a supported ISO 4217 code.
   294      */
   295      */
   295     public static Currency getInstance(String currencyCode) {
   296     public static Currency getInstance(String currencyCode) {
   296         return getInstance(currencyCode, Integer.MIN_VALUE, 0);
   297         return getInstance(currencyCode, Integer.MIN_VALUE, 0);
   297     }
   298     }
   347         instance = instances.putIfAbsent(currencyCode, currencyVal);
   348         instance = instances.putIfAbsent(currencyCode, currencyVal);
   348         return (instance != null ? instance : currencyVal);
   349         return (instance != null ? instance : currencyVal);
   349     }
   350     }
   350 
   351 
   351     /**
   352     /**
   352      * Returns the <code>Currency</code> instance for the country of the
   353      * Returns the {@code Currency} instance for the country of the
   353      * given locale. The language and variant components of the locale
   354      * given locale. The language and variant components of the locale
   354      * are ignored. The result may vary over time, as countries change their
   355      * are ignored. The result may vary over time, as countries change their
   355      * currencies. For example, for the original member countries of the
   356      * currencies. For example, for the original member countries of the
   356      * European Monetary Union, the method returns the old national currencies
   357      * European Monetary Union, the method returns the old national currencies
   357      * until December 31, 2001, and the Euro from January 1, 2002, local time
   358      * until December 31, 2001, and the Euro from January 1, 2002, local time
   362      * the instance returned from this method reflects
   363      * the instance returned from this method reflects
   363      * the values specified with those extensions. If both "cu" and "rg" are
   364      * the values specified with those extensions. If both "cu" and "rg" are
   364      * specified, the currency from the "cu" extension supersedes the implicit one
   365      * specified, the currency from the "cu" extension supersedes the implicit one
   365      * from the "rg" extension.
   366      * from the "rg" extension.
   366      * <p>
   367      * <p>
   367      * The method returns <code>null</code> for territories that don't
   368      * The method returns {@code null} for territories that don't
   368      * have a currency, such as Antarctica.
   369      * have a currency, such as Antarctica.
   369      *
   370      *
   370      * @param locale the locale for whose country a <code>Currency</code>
   371      * @param locale the locale for whose country a {@code Currency}
   371      * instance is needed
   372      * instance is needed
   372      * @return the <code>Currency</code> instance for the country of the given
   373      * @return the {@code Currency} instance for the country of the given
   373      * locale, or {@code null}
   374      * locale, or {@code null}
   374      * @exception NullPointerException if <code>locale</code>
   375      * @throws    NullPointerException if {@code locale}
   375      * is {@code null}
   376      * is {@code null}
   376      * @exception IllegalArgumentException if the country of the given {@code locale}
   377      * @throws    IllegalArgumentException if the country of the given {@code locale}
   377      * is not a supported ISO 3166 country code.
   378      * is not a supported ISO 3166 country code.
   378      */
   379      */
   379     public static Currency getInstance(Locale locale) {
   380     public static Currency getInstance(Locale locale) {
   380         // check for locale overrides
   381         // check for locale overrides
   381         String override = locale.getUnicodeLocaleType("cu");
   382         String override = locale.getUnicodeLocaleType("cu");
   534      * the value specified with that extension.
   535      * the value specified with that extension.
   535      *
   536      *
   536      * @param locale the locale for which a display name for this currency is
   537      * @param locale the locale for which a display name for this currency is
   537      * needed
   538      * needed
   538      * @return the symbol of this currency for the specified locale
   539      * @return the symbol of this currency for the specified locale
   539      * @exception NullPointerException if <code>locale</code> is null
   540      * @throws    NullPointerException if {@code locale} is null
   540      */
   541      */
   541     public String getSymbol(Locale locale) {
   542     public String getSymbol(Locale locale) {
   542         LocaleServiceProviderPool pool =
   543         LocaleServiceProviderPool pool =
   543             LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
   544             LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
   544         locale = CalendarDataUtility.findRegionOverride(locale);
   545         locale = CalendarDataUtility.findRegionOverride(locale);
   629      * for the specified locale, the ISO 4217 currency code is returned.
   630      * for the specified locale, the ISO 4217 currency code is returned.
   630      *
   631      *
   631      * @param locale the locale for which a display name for this currency is
   632      * @param locale the locale for which a display name for this currency is
   632      * needed
   633      * needed
   633      * @return the display name of this currency for the specified locale
   634      * @return the display name of this currency for the specified locale
   634      * @exception NullPointerException if <code>locale</code> is null
   635      * @throws    NullPointerException if {@code locale} is null
   635      * @since 1.7
   636      * @since 1.7
   636      */
   637      */
   637     public String getDisplayName(Locale locale) {
   638     public String getDisplayName(Locale locale) {
   638         LocaleServiceProviderPool pool =
   639         LocaleServiceProviderPool pool =
   639             LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
   640             LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
   659     }
   660     }
   660 
   661 
   661     /**
   662     /**
   662      * Resolves instances being deserialized to a single instance per currency.
   663      * Resolves instances being deserialized to a single instance per currency.
   663      */
   664      */
       
   665     @java.io.Serial
   664     private Object readResolve() {
   666     private Object readResolve() {
   665         return getInstance(currencyCode);
   667         return getInstance(currencyCode);
   666     }
   668     }
   667 
   669 
   668     /**
   670     /**
  1193         }
  1195         }
  1194 
  1196 
  1195     }
  1197     }
  1196 
  1198 
  1197 }
  1199 }
  1198 
       
  1199