/projects/eclipse_SDK-3.7.1/plugins/com.ibm.icu.source_4.4.2.v20110208/com/ibm/icu/impl/ICUCurrencyDisplayInfoProvider.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 279 lines · 235 code · 29 blank · 15 comment · 66 complexity · 3fbdf2868ede9fabb83f728c1bda05c2 MD5 · raw file

  1. /*
  2. *******************************************************************************
  3. * Copyright (C) 2009-2010, International Business Machines Corporation and *
  4. * others. All Rights Reserved. *
  5. *******************************************************************************
  6. */
  7. package com.ibm.icu.impl;
  8. import java.lang.ref.SoftReference;
  9. import java.util.Collections;
  10. import java.util.HashMap;
  11. import java.util.HashSet;
  12. import java.util.Map;
  13. import java.util.Set;
  14. import java.util.TreeMap;
  15. import com.ibm.icu.impl.CurrencyData.CurrencyDisplayInfo;
  16. import com.ibm.icu.impl.CurrencyData.CurrencyDisplayInfoProvider;
  17. import com.ibm.icu.impl.CurrencyData.CurrencyFormatInfo;
  18. import com.ibm.icu.impl.CurrencyData.CurrencySpacingInfo;
  19. import com.ibm.icu.util.ULocale;
  20. import com.ibm.icu.util.UResourceBundle;
  21. public class ICUCurrencyDisplayInfoProvider implements CurrencyDisplayInfoProvider {
  22. public ICUCurrencyDisplayInfoProvider() {
  23. }
  24. public CurrencyDisplayInfo getInstance(ULocale locale, boolean withFallback) {
  25. ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
  26. ICUResourceBundle.ICU_CURR_BASE_NAME, locale);
  27. if (!withFallback) {
  28. int status = rb.getLoadingStatus();
  29. if (status == ICUResourceBundle.FROM_DEFAULT || status == ICUResourceBundle.FROM_ROOT) {
  30. return CurrencyData.DefaultInfo.getWithFallback(false);
  31. }
  32. }
  33. return new ICUCurrencyDisplayInfo(rb, withFallback);
  34. }
  35. public boolean hasData() {
  36. return true;
  37. }
  38. static class ICUCurrencyDisplayInfo extends CurrencyDisplayInfo {
  39. private final boolean fallback;
  40. private final ICUResourceBundle rb;
  41. private final ICUResourceBundle currencies;
  42. private final ICUResourceBundle plurals;
  43. private SoftReference<Map<String, String>> _symbolMapRef;
  44. private SoftReference<Map<String, String>> _nameMapRef;
  45. public ICUCurrencyDisplayInfo(ICUResourceBundle rb, boolean fallback) {
  46. this.fallback = fallback;
  47. this.rb = rb;
  48. this.currencies = rb.findTopLevel("Currencies");
  49. this.plurals = rb.findTopLevel("CurrencyPlurals");
  50. }
  51. @Override
  52. public ULocale getLocale() {
  53. return rb.getULocale();
  54. }
  55. @Override
  56. public String getName(String isoCode) {
  57. return getName(isoCode, false);
  58. }
  59. @Override
  60. public String getSymbol(String isoCode) {
  61. return getName(isoCode, true);
  62. }
  63. private String getName(String isoCode, boolean symbolName) {
  64. if (currencies != null) {
  65. ICUResourceBundle result = currencies.findWithFallback(isoCode);
  66. if (result != null) {
  67. if (!fallback) {
  68. int status = result.getLoadingStatus();
  69. if (status == ICUResourceBundle.FROM_DEFAULT ||
  70. status == ICUResourceBundle.FROM_ROOT) {
  71. return null;
  72. }
  73. }
  74. return result.getString(symbolName ? 0 : 1);
  75. }
  76. }
  77. return fallback ? isoCode : null;
  78. }
  79. @Override
  80. public String getPluralName(String isoCode, String pluralKey ) {
  81. // See http://unicode.org/reports/tr35/#Currencies, especially the fallback rule.
  82. if (plurals != null) {
  83. ICUResourceBundle pluralsBundle = plurals.findWithFallback(isoCode);
  84. if (pluralsBundle != null) {
  85. ICUResourceBundle pluralBundle = pluralsBundle.findWithFallback(pluralKey);
  86. if (pluralBundle == null) {
  87. if (!fallback) {
  88. return null;
  89. }
  90. pluralBundle = pluralsBundle.findWithFallback("other");
  91. if (pluralBundle == null) {
  92. return getName(isoCode);
  93. }
  94. }
  95. return pluralBundle.getString();
  96. }
  97. }
  98. return fallback ? getName(isoCode) : null;
  99. }
  100. @Override
  101. public Map<String, String> symbolMap() {
  102. Map<String, String> map = _symbolMapRef == null ? null : _symbolMapRef.get();
  103. if (map == null) {
  104. map = _createSymbolMap();
  105. // atomic and idempotent
  106. _symbolMapRef = new SoftReference<Map<String, String>>(map);
  107. }
  108. return map;
  109. }
  110. @Override
  111. public Map<String, String> nameMap() {
  112. Map<String, String> map = _nameMapRef == null ? null : _nameMapRef.get();
  113. if (map == null) {
  114. map = _createNameMap();
  115. // atomic and idempotent
  116. _nameMapRef = new SoftReference<Map<String, String>>(map);
  117. }
  118. return map;
  119. }
  120. @Override
  121. public Map<String, String> getUnitPatterns() {
  122. Map<String, String> result = new HashMap<String, String>();
  123. ULocale locale = rb.getULocale();
  124. for (;locale != null; locale = locale.getFallback()) {
  125. ICUResourceBundle r = (ICUResourceBundle) UResourceBundle.getBundleInstance(
  126. ICUResourceBundle.ICU_CURR_BASE_NAME, locale);
  127. if (r == null) {
  128. continue;
  129. }
  130. ICUResourceBundle cr = r.findWithFallback("CurrencyUnitPatterns");
  131. if (cr == null) {
  132. continue;
  133. }
  134. for (int index = 0, size = cr.getSize(); index < size; ++index) {
  135. ICUResourceBundle b = (ICUResourceBundle) cr.get(index);
  136. String key = b.getKey();
  137. if (result.containsKey(key)) {
  138. continue;
  139. }
  140. result.put(key, b.getString());
  141. }
  142. }
  143. // Default result is the empty map. Callers who require a pattern will have to
  144. // supply a default.
  145. return Collections.unmodifiableMap(result);
  146. }
  147. @Override
  148. public CurrencyFormatInfo getFormatInfo(String isoCode) {
  149. ICUResourceBundle crb = currencies.findWithFallback(isoCode);
  150. if (crb != null && crb.getSize() > 2) {
  151. crb = crb.at(2);
  152. if (crb != null) {
  153. String pattern = crb.getString(0);
  154. char separator = crb.getString(1).charAt(0);
  155. char groupingSeparator = crb.getString(2).charAt(0);
  156. return new CurrencyFormatInfo(pattern, separator, groupingSeparator);
  157. }
  158. }
  159. return null;
  160. }
  161. @Override
  162. public CurrencySpacingInfo getSpacingInfo() {
  163. ICUResourceBundle srb = rb.findWithFallback("currencySpacing");
  164. if (srb != null) {
  165. ICUResourceBundle brb = srb.findWithFallback("beforeCurrency");
  166. ICUResourceBundle arb = srb.findWithFallback("afterCurrency");
  167. if (brb != null && brb != null) {
  168. String beforeCurrencyMatch = brb.findWithFallback("currencyMatch").getString();
  169. String beforeContextMatch = brb.findWithFallback("surroundingMatch").getString();
  170. String beforeInsert = brb.findWithFallback("insertBetween").getString();
  171. String afterCurrencyMatch = arb.findWithFallback("currencyMatch").getString();
  172. String afterContextMatch = arb.findWithFallback("surroundingMatch").getString();
  173. String afterInsert = arb.findWithFallback("insertBetween").getString();
  174. return new CurrencySpacingInfo(
  175. beforeCurrencyMatch, beforeContextMatch, beforeInsert,
  176. afterCurrencyMatch, afterContextMatch, afterInsert);
  177. }
  178. }
  179. return fallback ? CurrencySpacingInfo.DEFAULT : null;
  180. }
  181. private Map<String, String> _createSymbolMap() {
  182. Map<String, String> result = new HashMap<String, String>();
  183. for (ULocale locale = rb.getULocale(); locale != null; locale = locale.getFallback()) {
  184. ICUResourceBundle bundle = (ICUResourceBundle)
  185. UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_CURR_BASE_NAME, locale);
  186. ICUResourceBundle curr = bundle.findTopLevel("Currencies");
  187. if (curr == null) {
  188. continue;
  189. }
  190. for (int i = 0; i < curr.getSize(); ++i) {
  191. ICUResourceBundle item = curr.at(i);
  192. String isoCode = item.getKey();
  193. if (!result.containsKey(isoCode)) {
  194. // put the code itself
  195. result.put(isoCode, isoCode);
  196. // 0 == symbol element
  197. String symbol = item.getString(0);
  198. result.put(symbol, isoCode);
  199. }
  200. }
  201. }
  202. return Collections.unmodifiableMap(result);
  203. }
  204. private Map<String, String> _createNameMap() {
  205. // ignore case variants
  206. Map<String, String> result = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
  207. Set<String> visited = new HashSet<String>();
  208. Map<String, Set<String>> visitedPlurals = new HashMap<String, Set<String>>();
  209. for (ULocale locale = rb.getULocale(); locale != null; locale = locale.getFallback()) {
  210. ICUResourceBundle bundle = (ICUResourceBundle)
  211. UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_CURR_BASE_NAME, locale);
  212. ICUResourceBundle curr = bundle.findTopLevel("Currencies");
  213. if (curr != null) {
  214. for (int i = 0; i < curr.getSize(); ++i) {
  215. ICUResourceBundle item = curr.at(i);
  216. String isoCode = item.getKey();
  217. if (!visited.contains(isoCode)) {
  218. visited.add(isoCode);
  219. // 1 == name element
  220. String name = item.getString(1);
  221. result.put(name, isoCode);
  222. }
  223. }
  224. }
  225. ICUResourceBundle plurals = bundle.findTopLevel("CurrencyPlurals");
  226. if (plurals != null) {
  227. for (int i = 0; i < plurals.getSize(); ++i) {
  228. ICUResourceBundle item = plurals.at(i);
  229. String isoCode = item.getKey();
  230. Set<String> pluralSet = visitedPlurals.get(isoCode);
  231. if (pluralSet == null) {
  232. pluralSet = new HashSet<String>();
  233. visitedPlurals.put(isoCode, pluralSet);
  234. }
  235. for (int j = 0; j < item.getSize(); ++j) {
  236. ICUResourceBundle plural = item.at(j);
  237. String pluralType = plural.getKey();
  238. if (!pluralSet.contains(pluralType)) {
  239. String pluralName = plural.getString();
  240. result.put(pluralName, isoCode);
  241. pluralSet.add(pluralType);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. return Collections.unmodifiableMap(result);
  248. }
  249. }
  250. }