]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/text/CurrencyPluralInfo.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / core / src / com / ibm / icu / text / CurrencyPluralInfo.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2009-2011, 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      * Given a number, returns the keyword of the first rule that applies
224      * to the number.
225      */
226     String select(double number) {
227         return pluralRules.select(number);
228     }
229
230     /**
231      * Currency plural pattern iterator.
232      *
233      * @return a iterator on the currency plural pattern key set.
234      */
235     Iterator<String> pluralPatternIterator() {
236         return pluralCountToCurrencyUnitPattern.keySet().iterator();
237     }
238
239     private void initialize(ULocale uloc) {
240         ulocale = uloc;
241         pluralRules = PluralRules.forLocale(uloc);
242         setupCurrencyPluralPattern(uloc);
243     }
244
245     private void setupCurrencyPluralPattern(ULocale uloc) {
246         pluralCountToCurrencyUnitPattern = new HashMap<String, String>();
247         
248         String numberStylePattern = NumberFormat.getPattern(uloc, NumberFormat.NUMBERSTYLE);
249         // Split the number style pattern into pos and neg if applicable
250         int separatorIndex = numberStylePattern.indexOf(";");
251         String negNumberPattern = null;
252         if (separatorIndex != -1) {
253             negNumberPattern = numberStylePattern.substring(separatorIndex + 1);
254             numberStylePattern = numberStylePattern.substring(0, separatorIndex);
255         }
256         Map<String, String> map = CurrencyData.provider.getInstance(uloc, true).getUnitPatterns();
257         for (Map.Entry<String, String> e : map.entrySet()) {
258             String pluralCount = e.getKey();
259             String pattern = e.getValue();
260             
261             // replace {0} with numberStylePattern
262             // and {1} with triple currency sign
263             String patternWithNumber = pattern.replace("{0}", numberStylePattern);
264             String patternWithCurrencySign = patternWithNumber.replace("{1}", tripleCurrencyStr);
265             if (separatorIndex != -1) {
266                 String negPattern = pattern;
267                 String negWithNumber = negPattern.replace("{0}", negNumberPattern);
268                 String negWithCurrSign = negWithNumber.replace("{1}", tripleCurrencyStr);
269                 StringBuilder posNegPatterns = new StringBuilder(patternWithCurrencySign);
270                 posNegPatterns.append(";");
271                 posNegPatterns.append(negWithCurrSign);
272                 patternWithCurrencySign = posNegPatterns.toString();
273             }
274             pluralCountToCurrencyUnitPattern.put(pluralCount, patternWithCurrencySign);
275         }
276     }
277
278
279     //-------------------- private data member ---------------------
280     //
281     // triple currency sign char array
282     private static final char[] tripleCurrencySign = {0xA4, 0xA4, 0xA4};
283     // triple currency sign string
284     private static final String tripleCurrencyStr = new String(tripleCurrencySign);
285
286     // default currency plural pattern char array
287     private static final char[] defaultCurrencyPluralPatternChar = {0, '.', '#', '#', ' ', 0xA4, 0xA4, 0xA4};
288     // default currency plural pattern string
289     private static final String defaultCurrencyPluralPattern = new String(defaultCurrencyPluralPatternChar);
290
291     // map from plural count to currency plural pattern, for example
292     // one (plural count) --> {0} {1} (currency plural pattern,
293     // in which {0} is the amount number, and {1} is the currency plural name).
294     private Map<String, String> pluralCountToCurrencyUnitPattern = null;
295
296     /*
297      * The plural rule is used to format currency plural name,
298      * for example: "3.00 US Dollars".
299      * If there are 3 currency signs in the currency pattern,
300      * the 3 currency signs will be replaced by the currency plural name.
301      */
302     private PluralRules pluralRules = null;
303
304     // locale
305     private ULocale ulocale = null;
306 }