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,19 +34,15 @@
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.locale.provider.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">
@@ -189,19 +185,20 @@
// Currency data format version
private static final int VALID_FORMAT_VERSION = 1;
static {
- AccessController.doPrivileged(new PrivilegedAction<Object>() {
- public Object run() {
+ 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";
- DataInputStream dis = new DataInputStream(
+ try (DataInputStream dis = new DataInputStream(
new BufferedInputStream(
- new FileInputStream(dataFile)));
+ 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,11 +216,11 @@
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
@@ -342,14 +339,14 @@
* 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
+ * locale, or {@code null}
* @exception NullPointerException if <code>locale</code> or its country
- * code is null
- * @exception IllegalArgumentException if the country of the given locale
+ * 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,11 +363,11 @@
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);
+ StringBuilder sb = new StringBuilder(country);
sb.append(finalChar);
return getInstance(sb.toString(), defaultFractionDigits, numericCode);
} else {
// special cases
if (tableEntry == INVALID_COUNTRY_ENTRY) {
@@ -468,38 +465,22 @@
* 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);
+ locale, 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.
@@ -544,35 +525,18 @@
* @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(
+ String result = pool.getLocalizedObject(
CurrencyNameGetter.INSTANCE,
- locale, bundleKey, bundle, currencyCode, DISPLAYNAME);
- }
-
- if (result == null) {
- result = bundle.getString(bundleKey);
- }
-
+ locale, currencyCode, DISPLAYNAME);
if (result != null) {
return result;
}
- } catch (MissingResourceException e) {
- // fall through
- }
// use currency code as symbol of last resort
return currencyCode;
}
@@ -579,10 +543,11 @@
/**
* 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,10 +586,11 @@
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;