src/share/classes/java/util/Currency.java
Print this page
rev 5696 : 6336885: RFE: Locale Data Deployment Enhancements
4609153: Provide locale data for Indic locales
5104387: Support for gl_ES locale (galician language)
6337471: desktop/system locale preferences support
7056139: (cal) SPI support for locale-dependent Calendar parameters
7058206: Provide CalendarData SPI for week params and display field value names
7073852: Support multiple scripts for digits and decimal symbols per locale
7079560: [Fmt-Da] Context dependent month names support in SimpleDateFormat
7171324: getAvailableLocales() of locale sensitive services should return the actual availability of locales
7151414: (cal) Support calendar type identification
7168528: LocaleServiceProvider needs to be aware of Locale extensions
7171372: (cal) locale's default Calendar should be created if unknown calendar is specified
Summary: JEP 127: Improve Locale Data Packaging and Adopt Unicode CLDR Data (part 1 w/o packaging changes. by Naoto Sato and Masayoshi Okutsu)
*** 34,52 ****
import java.io.Serializable;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
- import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.spi.CurrencyNameProvider;
! import java.util.spi.LocaleServiceProvider;
! import sun.util.LocaleServiceProviderPool;
import sun.util.logging.PlatformLogger;
- import sun.util.resources.LocaleData;
- import sun.util.resources.OpenListResourceBundle;
/**
* Represents a currency. Currencies are identified by their ISO 4217 currency
* codes. Visit the <a href="http://www.iso.org/iso/en/prods-services/popstds/currencycodes.html">
--- 34,48 ----
import java.io.Serializable;
import java.security.AccessController;
import java.security.PrivilegedAction;
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.LocaleServiceProviderPool;
import sun.util.logging.PlatformLogger;
/**
* Represents a currency. Currencies are identified by their ISO 4217 currency
* codes. Visit the <a href="http://www.iso.org/iso/en/prods-services/popstds/currencycodes.html">
*** 189,207 ****
// Currency data format version
private static final int VALID_FORMAT_VERSION = 1;
static {
! AccessController.doPrivileged(new PrivilegedAction<Object>() {
! public Object run() {
String homeDir = System.getProperty("java.home");
try {
String dataFile = homeDir + File.separator +
"lib" + File.separator + "currency.data";
! DataInputStream dis = new DataInputStream(
new BufferedInputStream(
! new FileInputStream(dataFile)));
if (dis.readInt() != MAGIC_NUMBER) {
throw new InternalError("Currency data is possibly corrupted");
}
formatVersion = dis.readInt();
if (formatVersion != VALID_FORMAT_VERSION) {
--- 185,204 ----
// Currency data format version
private static final int VALID_FORMAT_VERSION = 1;
static {
! AccessController.doPrivileged(new PrivilegedAction<Void>() {
! @Override
! public Void run() {
String homeDir = System.getProperty("java.home");
try {
String dataFile = homeDir + File.separator +
"lib" + File.separator + "currency.data";
! try (DataInputStream dis = new DataInputStream(
new BufferedInputStream(
! new FileInputStream(dataFile)))) {
if (dis.readInt() != MAGIC_NUMBER) {
throw new InternalError("Currency data is possibly corrupted");
}
formatVersion = dis.readInt();
if (formatVersion != VALID_FORMAT_VERSION) {
*** 219,229 ****
scNewCurrenciesNumericCode = readIntArray(dis, scCount);
int ocCount = dis.readInt();
otherCurrencies = dis.readUTF();
otherCurrenciesDFD = readIntArray(dis, ocCount);
otherCurrenciesNumericCode = readIntArray(dis, ocCount);
! dis.close();
} catch (IOException e) {
throw new InternalError(e);
}
// look for the properties file for overrides
--- 216,226 ----
scNewCurrenciesNumericCode = readIntArray(dis, scCount);
int ocCount = dis.readInt();
otherCurrencies = dis.readUTF();
otherCurrenciesDFD = readIntArray(dis, ocCount);
otherCurrenciesNumericCode = readIntArray(dis, ocCount);
! }
} catch (IOException e) {
throw new InternalError(e);
}
// look for the properties file for overrides
*** 342,355 ****
* have a currency, such as Antarctica.
*
* @param locale the locale for whose country a <code>Currency</code>
* instance is needed
* @return the <code>Currency</code> instance for the country of the given
! * locale, or null
* @exception NullPointerException if <code>locale</code> or its country
! * code is null
! * @exception IllegalArgumentException if the country of the given locale
* is not a supported ISO 3166 country code.
*/
public static Currency getInstance(Locale locale) {
String country = locale.getCountry();
if (country == null) {
--- 339,352 ----
* have a currency, such as Antarctica.
*
* @param locale the locale for whose country a <code>Currency</code>
* instance is needed
* @return the <code>Currency</code> instance for the country of the given
! * locale, or {@code null}
* @exception NullPointerException if <code>locale</code> or its country
! * code 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) {
*** 366,376 ****
if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
&& tableEntry != INVALID_COUNTRY_ENTRY) {
char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');
int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
! StringBuffer sb = new StringBuffer(country);
sb.append(finalChar);
return getInstance(sb.toString(), defaultFractionDigits, numericCode);
} else {
// special cases
if (tableEntry == INVALID_COUNTRY_ENTRY) {
--- 363,373 ----
if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
&& tableEntry != INVALID_COUNTRY_ENTRY) {
char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');
int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
! StringBuilder sb = new StringBuilder(country);
sb.append(finalChar);
return getInstance(sb.toString(), defaultFractionDigits, numericCode);
} else {
// special cases
if (tableEntry == INVALID_COUNTRY_ENTRY) {
*** 468,505 ****
* needed
* @return the symbol of this currency for the specified locale
* @exception NullPointerException if <code>locale</code> is null
*/
public String getSymbol(Locale locale) {
- try {
- // Check whether a provider can provide an implementation that's closer
- // to the requested locale than what the Java runtime itself can provide.
LocaleServiceProviderPool pool =
LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
-
- if (pool.hasProviders()) {
- // Assuming that all the country locales include necessary currency
- // symbols in the Java runtime's resources, so there is no need to
- // examine whether Java runtime's currency resource bundle is missing
- // names. Therefore, no resource bundle is provided for calling this
- // method.
String symbol = pool.getLocalizedObject(
CurrencyNameGetter.INSTANCE,
! locale, (OpenListResourceBundle)null,
! currencyCode, SYMBOL);
if (symbol != null) {
return symbol;
}
- }
- ResourceBundle bundle = LocaleData.getCurrencyNames(locale);
- return bundle.getString(currencyCode);
- } catch (MissingResourceException e) {
// use currency code as symbol of last resort
return currencyCode;
}
- }
/**
* Gets the default number of fraction digits used with this currency.
* For example, the default number of fraction digits for the Euro is 2,
* while for the Japanese Yen it's 0.
--- 465,486 ----
* 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);
String symbol = pool.getLocalizedObject(
CurrencyNameGetter.INSTANCE,
! locale, currencyCode, SYMBOL);
if (symbol != null) {
return symbol;
}
// use currency code as symbol of last resort
return currencyCode;
}
/**
* Gets the default number of fraction digits used with this currency.
* For example, the default number of fraction digits for the Euro is 2,
* while for the Japanese Yen it's 0.
*** 544,578 ****
* @return the display name of this currency for the specified locale
* @exception NullPointerException if <code>locale</code> is null
* @since 1.7
*/
public String getDisplayName(Locale locale) {
- try {
- OpenListResourceBundle bundle = LocaleData.getCurrencyNames(locale);
- String result = null;
- String bundleKey = currencyCode.toLowerCase(Locale.ROOT);
-
- // Check whether a provider can provide an implementation that's closer
- // to the requested locale than what the Java runtime itself can provide.
LocaleServiceProviderPool pool =
LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
! if (pool.hasProviders()) {
! result = pool.getLocalizedObject(
CurrencyNameGetter.INSTANCE,
! locale, bundleKey, bundle, currencyCode, DISPLAYNAME);
! }
!
! if (result == null) {
! result = bundle.getString(bundleKey);
! }
!
if (result != null) {
return result;
}
- } catch (MissingResourceException e) {
- // fall through
- }
// use currency code as symbol of last resort
return currencyCode;
}
--- 525,542 ----
* @return the display name of this currency for the specified locale
* @exception NullPointerException if <code>locale</code> is null
* @since 1.7
*/
public String getDisplayName(Locale locale) {
LocaleServiceProviderPool pool =
LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
! String result = pool.getLocalizedObject(
CurrencyNameGetter.INSTANCE,
! locale, currencyCode, DISPLAYNAME);
if (result != null) {
return result;
}
// use currency code as symbol of last resort
return currencyCode;
}
*** 579,588 ****
--- 543,553 ----
/**
* Returns the ISO 4217 currency code of this currency.
*
* @return the ISO 4217 currency code of this currency
*/
+ @Override
public String toString() {
return currencyCode;
}
/**
*** 621,630 ****
--- 586,596 ----
private static class CurrencyNameGetter
implements LocaleServiceProviderPool.LocalizedObjectGetter<CurrencyNameProvider,
String> {
private static final CurrencyNameGetter INSTANCE = new CurrencyNameGetter();
+ @Override
public String getObject(CurrencyNameProvider currencyNameProvider,
Locale locale,
String key,
Object... params) {
assert params.length == 1;