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