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