mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-06 22:15:31 +00:00
ICU-7964 Merging new ICU4J Currency APIs and test case from the work branch to the trunk.
X-SVN-Rev: 31318
This commit is contained in:
parent
7f9aa7a129
commit
9b3a63154f
2 changed files with 147 additions and 0 deletions
|
@ -12,10 +12,13 @@ import java.text.ParsePosition;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ibm.icu.impl.ICUCache;
|
||||
import com.ibm.icu.impl.ICUDebug;
|
||||
|
@ -167,6 +170,26 @@ public class Currency extends MeasureUnit implements Serializable {
|
|||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the set of available currencies. The returned set of currencies contains all of the
|
||||
* available currencies, including obsolete ones. The result set can be modified without
|
||||
* affecting the available currencies in the runtime.
|
||||
*
|
||||
* @return The set of available currencies. The returned set could be empty if there is no
|
||||
* currency data available.
|
||||
*
|
||||
* @stable ICU 49
|
||||
*/
|
||||
public static Set<Currency> getAvailableCurrencies() {
|
||||
CurrencyMetaInfo info = CurrencyMetaInfo.getInstance();
|
||||
List<String> list = info.currencies(CurrencyFilter.all());
|
||||
HashSet<Currency> resultSet = new HashSet<Currency>(list.size());
|
||||
for (String code : list) {
|
||||
resultSet.add(new Currency(code));
|
||||
}
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
private static final String EUR_STR = "EUR";
|
||||
private static final ICUCache<ULocale, String> currencyCodeCache = new SimpleCache<ULocale, String>();
|
||||
|
||||
|
@ -400,6 +423,29 @@ public class Currency extends MeasureUnit implements Serializable {
|
|||
return isoCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ISO 4217 numeric code for this currency object.
|
||||
* <p>Note: If the ISO 4217 numeric code is not assigned for the currency or
|
||||
* the currency is unknown, this method returns 0.</p>
|
||||
* @return The ISO 4217 numeric code of this currency.
|
||||
* @stable ICU 49
|
||||
*/
|
||||
public int getNumericCode() {
|
||||
int code = 0;
|
||||
try {
|
||||
UResourceBundle bundle = UResourceBundle.getBundleInstance(
|
||||
ICUResourceBundle.ICU_BASE_NAME,
|
||||
"currencyNumericCodes",
|
||||
ICUResourceBundle.ICU_DATA_CLASS_LOADER);
|
||||
UResourceBundle codeMap = bundle.get("codeMap");
|
||||
UResourceBundle numCode = codeMap.get(isoCode);
|
||||
code = numCode.getInt();
|
||||
} catch (MissingResourceException e) {
|
||||
// fall through
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience and compatibility override of getName that
|
||||
* requests the symbol name for the default <code>DISPLAY</code> locale.
|
||||
|
@ -533,6 +579,41 @@ public class Currency extends MeasureUnit implements Serializable {
|
|||
return names.getPluralName(isoCode, pluralCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the display name for this currency in the default locale.
|
||||
* If the resource data for the default locale contains no entry for this currency,
|
||||
* then the ISO 4217 code is returned.
|
||||
* <p>
|
||||
* Note: This method was added for JDK compatibility support and equivalent to
|
||||
* <code>getName(Locale.getDefault(), LONG_NAME, null)</code>.
|
||||
*
|
||||
* @return The display name of this currency
|
||||
* @see #getDisplayName(Locale)
|
||||
* @see #getName(Locale, int, boolean[])
|
||||
* @stable ICU 49
|
||||
*/
|
||||
public String getDisplayName() {
|
||||
return getName(Locale.getDefault(), LONG_NAME, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the display name for this currency in the given locale.
|
||||
* If the resource data for the given locale contains no entry for this currency,
|
||||
* then the ISO 4217 code is returned.
|
||||
* <p>
|
||||
* Note: This method was added for JDK compatibility support and equivalent to
|
||||
* <code>getName(locale, LONG_NAME, null)</code>.
|
||||
*
|
||||
* @param locale locale in which to display currency
|
||||
* @return The display name of this currency for the specified locale
|
||||
* @see #getDisplayName(Locale)
|
||||
* @see #getName(Locale, int, boolean[])
|
||||
* @stable ICU 49
|
||||
*/
|
||||
public String getDisplayName(Locale locale) {
|
||||
return getName(locale, LONG_NAME, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to parse the given string as a currency, either as a
|
||||
* display name in the given locale, or as a 3-letter ISO 4217
|
||||
|
|
|
@ -522,4 +522,70 @@ public class CurrencyTest extends TestFmwk {
|
|||
logln("IllegalArgumentException, because lower range is after upper range");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for getAvailableCurrencies()
|
||||
*/
|
||||
public void TestGetAvailableCurrencies() {
|
||||
Set<Currency> avail1 = Currency.getAvailableCurrencies();
|
||||
|
||||
// returned set must be modifiable - add one more currency
|
||||
avail1.add(Currency.getInstance("ZZZ")); // ZZZ is not defined by ISO 4217
|
||||
|
||||
Set<Currency> avail2 = Currency.getAvailableCurrencies();
|
||||
assertTrue("avail1 does not contain all currencies in avail2", avail1.containsAll(avail2));
|
||||
assertTrue("avail1 must have one more currency", (avail1.size() - avail2.size() == 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for getNumericCode()
|
||||
*/
|
||||
public void TestGetNumericCode() {
|
||||
final Object[][] NUMCODE_TESTDATA = {
|
||||
{"USD", 840},
|
||||
{"Usd", 840}, /* mixed casing */
|
||||
{"EUR", 978},
|
||||
{"JPY", 392},
|
||||
{"XFU", 0}, /* XFU: no numeric code */
|
||||
{"ZZZ", 0}, /* ZZZ: undefined ISO currency code */
|
||||
};
|
||||
|
||||
for (Object[] data : NUMCODE_TESTDATA) {
|
||||
Currency cur = Currency.getInstance((String)data[0]);
|
||||
int numCode = cur.getNumericCode();
|
||||
int expected = ((Integer)data[1]).intValue();
|
||||
if (numCode != expected) {
|
||||
errln("FAIL: getNumericCode returned " + numCode + " for "
|
||||
+ cur.getCurrencyCode() + " - expected: " + expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for getDisplayName()
|
||||
*/
|
||||
public void TestGetDisplayName() {
|
||||
final String[][] DISPNAME_TESTDATA = {
|
||||
{"USD", "US Dollar"},
|
||||
{"EUR", "Euro"},
|
||||
{"JPY", "Japanese Yen"},
|
||||
};
|
||||
|
||||
Locale defLocale = Locale.getDefault();
|
||||
Locale jaJP = new Locale("ja", "JP");
|
||||
Locale root = new Locale("");
|
||||
|
||||
for (String[] data : DISPNAME_TESTDATA) {
|
||||
Currency cur = Currency.getInstance(data[0]);
|
||||
assertEquals("getDisplayName() for " + data[0], data[1], cur.getDisplayName());
|
||||
assertEquals("getDisplayName() for " + data[0] + " in locale " + defLocale, data[1], cur.getDisplayName(defLocale));
|
||||
|
||||
// ICU has localized display name for ja
|
||||
assertNotEquals("getDisplayName() for " + data[0] + " in locale " + jaJP, data[1], cur.getDisplayName(jaJP));
|
||||
|
||||
// root locale does not have any localized display names,
|
||||
// so the currency code itself should be returned
|
||||
assertEquals("getDisplayName() for " + data[0] + " in locale " + root, data[0], cur.getDisplayName(root));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue