OLD | NEW |
(Empty) | |
| 1 // © 2017 and later: Unicode, Inc. and others. |
| 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
| 3 package com.ibm.icu.impl.number.parse; |
| 4 |
| 5 import java.util.Iterator; |
| 6 |
| 7 import com.ibm.icu.impl.TextTrieMap; |
| 8 import com.ibm.icu.text.UnicodeSet; |
| 9 import com.ibm.icu.util.Currency; |
| 10 import com.ibm.icu.util.Currency.CurrencyStringInfo; |
| 11 import com.ibm.icu.util.ULocale; |
| 12 |
| 13 /** |
| 14 * @author sffc |
| 15 * |
| 16 */ |
| 17 public class CurrencyTrieMatcher implements NumberParseMatcher { |
| 18 |
| 19 private final TextTrieMap<CurrencyStringInfo> longNameTrie; |
| 20 private final TextTrieMap<CurrencyStringInfo> symbolTrie; |
| 21 |
| 22 public static NumberParseMatcher getInstance(ULocale locale) { |
| 23 // TODO: Pre-compute some of the more popular locales? |
| 24 return new CurrencyTrieMatcher(locale); |
| 25 } |
| 26 |
| 27 private CurrencyTrieMatcher(ULocale locale) { |
| 28 // TODO: Currency trie does not currently have an option for case foldin
g. It defaults to use |
| 29 // case folding on long-names but not symbols. |
| 30 longNameTrie = Currency.getParsingTrie(locale, Currency.LONG_NAME); |
| 31 symbolTrie = Currency.getParsingTrie(locale, Currency.SYMBOL_NAME); |
| 32 } |
| 33 |
| 34 @Override |
| 35 public boolean match(StringSegment segment, ParsedNumber result) { |
| 36 if (result.currencyCode != null) { |
| 37 return false; |
| 38 } |
| 39 |
| 40 TextTrieMap.Output trieOutput = new TextTrieMap.Output(); |
| 41 Iterator<CurrencyStringInfo> values = longNameTrie.get(segment, 0, trieO
utput); |
| 42 if (values == null) { |
| 43 values = symbolTrie.get(segment, 0, trieOutput); |
| 44 } |
| 45 if (values != null) { |
| 46 result.currencyCode = values.next().getISOCode(); |
| 47 segment.adjustOffset(trieOutput.matchLength); |
| 48 result.setCharsConsumed(segment); |
| 49 } |
| 50 return trieOutput.partialMatch; |
| 51 } |
| 52 |
| 53 @Override |
| 54 public UnicodeSet getLeadCodePoints() { |
| 55 UnicodeSet leadCodePoints = new UnicodeSet(); |
| 56 longNameTrie.putLeadCodePoints(leadCodePoints); |
| 57 symbolTrie.putLeadCodePoints(leadCodePoints); |
| 58 return leadCodePoints.freeze(); |
| 59 } |
| 60 |
| 61 @Override |
| 62 public void postProcess(ParsedNumber result) { |
| 63 // No-op |
| 64 } |
| 65 |
| 66 @Override |
| 67 public String toString() { |
| 68 return "<CurrencyTrieMatcher>"; |
| 69 } |
| 70 } |
OLD | NEW |