]> granicus.if.org Git - icu/commitdiff
ICU-10069 Take into account compatible currency symbols, e.g half-width and full...
authorTravis Keep <keep94@gmail.com>
Wed, 8 May 2013 23:46:30 +0000 (23:46 +0000)
committerTravis Keep <keep94@gmail.com>
Wed, 8 May 2013 23:46:30 +0000 (23:46 +0000)
X-SVN-Rev: 33616

icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index 38c744098d8e030b46da6a49c5d28baf6b709ed3..04b2eb86512bc194637a3d80cdecdc5324f4b098 100644 (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
index df06f53e6017204197930adc74b1761409b840df..7db02cd422a48bcf257b2339e98c2452d266ba87 100644 (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");