diff --git a/icu4j/src/com/ibm/icu/dev/test/util/CurrencyTest.java b/icu4j/src/com/ibm/icu/dev/test/util/CurrencyTest.java index 88d05a5db3..291c795b31 100644 --- a/icu4j/src/com/ibm/icu/dev/test/util/CurrencyTest.java +++ b/icu4j/src/com/ibm/icu/dev/test/util/CurrencyTest.java @@ -15,6 +15,7 @@ import com.ibm.icu.text.DecimalFormatSymbols; import com.ibm.icu.util.*; import java.util.Locale; +import java.util.Date; /** * @test @@ -175,137 +176,217 @@ public class CurrencyTest extends TestFmwk { } } - public void TestAvailableCurrencyCodes() - { -/* - // local Variables - String[] currency; + public void TestAvailableCurrencyCodes() + { + // local Variables + String[] currency; - // Cycle through historical currencies - currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(-630720000000L)); - if (currency != null) - { - errln("got currency"); - } - else - { - errln("no currency"); - } - if (!"AMD".equals(currency[0])) - { - errln("didn't return AMD for eo_AM returned: " + currency); - } - currency = Currency.getInstance(new ULocale("eo_AM"), new Date(0L)); - result = currency.getCurrencyCode(); - if (!"SUR".equals(result)) - { - errln("didn't return SUR for eo_AM returned: " + result); - } + // Cycle through historical currencies + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(-630720000000L)); // pre 1961 + if (currency != null) + { + errln("FAIL: didn't return null for eo_AM"); + } - currency = Currency.getInstance(new ULocale("eo_AM"), new Date(693792000000L)); - result = currency.getCurrencyCode(); - if (!"RUR".equals(result)) - { - errln("didn't return RUR for eo_AM returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(0L)); // 1970 + if (currency.length != 1) + { + errln("FAIL: didn't return 1 for eo_AM returned: " + currency.length); + } + if (!"SUR".equals(currency[0])) + { + errln("didn't return SUR for eo_AM returned: " + currency[0]); + } - currency = Currency.getInstance(new ULocale("eo_AM"), new Date(977616000000L)); - result = currency.getCurrencyCode(); - if (!"AMD".equals(result)) - { - errln("didn't return AMD for eo_AM returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(693792000000L)); // 1992 + if (currency.length != 1) + { + errln("FAIL: didn't return 1 for eo_AM returned: " + currency.length); + } + if (!"RUR".equals(currency[0])) + { + errln("didn't return RUR for eo_AM returned: " + currency[0]); + } - // Locale AD has multiple currencies at once - currency = Currency.getInstance(new ULocale("eo_AD"), new Date(977616000000L)); - result = currency.getCurrencyCode(); - if (!"EUR".equals(result)) - { - errln("didn't return EUR for eo_AD returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(977616000000L)); // post 1993 + if (currency.length != 1) + { + errln("FAIL: didn't return 1 for eo_AM returned: " + currency.length); + } + if (!"AMD".equals(currency[0])) + { + errln("didn't return AMD for eo_AM returned: " + currency[0]); + } - currency = Currency.getInstance(new ULocale("eo_AD"), new Date(0L)); - result = currency.getCurrencyCode(); - if (!"ESP".equals(result)) - { - errln("didn't return ESP for eo_AD returned: " + result); - } + // Locale AD has multiple currencies at once + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AD"), new Date(977616000000L)); // 2001 + if (currency.length != 4) + { + errln("FAIL: didn't return 4 for eo_AD returned: " + currency.length); + } + if (!"EUR".equals(currency[0])) + { + errln("didn't return EUR for eo_AD returned: " + currency[0]); + } + if (!"ESP".equals(currency[1])) + { + errln("didn't return ESP for eo_AD returned: " + currency[1]); + } + if (!"FRF".equals(currency[2])) + { + errln("didn't return FRF for eo_AD returned: " + currency[2]); + } + if (!"ADP".equals(currency[3])) + { + errln("didn't return ADP for eo_AD returned: " + currency[3]); + } - // Locale UA has gap between years 1994 - 1996 - currency = Currency.getInstance(new ULocale("eo_UA"), new Date(788400000000L)); - result = currency.getCurrencyCode(); - if (!"UAH".equals(result)) - { - errln("didn't return UAH for eo_UA returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AD"), new Date(0L)); // 1970 + if (currency.length != 3) + { + errln("FAIL: didn't return 3 for eo_AD returned: " + currency.length); + } + if (!"ESP".equals(currency[0])) + { + errln("didn't return ESP for eo_AD returned: " + currency[0]); + } + if (!"FRF".equals(currency[1])) + { + errln("didn't return FRF for eo_AD returned: " + currency[1]); + } + if (!"ADP".equals(currency[2])) + { + errln("didn't return ADP for eo_AD returned: " + currency[2]); + } - // Cycle through historical currencies - currency = Currency.getInstance(new ULocale("eo_AO"), new Date(977616000000L)); - result = currency.getCurrencyCode(); - if (!"AOA".equals(result)) - { - errln("didn't return AOA for eo_AO returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AD"), new Date(-630720000000L)); // 1950 + if (currency.length != 2) + { + errln("FAIL: didn't return 2 for eo_AD returned: " + currency.length); + } + if (!"ESP".equals(currency[0])) + { + errln("didn't return ESP for eo_AD returned: " + currency[0]); + } + if (!"ADP".equals(currency[1])) + { + errln("didn't return ADP for eo_AD returned: " + currency[1]); + } - currency = Currency.getInstance(new ULocale("eo_AO"), new Date(819936000000L)); - result = currency.getCurrencyCode(); - if (!"AOR".equals(result)) - { - errln("didn't return AOR for eo_AO returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AD"), new Date(-2207520000000L)); // 1900 + if (currency.length != 1) + { + errln("FAIL: didn't return 1 for eo_AD returned: " + currency.length); + } + if (!"ESP".equals(currency[0])) + { + errln("didn't return ESP for eo_AD returned: " + currency[0]); + } - currency = Currency.getInstance(new ULocale("eo_AO"), new Date(662256000000L)); - result = currency.getCurrencyCode(); - if (!"AON".equals(result)) - { - errln("didn't return AON for eo_AO returned: " + result); - } + // Locale UA has gap between years 1994 - 1996 + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_UA"), new Date(788400000000L)); + if (currency != null) + { + errln("FAIL: didn't return null for eo_UA"); + } - currency = Currency.getInstance(new ULocale("eo_AO"), new Date(315360000000L)); - result = currency.getCurrencyCode(); - if (!"AOK".equals(result)) - { - errln("didn't return AOK for eo_AO returned: " + result); - } + // Test for bogus locale + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_QQ"), new Date(0L)); + if (currency != null) + { + errln("FAIL: didn't return null for eo_QQ"); + } - currency = Currency.getInstance(new ULocale("eo_AO"), new Date(0L)); - result = currency.getCurrencyCode(); - if (!"AOA".equals(result)) - { - errln("didn't return AOA for eo_AO returned: " + result); - } + // Cycle through historical currencies + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(977616000000L)); // 2001 + if (currency.length != 1) + { + errln("FAIL: didn't return 1 for eo_AO returned: " + currency.length); + } + if (!"AOA".equals(currency[0])) + { + errln("didn't return AOA for eo_AO returned: " + currency[0]); + } - // Test EURO support - currency = Currency.getInstance(new ULocale("en_US"), new Date(System.currentTimeMillis())); - result = currency.getCurrencyCode(); - if (!"USD".equals(result)) - { - errln("didn't return USD for en_US returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(819936000000L)); // 1996 + if (currency.length != 2) + { + errln("FAIL: didn't return 2 for eo_AO returned: " + currency.length); + } + if (!"AOR".equals(currency[0])) + { + errln("didn't return AOR for eo_AO returned: " + currency[0]); + } + if (!"AON".equals(currency[1])) + { + errln("didn't return AON for eo_AO returned: " + currency[1]); + } - currency = Currency.getInstance(new ULocale("en_US_PREEURO"), new Date(System.currentTimeMillis())); - result = currency.getCurrencyCode(); - if (!"USD".equals(result)) - { - errln("didn't return USD for en_US_PREEURO returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(662256000000L)); // + if (currency.length != 2) + { + errln("FAIL: didn't return 2 for eo_AO returned: " + currency.length); + } + if (!"AON".equals(currency[0])) + { + errln("didn't return AON for eo_AO returned: " + currency[0]); + } + if (!"AOK".equals(currency[1])) + { + errln("didn't return AOK for eo_AO returned: " + currency[1]); + } - currency = Currency.getInstance(new ULocale("en_US_Q"), new Date(System.currentTimeMillis())); - result = currency.getCurrencyCode(); - if (!"USD".equals(result)) - { - errln("didn't return USD for en_US_Q returned: " + result); - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(315360000000L)); // 1980 + if (currency.length != 1) + { + errln("FAIL: didn't return 1 for eo_AO returned: " + currency.length); + } + if (!"AOK".equals(currency[0])) + { + errln("didn't return AOK for eo_AO returned: " + currency[0]); + } - // non-existant locale - currency = Currency.getAvailableCurrencyCodes(new ULocale("en_QQ"), new Date(System.currentTimeMillis())); - if (currency != null) - { - errln("didn't return NULL for en_QQ"); - } -*/ - } + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(0L)); // 1970 + if (currency != null) + { + errln("FAIL: didn't return null for eo_AO"); + } + // Test with currency keyword override + currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_DE@currency=DEM"), new Date(977616000000L)); // 2001 + if (currency.length != 2) + { + errln("FAIL: didn't return 2 for eo_DE@currency=DEM returned: " + currency.length); + } + if (!"EUR".equals(currency[0])) + { + errln("didn't return EUR for eo_DE@currency=DEM returned: " + currency[0]); + } + if (!"DEM".equals(currency[1])) + { + errln("didn't return DEM for eo_DE@currency=DEM returned: " + currency[1]); + } + + // Test Euro Support + currency = Currency.getAvailableCurrencyCodes(new ULocale("en_US"), new Date(System.currentTimeMillis())); + if (!"USD".equals(currency[0])) + { + errln("didn't return USD for en_US returned: " + currency[0]); + } + + currency = Currency.getAvailableCurrencyCodes(new ULocale("en_US_PREEURO"), new Date(System.currentTimeMillis())); + if (!"USD".equals(currency[0])) + { + errln("didn't return USD for en_US_PREEURO returned: " + currency[0]); + } + + currency = Currency.getAvailableCurrencyCodes(new ULocale("en_US_Q"), new Date(System.currentTimeMillis())); + if (!"USD".equals(currency[0])) + { + errln("didn't return USD for en_US_Q returned: " + currency[0]); + } + + } public void TestDeprecatedCurrencyFormat() { // bug 5952 diff --git a/icu4j/src/com/ibm/icu/util/Currency.java b/icu4j/src/com/ibm/icu/util/Currency.java index 8d69360ae0..2a398c8027 100644 --- a/icu4j/src/com/ibm/icu/util/Currency.java +++ b/icu4j/src/com/ibm/icu/util/Currency.java @@ -12,6 +12,7 @@ import java.text.ParsePosition; import java.util.Locale; import java.util.MissingResourceException; import java.util.Date; +import java.util.Vector; import com.ibm.icu.impl.ICUDebug; import com.ibm.icu.impl.ICUResourceBundle; @@ -134,9 +135,10 @@ public class Currency extends MeasureUnit implements Serializable { { // local variables String country = loc.getCountry(); - //String variant = loc.getVariant(); long dateL = d.getTime(); long mask = 4294967295L; + + Vector currCodeVector = new Vector(); // Get supplementalData ICUResourceBundle bundle = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, @@ -157,99 +159,52 @@ public class Currency extends MeasureUnit implements Serializable { UResourceBundle cm = bundle.get("CurrencyMap"); UResourceBundle countryArray = cm.get(country); - // First pass, get a count of valid currencies - int currCount = 0; - for (int i = 0; i < countryArray.getSize(); i++) - { - // get the currency resource - UResourceBundle currencyReq = countryArray.get(i); + // Get valid currencies + for (int i = 0; i < countryArray.getSize(); i++) + { + // get the currency resource + UResourceBundle currencyReq = countryArray.get(i); + String curriso = null; + curriso = currencyReq.getString("id"); - // get the from date - long fromDate = 0; - UResourceBundle fromRes = currencyReq.get("from"); - int[] fromArray = fromRes.getIntVector(); - fromDate = (long)fromArray[0] << 32; - fromDate |= ((long)fromArray[1] & mask); + // get the from date + long fromDate = 0; + UResourceBundle fromRes = currencyReq.get("from"); + int[] fromArray = fromRes.getIntVector(); + fromDate = (long)fromArray[0] << 32; + fromDate |= ((long)fromArray[1] & mask); - // get the to date and check the date range - if (currencyReq.getSize() > 2) - { - long toDate = 0; - UResourceBundle toRes = currencyReq.get("to"); - int[] toArray = toRes.getIntVector(); - toDate = (long)toArray[0] << 32; - toDate |= ((long)toArray[1] & mask); + // get the to date and check the date range + if (currencyReq.getSize() > 2) + { + long toDate = 0; + UResourceBundle toRes = currencyReq.get("to"); + int[] toArray = toRes.getIntVector(); + toDate = (long)toArray[0] << 32; + toDate |= ((long)toArray[1] & mask); - if ((fromDate <= dateL) && (dateL < toDate)) - { - currCount++; - } - } - else - { - if (fromDate <= dateL) - { - currCount++; - } - } + if ((fromDate <= dateL) && (dateL < toDate)) + { + currCodeVector.addElement(curriso); + } + } + else + { + if (fromDate <= dateL) + { + currCodeVector.addElement(curriso); + } + } - } // end For loop + } // end For loop - // Allocate array to return - if (currCount == 0) - { - return null; - } + // return the String array if we have matches + currCodeVector.trimToSize(); + if (currCodeVector.size() != 0) + { + return ((String[])currCodeVector.toArray(new String[0])); + } - String[] currCodes = new String[currCount]; - int currIndex = 0; - - // Second pass, get the actual currency codes - for (int i = 0; i < countryArray.getSize(); i++) - { - // get the currency resource - UResourceBundle currencyReq = countryArray.get(i); - String curriso = null; - curriso = currencyReq.getString("id"); - - // get the from date - long fromDate = 0; - UResourceBundle fromRes = currencyReq.get("from"); - int[] fromArray = fromRes.getIntVector(); - fromDate = (long)fromArray[0] << 32; - fromDate |= ((long)fromArray[1] & mask); - - // get the to date and check the date range - if (currencyReq.getSize() > 2) - { - long toDate = 0; - UResourceBundle toRes = currencyReq.get("to"); - int[] toArray = toRes.getIntVector(); - toDate = (long)toArray[0] << 32; - toDate |= ((long)toArray[1] & mask); - - if ((fromDate <= dateL) && (dateL < toDate)) - { - currCodes[currIndex] = new String(curriso); - currIndex++; - } - } - else - { - if (fromDate <= dateL) - { - currCodes[currIndex] = new String(curriso); - currIndex++; - } - } - - } // end For loop - - // Process the matching ids. Due to gaps in the windows of time - // for valid currencies, it is possible that no currency is valid - // for the given time. It is possible that we will return multiple - // currencies for the given time. - return currCodes; } catch (MissingResourceException ex) {