1/* GENERATED SOURCE. DO NOT MODIFY. */ 2/* 3 ******************************************************************************* 4 * Copyright (C) 2009-2014, International Business Machines Corporation and * 5 * others. All Rights Reserved. * 6 ******************************************************************************* 7 */ 8package android.icu.text; 9 10import java.io.Serializable; 11import java.util.HashMap; 12import java.util.Iterator; 13import java.util.Locale; 14import java.util.Map; 15 16import android.icu.impl.CurrencyData; 17import android.icu.util.ICUCloneNotSupportedException; 18import android.icu.util.ULocale; 19import android.icu.util.ULocale.Category; 20 21/** 22 * This class represents the information needed by 23 * DecimalFormat to format currency plural, 24 * such as "3.00 US dollars" or "1.00 US dollar". 25 * DecimalFormat creates for itself an instance of 26 * CurrencyPluralInfo from its locale data. 27 * If you need to change any of these symbols, you can get the 28 * CurrencyPluralInfo object from your 29 * DecimalFormat and modify it. 30 * 31 * Following are the information needed for currency plural format and parse: 32 * locale information, 33 * plural rule of the locale, 34 * currency plural pattern of the locale. 35 */ 36 37public class CurrencyPluralInfo implements Cloneable, Serializable { 38 private static final long serialVersionUID = 1; 39 40 /** 41 * Create a CurrencyPluralInfo object for the default <code>FORMAT</code> locale. 42 * @see Category#FORMAT 43 */ 44 public CurrencyPluralInfo() { 45 initialize(ULocale.getDefault(Category.FORMAT)); 46 } 47 48 /** 49 * Create a CurrencyPluralInfo object for the given locale. 50 * @param locale the locale 51 */ 52 public CurrencyPluralInfo(Locale locale) { 53 initialize(ULocale.forLocale(locale)); 54 } 55 56 /** 57 * Create a CurrencyPluralInfo object for the given locale. 58 * @param locale the locale 59 */ 60 public CurrencyPluralInfo(ULocale locale) { 61 initialize(locale); 62 } 63 64 /** 65 * Gets a CurrencyPluralInfo instance for the default locale. 66 * 67 * @return A CurrencyPluralInfo instance. 68 */ 69 public static CurrencyPluralInfo getInstance() { 70 return new CurrencyPluralInfo(); 71 } 72 73 /** 74 * Gets a CurrencyPluralInfo instance for the given locale. 75 * 76 * @param locale the locale. 77 * @return A CurrencyPluralInfo instance. 78 */ 79 public static CurrencyPluralInfo getInstance(Locale locale) { 80 return new CurrencyPluralInfo(locale); 81 } 82 83 /** 84 * Gets a CurrencyPluralInfo instance for the given locale. 85 * 86 * @param locale the locale. 87 * @return A CurrencyPluralInfo instance. 88 */ 89 public static CurrencyPluralInfo getInstance(ULocale locale) { 90 return new CurrencyPluralInfo(locale); 91 } 92 93 /** 94 * Gets plural rules of this locale, used for currency plural format 95 * 96 * @return plural rule 97 */ 98 public PluralRules getPluralRules() { 99 return pluralRules; 100 } 101 102 /** 103 * Given a plural count, gets currency plural pattern of this locale, 104 * used for currency plural format 105 * 106 * @param pluralCount currency plural count 107 * @return a currency plural pattern based on plural count 108 */ 109 public String getCurrencyPluralPattern(String pluralCount) { 110 String currencyPluralPattern = pluralCountToCurrencyUnitPattern.get(pluralCount); 111 if (currencyPluralPattern == null) { 112 // fall back to "other" 113 if (!pluralCount.equals("other")) { 114 currencyPluralPattern = pluralCountToCurrencyUnitPattern.get("other"); 115 } 116 if (currencyPluralPattern == null) { 117 // no currencyUnitPatterns defined, 118 // fallback to predefined default. 119 // This should never happen when ICU resource files are 120 // available, since currencyUnitPattern of "other" is always 121 // defined in root. 122 currencyPluralPattern = defaultCurrencyPluralPattern; 123 } 124 } 125 return currencyPluralPattern; 126 } 127 128 /** 129 * Get locale 130 * 131 * @return locale 132 */ 133 public ULocale getLocale() { 134 return ulocale; 135 } 136 137 /** 138 * Set plural rules. These are initially set in the constructor based on the locale, 139 * and usually do not need to be changed. 140 * 141 * @param ruleDescription new plural rule description 142 */ 143 public void setPluralRules(String ruleDescription) { 144 pluralRules = PluralRules.createRules(ruleDescription); 145 } 146 147 /** 148 * Set currency plural patterns. These are initially set in the constructor based on the 149 * locale, and usually do not need to be changed. 150 * 151 * @param pluralCount the plural count for which the currency pattern will 152 * be overridden. 153 * @param pattern the new currency plural pattern 154 */ 155 public void setCurrencyPluralPattern(String pluralCount, String pattern) { 156 pluralCountToCurrencyUnitPattern.put(pluralCount, pattern); 157 } 158 159 /** 160 * Set locale. This also sets both the plural rules and the currency plural patterns to be 161 * the defaults for the locale. 162 * 163 * @param loc the new locale to set 164 */ 165 public void setLocale(ULocale loc) { 166 ulocale = loc; 167 initialize(loc); 168 } 169 170 /** 171 * Standard override 172 */ 173 public Object clone() { 174 try { 175 CurrencyPluralInfo other = (CurrencyPluralInfo) super.clone(); 176 // locale is immutable 177 other.ulocale = (ULocale)ulocale.clone(); 178 // plural rule is immutable 179 //other.pluralRules = pluralRules; 180 // clone content 181 //other.pluralCountToCurrencyUnitPattern = pluralCountToCurrencyUnitPattern; 182 other.pluralCountToCurrencyUnitPattern = new HashMap<String, String>(); 183 for (String pluralCount : pluralCountToCurrencyUnitPattern.keySet()) { 184 String currencyPattern = pluralCountToCurrencyUnitPattern.get(pluralCount); 185 other.pluralCountToCurrencyUnitPattern.put(pluralCount, currencyPattern); 186 } 187 return other; 188 } catch (CloneNotSupportedException e) { 189 throw new ICUCloneNotSupportedException(e); 190 } 191 } 192 193 /** 194 * Override equals 195 */ 196 public boolean equals(Object a) { 197 if (a instanceof CurrencyPluralInfo) { 198 CurrencyPluralInfo other = (CurrencyPluralInfo)a; 199 return pluralRules.equals(other.pluralRules) && 200 pluralCountToCurrencyUnitPattern.equals(other.pluralCountToCurrencyUnitPattern); 201 } 202 return false; 203 } 204 205 /** 206 * Mock implementation of hashCode(). This implementation always returns a constant 207 * value. When Java assertion is enabled, this method triggers an assertion failure. 208 * @deprecated This API is ICU internal only. 209 * @hide original deprecated declaration 210 * @hide draft / provisional / internal are hidden on Android 211 */ 212 @Deprecated 213 public int hashCode() { 214 assert false : "hashCode not designed"; 215 return 42; 216 } 217 218 /** 219 * Given a number, returns the keyword of the first rule that applies 220 * to the number. 221 * @deprecated This API is ICU internal only. 222 * @hide draft / provisional / internal are hidden on Android 223 */ 224 @Deprecated 225 String select(double number) { 226 return pluralRules.select(number); 227 } 228 229 /** 230 * Given a number, returns the keyword of the first rule that applies 231 * to the number. 232 * @deprecated This API is ICU internal only. 233 * @hide draft / provisional / internal are hidden on Android 234 */ 235 @Deprecated 236 String select(PluralRules.FixedDecimal numberInfo) { 237 return pluralRules.select(numberInfo); 238 } 239 240 /** 241 * Currency plural pattern iterator. 242 * 243 * @return a iterator on the currency plural pattern key set. 244 */ 245 Iterator<String> pluralPatternIterator() { 246 return pluralCountToCurrencyUnitPattern.keySet().iterator(); 247 } 248 249 private void initialize(ULocale uloc) { 250 ulocale = uloc; 251 pluralRules = PluralRules.forLocale(uloc); 252 setupCurrencyPluralPattern(uloc); 253 } 254 255 private void setupCurrencyPluralPattern(ULocale uloc) { 256 pluralCountToCurrencyUnitPattern = new HashMap<String, String>(); 257 258 String numberStylePattern = NumberFormat.getPattern(uloc, NumberFormat.NUMBERSTYLE); 259 // Split the number style pattern into pos and neg if applicable 260 int separatorIndex = numberStylePattern.indexOf(";"); 261 String negNumberPattern = null; 262 if (separatorIndex != -1) { 263 negNumberPattern = numberStylePattern.substring(separatorIndex + 1); 264 numberStylePattern = numberStylePattern.substring(0, separatorIndex); 265 } 266 Map<String, String> map = CurrencyData.provider.getInstance(uloc, true).getUnitPatterns(); 267 for (Map.Entry<String, String> e : map.entrySet()) { 268 String pluralCount = e.getKey(); 269 String pattern = e.getValue(); 270 271 // replace {0} with numberStylePattern 272 // and {1} with triple currency sign 273 String patternWithNumber = pattern.replace("{0}", numberStylePattern); 274 String patternWithCurrencySign = patternWithNumber.replace("{1}", tripleCurrencyStr); 275 if (separatorIndex != -1) { 276 String negPattern = pattern; 277 String negWithNumber = negPattern.replace("{0}", negNumberPattern); 278 String negWithCurrSign = negWithNumber.replace("{1}", tripleCurrencyStr); 279 StringBuilder posNegPatterns = new StringBuilder(patternWithCurrencySign); 280 posNegPatterns.append(";"); 281 posNegPatterns.append(negWithCurrSign); 282 patternWithCurrencySign = posNegPatterns.toString(); 283 } 284 pluralCountToCurrencyUnitPattern.put(pluralCount, patternWithCurrencySign); 285 } 286 } 287 288 289 //-------------------- private data member --------------------- 290 // 291 // triple currency sign char array 292 private static final char[] tripleCurrencySign = {0xA4, 0xA4, 0xA4}; 293 // triple currency sign string 294 private static final String tripleCurrencyStr = new String(tripleCurrencySign); 295 296 // default currency plural pattern char array 297 private static final char[] defaultCurrencyPluralPatternChar = {0, '.', '#', '#', ' ', 0xA4, 0xA4, 0xA4}; 298 // default currency plural pattern string 299 private static final String defaultCurrencyPluralPattern = new String(defaultCurrencyPluralPatternChar); 300 301 // map from plural count to currency plural pattern, for example 302 // one (plural count) --> {0} {1} (currency plural pattern, 303 // in which {0} is the amount number, and {1} is the currency plural name). 304 private Map<String, String> pluralCountToCurrencyUnitPattern = null; 305 306 /* 307 * The plural rule is used to format currency plural name, 308 * for example: "3.00 US Dollars". 309 * If there are 3 currency signs in the currency pattern, 310 * the 3 currency signs will be replaced by the currency plural name. 311 */ 312 private PluralRules pluralRules = null; 313 314 // locale 315 private ULocale ulocale = null; 316} 317