ICU-2203 registry updates for Currency

X-SVN-Rev: 9851
This commit is contained in:
Doug Felt 2002-09-07 00:15:35 +00:00
parent d96f819545
commit fdf6867d3a
3 changed files with 184 additions and 14 deletions

View file

@ -4,8 +4,8 @@
* others. All Rights Reserved. *
*******************************************************************************
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/format/NumberFormatTest.java,v $
* $Date: 2002/07/31 19:37:06 $
* $Revision: 1.6 $
* $Date: 2002/09/07 00:15:25 $
* $Revision: 1.7 $
*
*****************************************************************************************
*/
@ -241,6 +241,49 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
}
/**
* Test the Currency registration-related API.
*/
public void TestCurrencyRegistration() {
// available locales
Locale[] locales = Currency.getAvailableLocales();
logln("available locales");
for (int i = 0; i < locales.length; ++i) {
logln("[" + i + "] " + locales[i].toString());
}
// identical instance
Currency fr0 = Currency.getInstance(Locale.FRANCE);
Currency fr1 = Currency.getInstance(Locale.FRANCE);
if (fr0 != fr1) {
errln("non-identical currencies for locale");
}
Currency us0 = Currency.getInstance(Locale.US);
// replace US with FR
Object key = Currency.register(fr0, Locale.US);
logln("FRENCH currency: " + fr0);
logln("US currency: " + us0);
// query US and get FR back
Currency us1 = Currency.getInstance(Locale.US);
if (us1 != fr0) {
errln("registry failed");
}
logln("new US currency: " + us1);
// unregister and get US back
if (!Currency.unregister(key)) {
errln("failed to unregister key: " + key);
}
Currency us2 = Currency.getInstance(Locale.US);
if (!us2.equals(us0)) {
errln("after unregister US didn't get original currency back: " + us2 + " != " + us0);
}
}
/**
* Test the Currency object handling, new as of ICU 2.2.
*/

View file

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/impl/ICULocaleService.java,v $
* $Date: 2002/08/13 23:40:52 $
* $Revision: 1.5 $
* $Date: 2002/09/07 00:15:33 $
* $Revision: 1.6 $
*
*******************************************************************************
*/
@ -20,7 +20,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;
public class ICULocaleService extends ICUService {
Locale fallbackLocale;
@ -35,6 +35,57 @@ public class ICULocaleService extends ICUService {
fallbackLocaleName = LocaleUtility.canonicalLocaleString(fallbackLocale.toString());
}
/**
* Convenience override for callers using locales.
*/
public Object get(Locale locale) {
return get(locale, null);
}
/**
* Convenience override for callers using locales.
*/
public Object get(Locale locale, Locale[] actualLocaleReturn) {
if (actualLocaleReturn == null) {
return get(locale.toString());
}
String[] temp = new String[1];
Object result = get(locale.toString(), temp);
actualLocaleReturn[0] = LocaleUtility.getLocaleFromName(temp[0]);
return result;
}
/**
* Convenience override for callers using locales.
*/
public Factory registerObject(Object obj, Locale locale) {
return registerObject(obj, locale, true);
}
/**
* Convenience override for callers using locales.
*/
public Factory registerObject(Object obj, Locale locale, boolean visible) {
return registerObject(obj, locale.toString(), visible);
}
/**
* Convenience method for callers using locales. This is the typical
* current API for this operation.
*/
public Locale[] getAvailableLocales() {
TreeSet sort = new TreeSet(String.CASE_INSENSITIVE_ORDER);
sort.addAll(getVisibleIDs());
Iterator iter = sort.iterator();
Locale[] locales = new Locale[sort.size()];
int n = 0;
while (iter.hasNext()) {
Locale loc = LocaleUtility.getLocaleFromName((String)iter.next());
locales[n++] = loc;
}
return locales;
}
/**
* A subclass of Key that implements a locale fallback mechanism.
* The first locale to search for is the locale provided by the

View file

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/util/Currency.java,v $
* $Date: 2002/08/13 23:43:27 $
* $Revision: 1.4 $
* $Date: 2002/09/07 00:15:35 $
* $Revision: 1.5 $
*
*******************************************************************************
*/
@ -19,6 +19,12 @@ import java.io.Serializable;
import com.ibm.icu.impl.ICULocaleData;
import com.ibm.icu.text.DecimalFormatSymbols;
import com.ibm.icu.impl.ICUService;
import com.ibm.icu.impl.ICUService.Key;
import com.ibm.icu.impl.ICUService.Factory;
import com.ibm.icu.impl.ICULocaleService;
import com.ibm.icu.impl.ICULocaleService.ICUResourceBundleFactory;
/**
* A class encapsulating a currency, as defined by ISO 4217. A
* <tt>Currency</tt> object can be created given a <tt>Locale</tt> or
@ -46,18 +52,34 @@ public class Currency implements Serializable {
*/
private String isoCode;
private static ICULocaleService service;
private static ICULocaleService getService() {
if (service == null) {
service = new ICULocaleService();
class CurrencyFactory extends ICUResourceBundleFactory {
CurrencyFactory() {
super ("LocaleElements", "CurrencyElements", true);
}
protected Object createFromBundle(ResourceBundle bundle, Key key) {
String[] ce = bundle.getStringArray("CurrencyElements");
return new Currency(ce[1]);
}
}
service.registerFactory(new CurrencyFactory());
}
return service;
}
/**
* Returns a currency object for the default currency in the given
* locale.
*/
public static Currency getInstance(Locale locale) {
// Look up the CurrencyElements resource for this locale.
// It contains: [0] = currency symbol, e.g. "$";
// [1] = intl. currency symbol, e.g. "USD";
// [2] = monetary decimal separator, e.g. ".".
ResourceBundle rb = ICULocaleData.getLocaleElements(locale);
String[] currencyElements = rb.getStringArray("CurrencyElements");
return getInstance(currencyElements[1]);
return (Currency)getService().get(locale);
}
/**
@ -67,6 +89,60 @@ public class Currency implements Serializable {
return new Currency(theISOCode);
}
/**
* Registers a new currency for the provided locale. The returned object
* is a key that can be used to unregister this currency object.
*/
public static Object register(Currency currency, Locale locale) {
return getService().registerObject(currency, locale);
}
/**
* Unregister the currency associated with this key (obtained from
* registerInstance).
*/
public static boolean unregister(Object registryKey) {
return getService().unregisterFactory((Factory)registryKey);
}
/**
* Return an array of the locales for which a currency
* is defined.
*/
public static Locale[] getAvailableLocales() {
return getService().getAvailableLocales();
}
/**
* Return a hashcode for this currency.
*/
public int hashCode() {
return isoCode.hashCode();
}
/**
* Return true if rhs is a Currency instance,
* is non-null, and has the same currency code.
*/
public boolean equals(Object rhs) {
try {
return equals((Currency)rhs);
}
catch (ClassCastException e) {
return false;
}
}
/**
* Return true if c is non-null and has the same currency code.
*/
public boolean equals(Currency c) {
if (c == null) return false;
if (c == this) return true;
return c.getClass() == Currency.class &&
this.isoCode.equals(c.isoCode);
}
/**
* Returns the ISO 4217 3-letter code for this currency object.
*/