ICU-10069 Take into account compatible currency symbols, e.g half-width and full-width yen symbol, when parsing currencies. Currency symbol equivalence relation hard-coded in Currency class for now.

X-SVN-Rev: 33616
This commit is contained in:
Travis Keep 2013-05-08 23:46:30 +00:00
parent 8eeb9f536d
commit 2469052770
2 changed files with 52 additions and 1 deletions

View File

@ -87,6 +87,13 @@ public class Currency extends MeasureUnit implements Serializable {
* @stable ICU 4.2
*/
public static final int PLURAL_LONG_NAME = 2;
private static final EquivalenceRelation<String> EQUIVALENT_CURRENCY_SYMBOLS =
new EquivalenceRelation<String>()
.add("\u00a5", "\uffe5")
.add("$", "\ufe69", "\uff04")
.add("\u20a8", "\u20b9")
.add("\u00a3", "\u20a4");
// begin registry stuff
@ -682,7 +689,9 @@ public class Currency extends MeasureUnit implements Serializable {
for (Map.Entry<String, String> e : names.symbolMap().entrySet()) {
String symbol = e.getKey();
String isoCode = e.getValue();
symTrie.put(symbol, new CurrencyStringInfo(isoCode, symbol));
for (String equivalentSymbol : EQUIVALENT_CURRENCY_SYMBOLS.get(symbol)) {
symTrie.put(equivalentSymbol, new CurrencyStringInfo(isoCode, symbol));
}
}
for (Map.Entry<String, String> e : names.nameMap().entrySet()) {
String name = e.getKey();
@ -881,5 +890,32 @@ public class Currency extends MeasureUnit implements Serializable {
CurrencyMetaInfo info = CurrencyMetaInfo.getInstance();
return info.currencies(filter.withTender());
}
private static final class EquivalenceRelation<T> {
private Map<T, Set<T>> data = new HashMap<T, Set<T>>();
public EquivalenceRelation<T> add(T... items) {
Set<T> group = new HashSet<T>();
for (T item : items) {
if (data.containsKey(item)) {
throw new IllegalArgumentException("All groups passed to add must be disjoint.");
}
group.add(item);
}
for (T item : items) {
data.put(item, group);
}
return this;
}
public Set<T> get(T item) {
Set<T> result = data.get(item);
if (result == null) {
return Collections.singleton(item);
}
return Collections.unmodifiableSet(result);
}
}
}
//eof

View File

@ -730,6 +730,13 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
expectCurrency(fmt, Currency.getInstance(Locale.FRANCE),
1234.56, "1 234,56 \u20AC"); // Euro
}
public void TestCompatibleCurrencies() {
NumberFormat fmt =
NumberFormat.getCurrencyInstance(Locale.US);
expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\u00A51,235"); // Yen half-width
expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\uFFE51,235"); // Yen full-wdith
}
public void TestCurrencyPatterns() {
int i;
@ -2185,6 +2192,14 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
errln("FAIL \"" + pat + "\", expected \"" + exp + "\"");
}
}
private void expectParseCurrency(NumberFormat fmt, Currency expected, String text) {
ParsePosition pos = new ParsePosition(0);
CurrencyAmount currencyAmount = fmt.parseCurrency(text, pos);
assertTrue("Parse of " + text + " should have succeeded.", pos.getIndex() > 0);
assertEquals("Currency should be correct.", expected, currencyAmount.getCurrency());
}
public void TestJB3832(){
ULocale locale = new ULocale("pt_PT@currency=PTE");