< prev index next >

src/java.base/share/classes/java/util/Currency.java

Print this page
rev 47733 : 8176841: Additional Unicode Language-Tag Extensions
8189134: New system properties for the default Locale extensions
Reviewed-by:

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -26,11 +26,10 @@
 package java.util;
 
 import java.io.BufferedInputStream;
 import java.io.DataInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.InputStream;
 import java.io.IOException;
 import java.io.Serializable;
 import java.security.AccessController;

@@ -40,10 +39,11 @@
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 import java.util.spi.CurrencyNameProvider;
+import sun.util.locale.provider.CalendarDataUtility;
 import sun.util.locale.provider.LocaleServiceProviderPool;
 import sun.util.logging.PlatformLogger;
 
 
 /**

@@ -346,10 +346,17 @@
      * currencies. For example, for the original member countries of the
      * European Monetary Union, the method returns the old national currencies
      * until December 31, 2001, and the Euro from January 1, 2002, local time
      * of the respective countries.
      * <p>
+     * If the specified <code>locale</code> contains "cu" and/or "rg"
+     * <a href="./Locale.html#def_locale_extension">Unicode extensions</a>,
+     * the instance returned from this method reflects 
+     * the values specified with those extensions. If both "cu" and "rg" are
+     * specified, the currency from "cu" extension supersedes the implicit one
+     * from "rg" extension.
+     * <p>
      * The method returns <code>null</code> for territories that don't
      * have a currency, such as Antarctica.
      *
      * @param locale the locale for whose country a <code>Currency</code>
      * instance is needed

@@ -359,16 +366,23 @@
      * is {@code null}
      * @exception IllegalArgumentException if the country of the given {@code locale}
      * is not a supported ISO 3166 country code.
      */
     public static Currency getInstance(Locale locale) {
-        String country = locale.getCountry();
-        if (country == null) {
-            throw new NullPointerException();
+        // check for locale overrides
+        String override = locale.getUnicodeLocaleType("cu");
+        if (override != null) {
+            try {
+                return getInstance(override.toUpperCase(Locale.ROOT));
+            } catch (IllegalArgumentException iae) {
+                // override currency is invalid. Fall through.
+            }
         }
 
-        if (country.length() != 2) {
+        String country = CalendarDataUtility.findRegionOverride(locale).orElse(locale).getCountry();
+
+        if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
             throw new IllegalArgumentException();
         }
 
         char char1 = country.charAt(0);
         char char2 = country.charAt(1);

@@ -480,10 +494,16 @@
      * {@link Locale.Category#DISPLAY DISPLAY} locale.
      * For example, for the US Dollar, the symbol is "$" if the default
      * locale is the US, while for other locales it may be "US$". If no
      * symbol can be determined, the ISO 4217 currency code is returned.
      * <p>
+     * If the default {@link Locale.Category#DISPLAY DISPLAY} locale
+     * contains "rg" (region override)
+     * <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
+     * the symbol returned from this method reflects 
+     * the value specified with that extension.
+     * <p>
      * This is equivalent to calling
      * {@link #getSymbol(Locale)
      *     getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}.
      *
      * @return the symbol of this currency for the default

@@ -496,19 +516,25 @@
     /**
      * Gets the symbol of this currency for the specified locale.
      * For example, for the US Dollar, the symbol is "$" if the specified
      * locale is the US, while for other locales it may be "US$". If no
      * symbol can be determined, the ISO 4217 currency code is returned.
+     * <p>
+     * If the specified <code>locale</code> contains "rg" (region override)
+     * <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
+     * the symbol returned from this method reflects 
+     * the value specified with that extension.
      *
      * @param locale the locale for which a display name for this currency is
      * needed
      * @return the symbol of this currency for the specified locale
      * @exception NullPointerException if <code>locale</code> is null
      */
     public String getSymbol(Locale locale) {
         LocaleServiceProviderPool pool =
             LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
+        locale = CalendarDataUtility.findRegionOverride(locale).orElse(locale);
         String symbol = pool.getLocalizedObject(
                                 CurrencyNameGetter.INSTANCE,
                                 locale, currencyCode, SYMBOL);
         if (symbol != null) {
             return symbol;
< prev index next >