Skip to content

Commit

Permalink
Issue #619: updated java.util.Locale, Currency to 1.7.
Browse files Browse the repository at this point in the history
	Change on 2015/09/30 by tball <tball@google.com>
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=104316459
  • Loading branch information
tomball committed Oct 1, 2015
1 parent 15b233a commit 2efe030
Show file tree
Hide file tree
Showing 5 changed files with 1,876 additions and 120 deletions.
1 change: 1 addition & 0 deletions jre_emul/Makefile
Expand Up @@ -530,6 +530,7 @@ JAVA_SOURCES_CORE = \
java/util/IllegalFormatFlagsException.java \
java/util/IllegalFormatPrecisionException.java \
java/util/IllegalFormatWidthException.java \
java/util/IllformedLocaleException.java \
java/util/InvalidPropertiesFormatException.java \
java/util/Iterator.java \
java/util/LinkedHashMap.java \
Expand Down
50 changes: 40 additions & 10 deletions jre_emul/android/libcore/luni/src/main/java/java/util/Currency.java
Expand Up @@ -35,6 +35,11 @@ public final class Currency implements Serializable {

private Currency(String currencyCode) {
this.currencyCode = currencyCode;
String symbol = ICU.getCurrencySymbol(Locale.US, currencyCode);
if (symbol == null) {
throw new IllegalArgumentException("Unsupported ISO 4217 currency code: " +
currencyCode);
}
}

/**
Expand All @@ -60,6 +65,9 @@ public static Currency getInstance(String currencyCode) {
*/
public static Currency getInstance(Locale locale) {
synchronized (localesToCurrencies) {
if (locale == null) {
throw new NullPointerException("locale == null");
}
Currency currency = localesToCurrencies.get(locale);
if (currency != null) {
return currency;
Expand All @@ -71,7 +79,7 @@ public static Currency getInstance(Locale locale) {
country = country + "_" + variant;
}

String currencyCode = ICU.getCurrencyCode(locale.toString());
String currencyCode = ICU.getCurrencyCode(country);
if (currencyCode == null) {
throw new IllegalArgumentException("Unsupported ISO 3166 country: " + locale);
} else if (currencyCode.equals("XXX")) {
Expand Down Expand Up @@ -103,6 +111,24 @@ public String getCurrencyCode() {
return currencyCode;
}

/**
* Equivalent to {@code getDisplayName(Locale.getDefault())}.
* See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
* @since 1.7
*/
public String getDisplayName() {
return getDisplayName(Locale.getDefault());
}

/**
* Returns the localized name of this currency in the given {@code locale}.
* Returns the ISO 4217 currency code if no localized name is available.
* @since 1.7
*/
public String getDisplayName(Locale locale) {
return ICU.getCurrencyDisplayName(locale, currencyCode);
}

/**
* Equivalent to {@code getSymbol(Locale.getDefault())}.
* See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
Expand All @@ -123,18 +149,17 @@ public String getSymbol() {
* <p>If there is no locale-specific currency symbol, the ISO 4217 currency code is returned.
*/
public String getSymbol(Locale locale) {
if (locale.getCountry().length() == 0) {
return currencyCode;
if (locale == null) {
throw new NullPointerException("locale == null");
}

// Check the locale first, in case the locale has the same currency.
LocaleData localeData = LocaleData.get(locale);
if (localeData.internationalCurrencySymbol.equals(currencyCode)) {
return localeData.currencySymbol;
}

// Try ICU, and fall back to the currency code if ICU has nothing.
String symbol = ICU.getCurrencySymbol(locale.toString());
String symbol = ICU.getCurrencySymbol(locale, currencyCode);
return symbol != null ? symbol : currencyCode;
}

Expand All @@ -146,11 +171,12 @@ public String getSymbol(Locale locale) {
* IMF Special Drawing Rights, -1 is returned.
*/
public int getDefaultFractionDigits() {
// In some places the code XXX is used as the fall back currency.
if (currencyCode.equals("XXX")) {
return -1;
}
return ICU.getCurrencyFractionDigits(currencyCode);
// In some places the code XXX is used as the fall back currency.
// The RI returns -1, but ICU defaults to 2 for unknown currencies.
if (currencyCode.equals("XXX")) {
return -1;
}
return ICU.getCurrencyFractionDigits(currencyCode);
}

/**
Expand All @@ -160,4 +186,8 @@ public int getDefaultFractionDigits() {
public String toString() {
return currencyCode;
}

private Object readResolve() {
return getInstance(currencyCode);
}
}
Expand Up @@ -21,7 +21,6 @@
*
* See {@link Locale} and {@link Locale.Builder}.
*
* @hide
* @since 1.7
*/
public class IllformedLocaleException extends RuntimeException {
Expand Down

0 comments on commit 2efe030

Please sign in to comment.