ICU-10344 add new field & factory method for currency CASH/STANDARD Usage

X-SVN-Rev: 35833
This commit is contained in:
Tom Zhang 2014-06-09 19:30:22 +00:00
parent fad96371c9
commit c90efd6e59
6 changed files with 2328 additions and 2334 deletions

View File

@ -11,7 +11,10 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import com.ibm.icu.impl.Grego;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.Currency.CurrencyUsage;
import com.ibm.icu.util.GregorianCalendar;
import com.ibm.icu.util.TimeZone;
/**
* Provides information about currencies that is not specific to a locale.
@ -532,11 +535,23 @@ public class CurrencyMetaInfo {
/**
* Returns the CurrencyDigits for the currency code.
* This is equivalent to currencyDigits(isoCode, CurrencyUsage.STANDARD);
* @param isoCode the currency code
* @return the CurrencyDigits
* @stable ICU 4.4
*/
public CurrencyDigits currencyDigits(String isoCode) {
return currencyDigits(isoCode, CurrencyUsage.STANDARD);
}
/**
* Returns the CurrencyDigits for the currency code with Context Usage.
* @param isoCode the currency code
* @param currencyUsage the currency usage
* @return the CurrencyDigits
* @draft ICU 54
*/
public CurrencyDigits currencyDigits(String isoCode, CurrencyUsage currencyUsage) {
return defaultDigits;
}
@ -565,7 +580,11 @@ public class CurrencyMetaInfo {
if (date == Long.MAX_VALUE || date == Long.MIN_VALUE) {
return null;
}
return Grego.timeToString(date);
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeZone(TimeZone.getTimeZone("GMT"));
gc.setTimeInMillis(date);
return "" + gc.get(Calendar.YEAR) + '-' + (gc.get(Calendar.MONTH) + 1) + '-' +
gc.get(Calendar.DAY_OF_MONTH);
}
private static String debugString(Object o) {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -89,6 +89,28 @@ public class Currency extends MeasureUnit {
.add("\u20a8", "\u20b9")
.add("\u00a3", "\u20a4");
/**
* Currency Usage used for Decimal Format
* @draft ICU 54
*/
public enum CurrencyUsage{
/**
* a setting to specify currency usage which determines currency digit and rounding
* for official purpose, for example: "50.00 NT$"
* @draft ICU 54
* @provisional This API might change or be removed in a future release.
*/
STANDARD,
/**
* a setting to specify currency usage which determines currency digit and rounding
* for cash purpose, for example: "50 NT$"
* @draft ICU 54
* @provisional This API might change or be removed in a future release.
*/
CASH
}
// begin registry stuff
// shim for service code
@ -729,25 +751,50 @@ public class Currency extends MeasureUnit {
/**
* Returns the number of the number of fraction digits that should
* be displayed for this currency.
* This is equivalent to getDefaultFractionDigits(CurrencyUsage.STANDARD);
* @return a non-negative number of fraction digits to be
* displayed
* @stable ICU 2.2
*/
public int getDefaultFractionDigits() {
return getDefaultFractionDigits(CurrencyUsage.STANDARD);
}
/**
* Returns the number of the number of fraction digits that should
* be displayed for this currency with Usage.
* @param Usage the usage of currency(Standard or Cash)
* @return a non-negative number of fraction digits to be
* displayed
* @draft ICU 54
*/
public int getDefaultFractionDigits(CurrencyUsage Usage) {
CurrencyMetaInfo info = CurrencyMetaInfo.getInstance();
CurrencyDigits digits = info.currencyDigits(subType);
CurrencyDigits digits = info.currencyDigits(subType, Usage);
return digits.fractionDigits;
}
/**
* Returns the rounding increment for this currency, or 0.0 if no
* rounding is done by this currency.
* This is equivalent to getRoundingIncrement(CurrencyUsage.STANDARD);
* @return the non-negative rounding increment, or 0.0 if none
* @stable ICU 2.2
*/
public double getRoundingIncrement() {
return getRoundingIncrement(CurrencyUsage.STANDARD);
}
/**
* Returns the rounding increment for this currency, or 0.0 if no
* rounding is done by this currency with the Usage.
* @param Usage the usage of currency(Standard or Cash)
* @return the non-negative rounding increment, or 0.0 if none
* @draft ICU 54
*/
public double getRoundingIncrement(CurrencyUsage Usage) {
CurrencyMetaInfo info = CurrencyMetaInfo.getInstance();
CurrencyDigits digits = info.currencyDigits(subType);
CurrencyDigits digits = info.currencyDigits(subType, Usage);
int data1 = digits.roundingIncrement;
@ -764,7 +811,7 @@ public class Currency extends MeasureUnit {
return 0.0;
}
// Return data[1] / 10^(data[0]). The only actual rounding data,
// Return data[1] / 10^(data[0]). The only actual rounding data,
// as of this writing, is CHF { 2, 25 }.
return (double) data1 / POW10[data0];
}

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2009-2013, International Business Machines Corporation and *
* Copyright (C) 2009-2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -13,6 +13,7 @@ import java.util.List;
import java.util.Set;
import com.ibm.icu.text.CurrencyMetaInfo;
import com.ibm.icu.util.Currency.CurrencyUsage;
/**
* ICU's currency meta info data.
@ -46,14 +47,25 @@ public class ICUCurrencyMetaInfo extends CurrencyMetaInfo {
@Override
public CurrencyDigits currencyDigits(String isoCode) {
return currencyDigits(isoCode, CurrencyUsage.STANDARD);
}
@Override
public CurrencyDigits currencyDigits(String isoCode, CurrencyUsage currencyPurpose) {
ICUResourceBundle b = digitInfo.findWithFallback(isoCode);
if (b == null) {
b = digitInfo.findWithFallback("DEFAULT");
}
int[] data = b.getIntVector();
return new CurrencyDigits(data[0], data[1]);
if (currencyPurpose == CurrencyUsage.CASH) {
return new CurrencyDigits(data[2], data[3]);
} else if (currencyPurpose == CurrencyUsage.STANDARD) {
return new CurrencyDigits(data[0], data[1]);
} else {
return new CurrencyDigits(data[0], data[1]);
}
}
private <T> List<T> collect(Collector<T> collector, CurrencyFilter filter) {
// We rely on the fact that the data lists the regions in order, and the
// priorities in order within region. This means we don't need

View File

@ -23,7 +23,6 @@ import java.util.Locale;
import java.util.Set;
import com.ibm.icu.dev.test.TestUtil;
import com.ibm.icu.impl.ICUConfig;
import com.ibm.icu.impl.LocaleUtility;
import com.ibm.icu.impl.data.ResourceReader;
import com.ibm.icu.impl.data.TokenIterator;
@ -2735,7 +2734,7 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
*/
public void TestGetInstance() {
// Tests "public final static NumberFormat getInstance(int style)"
int maxStyle = NumberFormat.ACCOUNTINGCURRENCYSTYLE;
int maxStyle = NumberFormat.CASHCURRENCYSTYLE;
int[] invalid_cases = { NumberFormat.NUMBERSTYLE - 1, NumberFormat.NUMBERSTYLE - 2,
maxStyle + 1, maxStyle + 2 };
@ -3025,24 +3024,26 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
expect(fmt, "2\uFF61345.67", 2345.67);
// Ticket#7128
// Ticket#7218
//
// Lenient separator parsing is enabled by default.
// A space character below is interpreted as a
// group separator, even ',' is used as grouping
// separator in the symbols.
sym.setGroupingSeparator(',');
fmt.setDecimalFormatSymbols(sym);
String skipExtSepParse = ICUConfig.get("com.ibm.icu.text.DecimalFormat.SkipExtendedSeparatorParsing", "false");
if (skipExtSepParse.equals("true")) {
// When the property SkipExtendedSeparatorParsing is true,
// DecimalFormat does not use the extended equivalent separator
// data and only uses the one in DecimalFormatSymbols.
expect(fmt, "23 456", 23);
} else {
// Lenient separator parsing is enabled by default.
// A space character below is interpreted as a
// group separator, even ',' is used as grouping
// separator in the symbols.
expect(fmt, "12 345", 12345);
}
expect(fmt, "12 345", 12345);
// When the property SkipExtendedSeparatorParsing is true,
// DecimalFormat does not use the extended equivalent separator
// data and only uses the one in DecimalFormatSymbols.
System.setProperty("com.ibm.icu.text.DecimalFormat.SkipExtendedSeparatorParsing", "true");
expect(fmt, "23 456", 23);
// Set the configuration back to the default
System.setProperty("com.ibm.icu.text.DecimalFormat.SkipExtendedSeparatorParsing", "false");
}
/*
@ -3608,4 +3609,72 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
expect(acfmt, num, fmt, rt);
}
}
public void TestCurrencyUsage() {
// the 1st one is checking setter/getter, while the 2nd one checks for getInstance
// compare the Currency and Currency Cash Digits
for(int i=0; i<2; i++){
String original_expected = "NT$123.57";
DecimalFormat custom = null;
if(i == 0){
custom = (DecimalFormat)DecimalFormat.getInstance(new ULocale("en_US@currency=TWD"), DecimalFormat.CURRENCYSTYLE);
String original = custom.format(123.567);
assertEquals("Test Currency Context", original_expected, original);
// test the getter
assertEquals("Test Currency Context Purpose", custom.getCurrencyUsage(), Currency.CurrencyUsage.STANDARD);
custom.setCurrencyUsage(Currency.CurrencyUsage.CASH);
assertEquals("Test Currency Context Purpose", custom.getCurrencyUsage(), Currency.CurrencyUsage.CASH);
}else{
custom = (DecimalFormat)DecimalFormat.getInstance(new ULocale("en_US@currency=TWD"), DecimalFormat.CASHCURRENCYSTYLE);
// test the getter
assertEquals("Test Currency Context Purpose", custom.getCurrencyUsage(), Currency.CurrencyUsage.CASH);
}
String cash_currency = custom.format(123.567);
String cash_currency_expected = "NT$124";
assertEquals("Test Currency Context", cash_currency_expected, cash_currency);
}
// the 1st one is checking setter/getter, while the 2nd one checks for getInstance
// compare the Currency and Currency Cash Rounding
for(int i=0; i<2; i++){
String original_rounding_expected = "CA$123.57";
DecimalFormat fmt = null;
if(i == 0){
fmt = (DecimalFormat)DecimalFormat.getInstance(new ULocale("en_US@currency=CAD"), DecimalFormat.CURRENCYSTYLE);
String original_rounding = fmt.format(123.566);
assertEquals("Test Currency Context", original_rounding_expected, original_rounding);
fmt.setCurrencyUsage(Currency.CurrencyUsage.CASH);
}else{
fmt = (DecimalFormat)DecimalFormat.getInstance(new ULocale("en_US@currency=CAD"), DecimalFormat.CASHCURRENCYSTYLE);
}
String cash_rounding_currency = fmt.format(123.567);
String cash__rounding_currency_expected = "CA$123.55";
assertEquals("Test Currency Context", cash__rounding_currency_expected, cash_rounding_currency);
}
// the 1st one is checking setter/getter, while the 2nd one checks for getInstance
// Test the currency change
for(int i=0; i<2; i++){
DecimalFormat fmt2 = null;
if(i == 1){
fmt2 = (DecimalFormat)NumberFormat.getInstance(new ULocale("en_US@currency=JPY"), NumberFormat.CURRENCYSTYLE);
fmt2.setCurrencyUsage(Currency.CurrencyUsage.CASH);
}else{
fmt2 = (DecimalFormat)NumberFormat.getInstance(new ULocale("en_US@currency=JPY"), NumberFormat.CASHCURRENCYSTYLE);
}
fmt2.setCurrency(Currency.getInstance("TWD"));
String TWD_changed = fmt2.format(123.567);
String TWD_changed_expected = "NT$124";
assertEquals("Test Currency Context", TWD_changed_expected, TWD_changed);
}
}
}