src/java.base/share/classes/java/util/Currency.java
changeset 48251 57148c79bd75
parent 47216 71c04702a3d5
child 48929 28d8fc8cd3cd
equal deleted inserted replaced
48250:739aa297c260 48251:57148c79bd75
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2017, 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
    26 package java.util;
    26 package java.util;
    27 
    27 
    28 import java.io.BufferedInputStream;
    28 import java.io.BufferedInputStream;
    29 import java.io.DataInputStream;
    29 import java.io.DataInputStream;
    30 import java.io.File;
    30 import java.io.File;
    31 import java.io.FileInputStream;
       
    32 import java.io.FileReader;
    31 import java.io.FileReader;
    33 import java.io.InputStream;
    32 import java.io.InputStream;
    34 import java.io.IOException;
    33 import java.io.IOException;
    35 import java.io.Serializable;
    34 import java.io.Serializable;
    36 import java.security.AccessController;
    35 import java.security.AccessController;
    40 import java.util.concurrent.ConcurrentHashMap;
    39 import java.util.concurrent.ConcurrentHashMap;
    41 import java.util.concurrent.ConcurrentMap;
    40 import java.util.concurrent.ConcurrentMap;
    42 import java.util.regex.Pattern;
    41 import java.util.regex.Pattern;
    43 import java.util.regex.Matcher;
    42 import java.util.regex.Matcher;
    44 import java.util.spi.CurrencyNameProvider;
    43 import java.util.spi.CurrencyNameProvider;
       
    44 import sun.util.locale.provider.CalendarDataUtility;
    45 import sun.util.locale.provider.LocaleServiceProviderPool;
    45 import sun.util.locale.provider.LocaleServiceProviderPool;
    46 import sun.util.logging.PlatformLogger;
    46 import sun.util.logging.PlatformLogger;
    47 
    47 
    48 
    48 
    49 /**
    49 /**
   346      * currencies. For example, for the original member countries of the
   346      * currencies. For example, for the original member countries of the
   347      * European Monetary Union, the method returns the old national currencies
   347      * European Monetary Union, the method returns the old national currencies
   348      * until December 31, 2001, and the Euro from January 1, 2002, local time
   348      * until December 31, 2001, and the Euro from January 1, 2002, local time
   349      * of the respective countries.
   349      * of the respective countries.
   350      * <p>
   350      * <p>
       
   351      * If the specified {@code locale} contains "cu" and/or "rg"
       
   352      * <a href="./Locale.html#def_locale_extension">Unicode extensions</a>,
       
   353      * the instance returned from this method reflects
       
   354      * the values specified with those extensions. If both "cu" and "rg" are
       
   355      * specified, the currency from the "cu" extension supersedes the implicit one
       
   356      * from the "rg" extension.
       
   357      * <p>
   351      * The method returns <code>null</code> for territories that don't
   358      * The method returns <code>null</code> for territories that don't
   352      * have a currency, such as Antarctica.
   359      * have a currency, such as Antarctica.
   353      *
   360      *
   354      * @param locale the locale for whose country a <code>Currency</code>
   361      * @param locale the locale for whose country a <code>Currency</code>
   355      * instance is needed
   362      * instance is needed
   359      * is {@code null}
   366      * is {@code null}
   360      * @exception IllegalArgumentException if the country of the given {@code locale}
   367      * @exception IllegalArgumentException if the country of the given {@code locale}
   361      * is not a supported ISO 3166 country code.
   368      * is not a supported ISO 3166 country code.
   362      */
   369      */
   363     public static Currency getInstance(Locale locale) {
   370     public static Currency getInstance(Locale locale) {
   364         String country = locale.getCountry();
   371         // check for locale overrides
   365         if (country == null) {
   372         String override = locale.getUnicodeLocaleType("cu");
   366             throw new NullPointerException();
   373         if (override != null) {
   367         }
   374             try {
   368 
   375                 return getInstance(override.toUpperCase(Locale.ROOT));
   369         if (country.length() != 2) {
   376             } catch (IllegalArgumentException iae) {
       
   377                 // override currency is invalid. Fall through.
       
   378             }
       
   379         }
       
   380 
       
   381         String country = CalendarDataUtility.findRegionOverride(locale).getCountry();
       
   382 
       
   383         if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
   370             throw new IllegalArgumentException();
   384             throw new IllegalArgumentException();
   371         }
   385         }
   372 
   386 
   373         char char1 = country.charAt(0);
   387         char char1 = country.charAt(0);
   374         char char2 = country.charAt(1);
   388         char char2 = country.charAt(1);
   480      * {@link Locale.Category#DISPLAY DISPLAY} locale.
   494      * {@link Locale.Category#DISPLAY DISPLAY} locale.
   481      * For example, for the US Dollar, the symbol is "$" if the default
   495      * For example, for the US Dollar, the symbol is "$" if the default
   482      * locale is the US, while for other locales it may be "US$". If no
   496      * locale is the US, while for other locales it may be "US$". If no
   483      * symbol can be determined, the ISO 4217 currency code is returned.
   497      * symbol can be determined, the ISO 4217 currency code is returned.
   484      * <p>
   498      * <p>
       
   499      * If the default {@link Locale.Category#DISPLAY DISPLAY} locale
       
   500      * contains "rg" (region override)
       
   501      * <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
       
   502      * the symbol returned from this method reflects
       
   503      * the value specified with that extension.
       
   504      * <p>
   485      * This is equivalent to calling
   505      * This is equivalent to calling
   486      * {@link #getSymbol(Locale)
   506      * {@link #getSymbol(Locale)
   487      *     getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}.
   507      *     getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}.
   488      *
   508      *
   489      * @return the symbol of this currency for the default
   509      * @return the symbol of this currency for the default
   496     /**
   516     /**
   497      * Gets the symbol of this currency for the specified locale.
   517      * Gets the symbol of this currency for the specified locale.
   498      * For example, for the US Dollar, the symbol is "$" if the specified
   518      * For example, for the US Dollar, the symbol is "$" if the specified
   499      * locale is the US, while for other locales it may be "US$". If no
   519      * locale is the US, while for other locales it may be "US$". If no
   500      * symbol can be determined, the ISO 4217 currency code is returned.
   520      * symbol can be determined, the ISO 4217 currency code is returned.
       
   521      * <p>
       
   522      * If the specified {@code locale} contains "rg" (region override)
       
   523      * <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
       
   524      * the symbol returned from this method reflects
       
   525      * the value specified with that extension.
   501      *
   526      *
   502      * @param locale the locale for which a display name for this currency is
   527      * @param locale the locale for which a display name for this currency is
   503      * needed
   528      * needed
   504      * @return the symbol of this currency for the specified locale
   529      * @return the symbol of this currency for the specified locale
   505      * @exception NullPointerException if <code>locale</code> is null
   530      * @exception NullPointerException if <code>locale</code> is null
   506      */
   531      */
   507     public String getSymbol(Locale locale) {
   532     public String getSymbol(Locale locale) {
   508         LocaleServiceProviderPool pool =
   533         LocaleServiceProviderPool pool =
   509             LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
   534             LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
       
   535         locale = CalendarDataUtility.findRegionOverride(locale);
   510         String symbol = pool.getLocalizedObject(
   536         String symbol = pool.getLocalizedObject(
   511                                 CurrencyNameGetter.INSTANCE,
   537                                 CurrencyNameGetter.INSTANCE,
   512                                 locale, currencyCode, SYMBOL);
   538                                 locale, currencyCode, SYMBOL);
   513         if (symbol != null) {
   539         if (symbol != null) {
   514             return symbol;
   540             return symbol;