]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/text/NumberFormat.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / text / NumberFormat.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 package com.ibm.icu.text;\r
9 \r
10 import java.io.IOException;\r
11 import java.io.InvalidObjectException;\r
12 import java.io.ObjectInputStream;\r
13 import java.io.ObjectOutputStream;\r
14 import java.math.BigInteger;\r
15 import java.text.FieldPosition;\r
16 import java.text.Format;\r
17 import java.text.ParseException;\r
18 import java.text.ParsePosition;\r
19 import java.util.Collections;\r
20 import java.util.Locale;\r
21 import java.util.MissingResourceException;\r
22 import java.util.Set;\r
23 \r
24 import com.ibm.icu.impl.ICUResourceBundle;\r
25 import com.ibm.icu.util.Currency;\r
26 import com.ibm.icu.util.CurrencyAmount;\r
27 import com.ibm.icu.util.ULocale;\r
28 import com.ibm.icu.util.UResourceBundle;\r
29 \r
30 /**\r
31  * {@icuenhanced java.text.NumberFormat}.{@icu _usage_}\r
32  *\r
33  * <code>NumberFormat</code> is the abstract base class for all number\r
34  * formats. This class provides the interface for formatting and parsing\r
35  * numbers. <code>NumberFormat</code> also provides methods for determining\r
36  * which locales have number formats, and what their names are.\r
37  *\r
38  * <code>NumberFormat</code> helps you to format and parse numbers for any locale.\r
39  * Your code can be completely independent of the locale conventions for\r
40  * decimal points, thousands-separators, or even the particular decimal\r
41  * digits used, or whether the number format is even decimal.\r
42  *\r
43  * <p>\r
44  * To format a number for the current Locale, use one of the factory\r
45  * class methods:\r
46  * <blockquote>\r
47  * <pre>\r
48  *  myString = NumberFormat.getInstance().format(myNumber);\r
49  * </pre>\r
50  * </blockquote>\r
51  * If you are formatting multiple numbers, it is\r
52  * more efficient to get the format and use it multiple times so that\r
53  * the system doesn't have to fetch the information about the local\r
54  * language and country conventions multiple times.\r
55  * <blockquote>\r
56  * <pre>\r
57  * NumberFormat nf = NumberFormat.getInstance();\r
58  * for (int i = 0; i < a.length; ++i) {\r
59  *     output.println(nf.format(myNumber[i]) + "; ");\r
60  * }\r
61  * </pre>\r
62  * </blockquote>\r
63  * To format a number for a different Locale, specify it in the\r
64  * call to <code>getInstance</code>.\r
65  * <blockquote>\r
66  * <pre>\r
67  * NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);\r
68  * </pre>\r
69  * </blockquote>\r
70  * You can also use a <code>NumberFormat</code> to parse numbers:\r
71  * <blockquote>\r
72  * <pre>\r
73  * myNumber = nf.parse(myString);\r
74  * </pre>\r
75  * </blockquote>\r
76  * Use <code>getInstance</code> or <code>getNumberInstance</code> to get the\r
77  * normal number format. Use <code>getIntegerInstance</code> to get an\r
78  * integer number format. Use <code>getCurrencyInstance</code> to get the\r
79  * currency number format. And use <code>getPercentInstance</code> to get a\r
80  * format for displaying percentages. With this format, a fraction like\r
81  * 0.53 is displayed as 53%.\r
82  *\r
83  * <p>\r
84  * Starting from ICU 4.2, you can use getInstance() by passing in a 'style'\r
85  * as parameter to get the correct instance.\r
86  * For example,\r
87  * use getInstance(...NUMBERSTYLE) to get the normal number format,\r
88  * getInstance(...PERCENTSTYLE) to get a format for displaying percentage,\r
89  * getInstance(...SCIENTIFICSTYLE) to get a format for displaying scientific number,\r
90  * getInstance(...INTEGERSTYLE) to get an integer number format,\r
91  * getInstance(...CURRENCYSTYLE) to get the currency number format,\r
92  * in which the currency is represented by its symbol, for example, "$3.00".\r
93  * getInstance(...ISOCURRENCYSTYLE)  to get the currency number format,\r
94  * in which the currency is represented by its ISO code, for example "USD3.00".\r
95  * getInstance(...PLURALCURRENCYSTYLE) to get the currency number format,\r
96  * in which the currency is represented by its full name in plural format,\r
97  * for example, "3.00 US dollars" or "1.00 US dollar".\r
98  *\r
99  *\r
100  * <p>\r
101  * You can also control the display of numbers with such methods as\r
102  * <code>setMinimumFractionDigits</code>.\r
103  * If you want even more control over the format or parsing,\r
104  * or want to give your users more control,\r
105  * you can try casting the <code>NumberFormat</code> you get from the factory methods\r
106  * to a <code>DecimalFormat</code>. This will work for the vast majority\r
107  * of locales; just remember to put it in a <code>try</code> block in case you\r
108  * encounter an unusual one.\r
109  *\r
110  * <p>\r
111  * NumberFormat is designed such that some controls\r
112  * work for formatting and others work for parsing.  The following is\r
113  * the detailed description for each these control methods,\r
114  * <p>\r
115  * setParseIntegerOnly : only affects parsing, e.g.\r
116  * if true,  "3456.78" -> 3456 (and leaves the parse position just after '6')\r
117  * if false, "3456.78" -> 3456.78 (and leaves the parse position just after '8')\r
118  * This is independent of formatting.  If you want to not show a decimal point\r
119  * where there might be no digits after the decimal point, use\r
120  * setDecimalSeparatorAlwaysShown on DecimalFormat.\r
121  * <p>\r
122  * You can also use forms of the <code>parse</code> and <code>format</code>\r
123  * methods with <code>ParsePosition</code> and <code>FieldPosition</code> to\r
124  * allow you to:\r
125  * <ul>\r
126  * <li> progressively parse through pieces of a string\r
127  * <li> align the decimal point and other areas\r
128  * </ul>\r
129  * For example, you can align numbers in two ways:\r
130  * <ol>\r
131  * <li> If you are using a monospaced font with spacing for alignment,\r
132  *      you can pass the <code>FieldPosition</code> in your format call, with\r
133  *      <code>field</code> = <code>INTEGER_FIELD</code>. On output,\r
134  *      <code>getEndIndex</code> will be set to the offset between the\r
135  *      last character of the integer and the decimal. Add\r
136  *      (desiredSpaceCount - getEndIndex) spaces at the front of the string.\r
137  *\r
138  * <li> If you are using proportional fonts,\r
139  *      instead of padding with spaces, measure the width\r
140  *      of the string in pixels from the start to <code>getEndIndex</code>.\r
141  *      Then move the pen by\r
142  *      (desiredPixelWidth - widthToAlignmentPoint) before drawing the text.\r
143  *      It also works where there is no decimal, but possibly additional\r
144  *      characters at the end, e.g., with parentheses in negative\r
145  *      numbers: "(12)" for -12.\r
146  * </ol>\r
147  *\r
148  * <h4>Synchronization</h4>\r
149  * <p>\r
150  * Number formats are generally not synchronized. It is recommended to create\r
151  * separate format instances for each thread. If multiple threads access a format\r
152  * concurrently, it must be synchronized externally.\r
153  * <p>\r
154  *\r
155  * <h4>DecimalFormat</h4>\r
156  * <p>DecimalFormat is the concrete implementation of NumberFormat, and the\r
157  * NumberFormat API is essentially an abstraction from DecimalFormat's API.\r
158  * Refer to DecimalFormat for more information about this API.</p>\r
159  *\r
160  * see          DecimalFormat\r
161  * see          java.text.ChoiceFormat\r
162  * @author       Mark Davis\r
163  * @author       Helena Shih\r
164  * @author       Alan Liu\r
165  * @stable ICU 2.0\r
166  */\r
167 public abstract class NumberFormat extends UFormat {\r
168 \r
169     /**\r
170      * {@icu} Constant to specify normal number style of format.\r
171      * @stable ICU 4.2\r
172      */\r
173     public static final int NUMBERSTYLE = 0;\r
174     /**\r
175      * {@icu} Constant to specify currency style of format which uses currency symbol\r
176      * to represent currency, for example: "$3.00".\r
177      * @stable ICU 4.2\r
178      */\r
179     public static final int CURRENCYSTYLE = 1;\r
180     /**\r
181      * {@icu} Constant to specify a style of format to display percent.\r
182      * @stable ICU 4.2\r
183      */\r
184     public static final int PERCENTSTYLE = 2;\r
185     /**\r
186      * {@icu} Constant to specify a style of format to display scientific number.\r
187      * @stable ICU 4.2\r
188      */\r
189     public static final int SCIENTIFICSTYLE = 3;\r
190     /**\r
191      * {@icu} Constant to specify a integer number style format.\r
192      * @stable ICU 4.2\r
193      */\r
194     public static final int INTEGERSTYLE = 4;\r
195     /**\r
196      * {@icu} Constant to specify currency style of format which uses currency\r
197      * ISO code to represent currency, for example: "USD3.00".\r
198      * @stable ICU 4.2\r
199      */\r
200     public static final int ISOCURRENCYSTYLE = 5;\r
201     /**\r
202      * {@icu} Constant to specify currency style of format which uses currency\r
203      * long name with plural format to represent currency, for example,\r
204      * "3.00 US Dollars".\r
205      * @stable ICU 4.2\r
206      */\r
207     public static final int PLURALCURRENCYSTYLE = 6;\r
208 \r
209     /**\r
210      * Field constant used to construct a FieldPosition object. Signifies that\r
211      * the position of the integer part of a formatted number should be returned.\r
212      * @see java.text.FieldPosition\r
213      * @stable ICU 2.0\r
214      */\r
215     public static final int INTEGER_FIELD = 0;\r
216 \r
217     /**\r
218      * Field constant used to construct a FieldPosition object. Signifies that\r
219      * the position of the fraction part of a formatted number should be returned.\r
220      * @see java.text.FieldPosition\r
221      * @stable ICU 2.0\r
222      */\r
223     public static final int FRACTION_FIELD = 1;\r
224 \r
225     /**\r
226      * Formats a number and appends the resulting text to the given string buffer.\r
227      * {@icunote} recognizes <code>BigInteger</code>\r
228      * and <code>BigDecimal</code> objects.\r
229      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
230      * @stable ICU 2.0\r
231      */\r
232     public StringBuffer format(Object number,\r
233                                StringBuffer toAppendTo,\r
234                                FieldPosition pos) {\r
235         if (number instanceof Long) {\r
236             return format(((Long)number).longValue(), toAppendTo, pos);\r
237         } else if (number instanceof BigInteger) {\r
238             return format((BigInteger) number, toAppendTo, pos);\r
239         } else if (number instanceof java.math.BigDecimal) {\r
240             return format((java.math.BigDecimal) number, toAppendTo, pos);\r
241         } else if (number instanceof com.ibm.icu.math.BigDecimal) {\r
242             return format((com.ibm.icu.math.BigDecimal) number, toAppendTo, pos);\r
243         } else if (number instanceof CurrencyAmount) {\r
244             return format((CurrencyAmount)number, toAppendTo, pos);\r
245         } else if (number instanceof Number) {\r
246             return format(((Number)number).doubleValue(), toAppendTo, pos);\r
247         } else {\r
248             throw new IllegalArgumentException("Cannot format given Object as a Number");\r
249         }\r
250     }\r
251 \r
252     /**\r
253      * Parses text from a string to produce a number.\r
254      * @param source the String to parse\r
255      * @param parsePosition the position at which to start the parse\r
256      * @return the parsed number, or null\r
257      * @see java.text.NumberFormat#parseObject(String, ParsePosition)\r
258      * @stable ICU 2.0\r
259      */\r
260     public final Object parseObject(String source,\r
261                                     ParsePosition parsePosition) {\r
262         return parse(source, parsePosition);\r
263     }\r
264 \r
265     /**\r
266      * Specialization of format.\r
267      * @see java.text.Format#format(Object)\r
268      * @stable ICU 2.0\r
269      */\r
270     public final String format(double number) {\r
271         return format(number,new StringBuffer(),\r
272                       new FieldPosition(0)).toString();\r
273     }\r
274 \r
275     /**\r
276      * Specialization of format.\r
277      * @see java.text.Format#format(Object)\r
278      * @stable ICU 2.0\r
279      */\r
280     public final String format(long number) {\r
281         StringBuffer buf = new StringBuffer(19);\r
282         FieldPosition pos = new FieldPosition(0);\r
283         format(number, buf, pos);\r
284         return buf.toString();\r
285     }\r
286 \r
287     /**\r
288      * {@icu} Convenience method to format a BigInteger.\r
289      * @stable ICU 2.0\r
290      */\r
291     public final String format(BigInteger number) {\r
292         return format(number, new StringBuffer(),\r
293                       new FieldPosition(0)).toString();\r
294     }\r
295 \r
296     /**\r
297      * Convenience method to format a BigDecimal.\r
298      * @stable ICU 2.0\r
299      */\r
300     public final String format(java.math.BigDecimal number) {\r
301         return format(number, new StringBuffer(),\r
302                       new FieldPosition(0)).toString();\r
303     }\r
304 \r
305     /**\r
306      * {@icu} Convenience method to format an ICU BigDecimal.\r
307      * @stable ICU 2.0\r
308      */\r
309     public final String format(com.ibm.icu.math.BigDecimal number) {\r
310         return format(number, new StringBuffer(),\r
311                       new FieldPosition(0)).toString();\r
312     }\r
313 \r
314     /**\r
315      * {@icu} Convenience method to format a CurrencyAmount.\r
316      * @stable ICU 3.0\r
317      */\r
318     public final String format(CurrencyAmount currAmt) {\r
319         return format(currAmt, new StringBuffer(),\r
320                       new FieldPosition(0)).toString();\r
321     }\r
322 \r
323     /**\r
324      * Specialization of format.\r
325      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
326      * @stable ICU 2.0\r
327      */\r
328     public abstract StringBuffer format(double number,\r
329                                         StringBuffer toAppendTo,\r
330                                         FieldPosition pos);\r
331 \r
332     /**\r
333      * Specialization of format.\r
334      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
335      * @stable ICU 2.0\r
336      */\r
337     public abstract StringBuffer format(long number,\r
338                                         StringBuffer toAppendTo,\r
339                                         FieldPosition pos);\r
340     /**\r
341      * {@icu} Formats a BigInteger. Specialization of format.\r
342      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
343      * @stable ICU 2.0\r
344      */\r
345     public abstract StringBuffer format(BigInteger number,\r
346                                         StringBuffer toAppendTo,\r
347                                         FieldPosition pos);\r
348     /**\r
349      * {@icu} Formats a BigDecimal. Specialization of format.\r
350      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
351      * @stable ICU 2.0\r
352      */\r
353     public abstract StringBuffer format(java.math.BigDecimal number,\r
354                                         StringBuffer toAppendTo,\r
355                                         FieldPosition pos);\r
356     /**\r
357      * {@icu} Formats an ICU BigDecimal. Specialization of format.\r
358      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
359      * @stable ICU 2.0\r
360      */\r
361     public abstract StringBuffer format(com.ibm.icu.math.BigDecimal number,\r
362                                         StringBuffer toAppendTo,\r
363                                         FieldPosition pos);\r
364     /**\r
365      * {@icu} Formats a CurrencyAmount. Specialization of format.\r
366      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
367      * @stable ICU 3.0\r
368      */\r
369     public StringBuffer format(CurrencyAmount currAmt,\r
370                                StringBuffer toAppendTo,\r
371                                FieldPosition pos) {\r
372         // Default implementation -- subclasses may override\r
373         Currency save = getCurrency(), curr = currAmt.getCurrency();\r
374         boolean same = curr.equals(save);\r
375         if (!same) setCurrency(curr);\r
376         format(currAmt.getNumber(), toAppendTo, pos);\r
377         if (!same) setCurrency(save);\r
378         return toAppendTo;\r
379     }\r
380 \r
381     /**\r
382      * Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,\r
383      * Long.MAX_VALUE] and with no decimals), otherwise a Double.\r
384      * If IntegerOnly is set, will stop at a decimal\r
385      * point (or equivalent; e.g., for rational numbers "1 2/3", will stop\r
386      * after the 1).\r
387      * Does not throw an exception; if no object can be parsed, index is\r
388      * unchanged!\r
389      * @see #isParseIntegerOnly\r
390      * @see java.text.Format#parseObject(String, ParsePosition)\r
391      * @stable ICU 2.0\r
392      */\r
393     public abstract Number parse(String text, ParsePosition parsePosition);\r
394 \r
395     /**\r
396      * Parses text from the beginning of the given string to produce a number.\r
397      * The method might not use the entire text of the given string.\r
398      *\r
399      * @param text A String whose beginning should be parsed.\r
400      * @return A Number parsed from the string.\r
401      * @throws ParseException if the beginning of the specified string\r
402      * cannot be parsed.\r
403      * @see #format\r
404      * @stable ICU 2.0\r
405      */\r
406     //Bug 4375399 [Richard/GCL]\r
407     public Number parse(String text) throws ParseException {\r
408         ParsePosition parsePosition = new ParsePosition(0);\r
409         Number result = parse(text, parsePosition);\r
410         if (parsePosition.getIndex() == 0) {\r
411             throw new ParseException("Unparseable number: \"" + text + '"',\r
412                                      parsePosition.getErrorIndex());\r
413         }\r
414         return result;\r
415     }\r
416 \r
417     /**\r
418      * Parses text from the given string as a CurrencyAmount.  Unlike\r
419      * the parse() method, this method will attempt to parse a generic\r
420      * currency name, searching for a match of this object's locale's\r
421      * currency display names, or for a 3-letter ISO currency code.\r
422      * This method will fail if this format is not a currency format,\r
423      * that is, if it does not contain the currency pattern symbol\r
424      * (U+00A4) in its prefix or suffix.\r
425      *\r
426      * @param text the string to parse\r
427      * @param pos input-output position; on input, the position within\r
428      * text to match; must have 0 <= pos.getIndex() < text.length();\r
429      * on output, the position after the last matched character. If\r
430      * the parse fails, the position in unchanged upon output.\r
431      * @return a CurrencyAmount, or null upon failure\r
432      */\r
433     CurrencyAmount parseCurrency(String text, ParsePosition pos) {\r
434         ///CLOVER:OFF\r
435         // Default implementation only -- subclasses should override\r
436         Number n = parse(text, pos);\r
437         return n == null ? null : new CurrencyAmount(n, getEffectiveCurrency());\r
438         ///CLOVER:ON\r
439     }\r
440 \r
441     /**\r
442      * Returns true if this format will parse numbers as integers only.\r
443      * For example in the English locale, with ParseIntegerOnly true, the\r
444      * string "1234." would be parsed as the integer value 1234 and parsing\r
445      * would stop at the "." character.  The decimal separator accepted\r
446      * by the parse operation is locale-dependent and determined by the\r
447      * subclass.\r
448      * @return true if this will parse integers only\r
449      * @stable ICU 2.0\r
450      */\r
451     public boolean isParseIntegerOnly() {\r
452         return parseIntegerOnly;\r
453     }\r
454 \r
455     /**\r
456      * Sets whether or not numbers should be parsed as integers only.\r
457      * @param value true if this should parse integers only\r
458      * @see #isParseIntegerOnly\r
459      * @stable ICU 2.0\r
460      */\r
461     public void setParseIntegerOnly(boolean value) {\r
462         parseIntegerOnly = value;\r
463     }\r
464 \r
465     /**\r
466      * {@icu} Sets whether strict parsing is in effect.  When this is true, the\r
467      * following conditions cause a parse failure (examples use the pattern "#,##0.#"):<ul>\r
468      * <li>Leading zeros<br>\r
469      * '00', '0123' fail the parse, but '0' and '0.001' pass</li>\r
470      * <li>Leading or doubled grouping separators<br>\r
471      * ',123' and '1,,234" fail</li>\r
472      * <li>Groups of incorrect length when grouping is used<br>\r
473      * '1,23' and '1234,567' fail, but '1234' passes</li>\r
474      * <li>Grouping separators used in numbers followed by exponents<br>\r
475      * '1,234E5' fails, but '1234E5' and '1,234E' pass ('E' is not an exponent when\r
476      * not followed by a number)</li>\r
477      * </ul>\r
478      * When strict parsing is off, leading zeros and all grouping separators are ignored.\r
479      * This is the default behavior.\r
480      * @param value True to enable strict parsing.  Default is false.\r
481      * @see #isParseStrict\r
482      * @stable ICU 3.6\r
483      */\r
484     public void setParseStrict(boolean value) {\r
485         parseStrict = value;\r
486     }\r
487 \r
488     /**\r
489      * {@icu} Returns whether strict parsing is in effect.\r
490      * @return true if strict parsing is in effect\r
491      * @see #setParseStrict\r
492      * @stable ICU 3.6\r
493      */\r
494     public boolean isParseStrict() {\r
495         return parseStrict;\r
496     }\r
497 \r
498     //============== Locale Stuff =====================\r
499 \r
500     /**\r
501      * Returns the default number format for the current default locale.\r
502      * The default format is one of the styles provided by the other\r
503      * factory methods: getNumberInstance, getIntegerInstance,\r
504      * getCurrencyInstance or getPercentInstance.\r
505      * Exactly which one is locale-dependent.\r
506      * @stable ICU 2.0\r
507      */\r
508     //Bug 4408066 [Richard/GCL]\r
509     public final static NumberFormat getInstance() {\r
510         return getInstance(ULocale.getDefault(), NUMBERSTYLE);\r
511     }\r
512 \r
513     /**\r
514      * Returns the default number format for the specified locale.\r
515      * The default format is one of the styles provided by the other\r
516      * factory methods: getNumberInstance, getCurrencyInstance or getPercentInstance.\r
517      * Exactly which one is locale-dependent.\r
518      * @stable ICU 2.0\r
519      */\r
520     public static NumberFormat getInstance(Locale inLocale) {\r
521         return getInstance(ULocale.forLocale(inLocale), NUMBERSTYLE);\r
522     }\r
523 \r
524     /**\r
525      * {@icu} Returns the default number format for the specified locale.\r
526      * The default format is one of the styles provided by the other\r
527      * factory methods: getNumberInstance, getCurrencyInstance or getPercentInstance.\r
528      * Exactly which one is locale-dependent.\r
529      * @stable ICU 3.2\r
530      */\r
531     public static NumberFormat getInstance(ULocale inLocale) {\r
532         return getInstance(inLocale, NUMBERSTYLE);\r
533     }\r
534 \r
535     /**\r
536      * {@icu} Returns a specific style number format for default locale.\r
537      * @param style  number format style\r
538      * @stable ICU 4.2\r
539      */\r
540     public final static NumberFormat getInstance(int style) {\r
541         return getInstance(ULocale.getDefault(), style);\r
542     }\r
543 \r
544     /**\r
545      * {@icu} Returns a specific style number format for a specific locale.\r
546      * @param inLocale  the specific locale.\r
547      * @param style     number format style\r
548      * @stable ICU 4.2\r
549      */\r
550     public static NumberFormat getInstance(Locale inLocale, int style) {\r
551         return getInstance(ULocale.forLocale(inLocale), style);\r
552     }\r
553 \r
554 \r
555     /**\r
556      * Returns a general-purpose number format for the current default locale.\r
557      * @stable ICU 2.0\r
558      */\r
559     public final static NumberFormat getNumberInstance() {\r
560         return getInstance(ULocale.getDefault(), NUMBERSTYLE);\r
561     }\r
562 \r
563     /**\r
564      * Returns a general-purpose number format for the specified locale.\r
565      * @stable ICU 2.0\r
566      */\r
567     public static NumberFormat getNumberInstance(Locale inLocale) {\r
568         return getInstance(ULocale.forLocale(inLocale), NUMBERSTYLE);\r
569     }\r
570 \r
571     /**\r
572      * {@icu} Returns a general-purpose number format for the specified locale.\r
573      * @stable ICU 3.2\r
574      */\r
575     public static NumberFormat getNumberInstance(ULocale inLocale) {\r
576         return getInstance(inLocale, NUMBERSTYLE);\r
577     }\r
578 \r
579     /**\r
580      * Returns an integer number format for the current default locale. The\r
581      * returned number format is configured to round floating point numbers\r
582      * to the nearest integer using IEEE half-even rounding (see {@link\r
583      * com.ibm.icu.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting,\r
584      * and to parse only the integer part of an input string (see {@link\r
585      * #isParseIntegerOnly isParseIntegerOnly}).\r
586      *\r
587      * @return a number format for integer values\r
588      * @stable ICU 2.0\r
589      */\r
590     //Bug 4408066 [Richard/GCL]\r
591     public final static NumberFormat getIntegerInstance() {\r
592         return getInstance(ULocale.getDefault(), INTEGERSTYLE);\r
593     }\r
594 \r
595     /**\r
596      * Returns an integer number format for the specified locale. The\r
597      * returned number format is configured to round floating point numbers\r
598      * to the nearest integer using IEEE half-even rounding (see {@link\r
599      * com.ibm.icu.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting,\r
600      * and to parse only the integer part of an input string (see {@link\r
601      * #isParseIntegerOnly isParseIntegerOnly}).\r
602      *\r
603      * @param inLocale the locale for which a number format is needed\r
604      * @return a number format for integer values\r
605      * @stable ICU 2.0\r
606      */\r
607     //Bug 4408066 [Richard/GCL]\r
608     public static NumberFormat getIntegerInstance(Locale inLocale) {\r
609         return getInstance(ULocale.forLocale(inLocale), INTEGERSTYLE);\r
610     }\r
611 \r
612     /**\r
613      * {@icu} Returns an integer number format for the specified locale. The\r
614      * returned number format is configured to round floating point numbers\r
615      * to the nearest integer using IEEE half-even rounding (see {@link\r
616      * com.ibm.icu.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting,\r
617      * and to parse only the integer part of an input string (see {@link\r
618      * #isParseIntegerOnly isParseIntegerOnly}).\r
619      *\r
620      * @param inLocale the locale for which a number format is needed\r
621      * @return a number format for integer values\r
622      * @stable ICU 3.2\r
623      */\r
624     public static NumberFormat getIntegerInstance(ULocale inLocale) {\r
625         return getInstance(inLocale, INTEGERSTYLE);\r
626     }\r
627 \r
628     /**\r
629      * Returns a currency format for the current default locale.\r
630      * @return a number format for currency\r
631      * @stable ICU 2.0\r
632      */\r
633     public final static NumberFormat getCurrencyInstance() {\r
634         return getInstance(ULocale.getDefault(), CURRENCYSTYLE);\r
635     }\r
636 \r
637     /**\r
638      * Returns a currency format for the specified locale.\r
639      * @return a number format for currency\r
640      * @stable ICU 2.0\r
641      */\r
642     public static NumberFormat getCurrencyInstance(Locale inLocale) {\r
643         return getInstance(ULocale.forLocale(inLocale), CURRENCYSTYLE);\r
644     }\r
645 \r
646     /**\r
647      * {@icu} Returns a currency format for the specified locale.\r
648      * @return a number format for currency\r
649      * @stable ICU 3.2\r
650      */\r
651     public static NumberFormat getCurrencyInstance(ULocale inLocale) {\r
652         return getInstance(inLocale, CURRENCYSTYLE);\r
653     }\r
654 \r
655     /**\r
656      * Returns a percentage format for the current default locale.\r
657      * @return a number format for percents\r
658      * @stable ICU 2.0\r
659      */\r
660     public final static NumberFormat getPercentInstance() {\r
661         return getInstance(ULocale.getDefault(), PERCENTSTYLE);\r
662     }\r
663 \r
664     /**\r
665      * Returns a percentage format for the specified locale.\r
666      * @return a number format for percents\r
667      * @stable ICU 2.0\r
668      */\r
669     public static NumberFormat getPercentInstance(Locale inLocale) {\r
670         return getInstance(ULocale.forLocale(inLocale), PERCENTSTYLE);\r
671     }\r
672 \r
673     /**\r
674      * {@icu} Returns a percentage format for the specified locale.\r
675      * @return a number format for percents\r
676      * @stable ICU 3.2\r
677      */\r
678     public static NumberFormat getPercentInstance(ULocale inLocale) {\r
679         return getInstance(inLocale, PERCENTSTYLE);\r
680     }\r
681 \r
682     /**\r
683      * {@icu} Returns a scientific format for the current default locale.\r
684      * @return a scientific number format\r
685      * @stable ICU 2.0\r
686      */\r
687     public final static NumberFormat getScientificInstance() {\r
688         return getInstance(ULocale.getDefault(), SCIENTIFICSTYLE);\r
689     }\r
690 \r
691     /**\r
692      * {@icu} Returns a scientific format for the specified locale.\r
693      * @return a scientific number format\r
694      * @stable ICU 2.0\r
695      */\r
696     public static NumberFormat getScientificInstance(Locale inLocale) {\r
697         return getInstance(ULocale.forLocale(inLocale), SCIENTIFICSTYLE);\r
698     }\r
699 \r
700     /**\r
701      * {@icu} Returns a scientific format for the specified locale.\r
702      * @return a scientific number format\r
703      * @stable ICU 3.2\r
704      */\r
705     public static NumberFormat getScientificInstance(ULocale inLocale) {\r
706         return getInstance(inLocale, SCIENTIFICSTYLE);\r
707     }\r
708 \r
709     // ===== Factory stuff =====\r
710     /**\r
711      * A NumberFormatFactory is used to register new number formats.  The factory\r
712      * should be able to create any of the predefined formats for each locale it\r
713      * supports.  When registered, the locales it supports extend or override the\r
714      * locales already supported by ICU.\r
715      *\r
716      * <p><b>Note:</b> as of ICU4J 3.2, the default API for NumberFormatFactory uses\r
717      * ULocale instead of Locale.  Instead of overriding createFormat(Locale, int),\r
718      * new implementations should override createFactory(ULocale, int).  Note that\r
719      * one of these two methods <b>MUST</b> be overridden or else an infinite\r
720      * loop will occur.\r
721      *\r
722      * @stable ICU 2.6\r
723      */\r
724     public static abstract class NumberFormatFactory {\r
725         /**\r
726          * Value passed to format requesting a default number format.\r
727          * @stable ICU 2.6\r
728          */\r
729         public static final int FORMAT_NUMBER = NUMBERSTYLE;\r
730 \r
731         /**\r
732          * Value passed to format requesting a currency format.\r
733          * @stable ICU 2.6\r
734          */\r
735         public static final int FORMAT_CURRENCY = CURRENCYSTYLE;\r
736 \r
737         /**\r
738          * Value passed to format requesting a percent format.\r
739          * @stable ICU 2.6\r
740          */\r
741         public static final int FORMAT_PERCENT = PERCENTSTYLE;\r
742 \r
743         /**\r
744          * Value passed to format requesting a scientific format.\r
745          * @stable ICU 2.6\r
746          */\r
747         public static final int FORMAT_SCIENTIFIC = SCIENTIFICSTYLE;\r
748 \r
749         /**\r
750          * Value passed to format requesting an integer format.\r
751          * @stable ICU 2.6\r
752          */\r
753         public static final int FORMAT_INTEGER = INTEGERSTYLE;\r
754 \r
755         /**\r
756          * Returns true if this factory is visible.  Default is true.\r
757          * If not visible, the locales supported by this factory will not\r
758          * be listed by getAvailableLocales.  This value must not change.\r
759          * @return true if the factory is visible.\r
760          * @stable ICU 2.6\r
761          */\r
762         public boolean visible() {\r
763             return true;\r
764         }\r
765 \r
766         /**\r
767          * Returns an immutable collection of the locale names directly\r
768          * supported by this factory.\r
769          * @return the supported locale names.\r
770          * @stable ICU 2.6\r
771          */\r
772          public abstract Set<String> getSupportedLocaleNames();\r
773 \r
774         /**\r
775          * Returns a number format of the appropriate type.  If the locale\r
776          * is not supported, return null.  If the locale is supported, but\r
777          * the type is not provided by this service, return null.  Otherwise\r
778          * return an appropriate instance of NumberFormat.\r
779          * <b>Note:</b> as of ICU4J 3.2, implementations should override\r
780          * this method instead of createFormat(Locale, int).\r
781          * @param loc the locale for which to create the format\r
782          * @param formatType the type of format\r
783          * @return the NumberFormat, or null.\r
784          * @stable ICU 3.2\r
785          */\r
786         public NumberFormat createFormat(ULocale loc, int formatType) {\r
787             return createFormat(loc.toLocale(), formatType);\r
788         }\r
789 \r
790         /**\r
791          * Returns a number format of the appropriate type.  If the locale\r
792          * is not supported, return null.  If the locale is supported, but\r
793          * the type is not provided by this service, return null.  Otherwise\r
794          * return an appropriate instance of NumberFormat.\r
795          * <b>Note:</b> as of ICU4J 3.2, createFormat(ULocale, int) should be\r
796          * overridden instead of this method.  This method is no longer\r
797          * abstract and delegates to that method.\r
798          * @param loc the locale for which to create the format\r
799          * @param formatType the type of format\r
800          * @return the NumberFormat, or null.\r
801          * @stable ICU 2.6\r
802          */\r
803         public NumberFormat createFormat(Locale loc, int formatType) {\r
804             return createFormat(ULocale.forLocale(loc), formatType);\r
805         }\r
806 \r
807         /**\r
808          * @stable ICU 2.6\r
809          */\r
810         protected NumberFormatFactory() {\r
811         }\r
812     }\r
813 \r
814     /**\r
815      * A NumberFormatFactory that supports a single locale.  It can be visible or invisible.\r
816      * @stable ICU 2.6\r
817      */\r
818     public static abstract class SimpleNumberFormatFactory extends NumberFormatFactory {\r
819         final Set<String> localeNames;\r
820         final boolean visible;\r
821 \r
822         /**\r
823          * Constructs a SimpleNumberFormatFactory with the given locale.\r
824          * @stable ICU 2.6\r
825          */\r
826         public SimpleNumberFormatFactory(Locale locale) {\r
827             this(locale, true);\r
828         }\r
829 \r
830         /**\r
831          * Constructs a SimpleNumberFormatFactory with the given locale and the\r
832          * visibility.\r
833          * @stable ICU 2.6\r
834          */\r
835         public SimpleNumberFormatFactory(Locale locale, boolean visible) {\r
836             localeNames = Collections.singleton(ULocale.forLocale(locale).getBaseName());\r
837             this.visible = visible;\r
838         }\r
839 \r
840         /**\r
841          * Constructs a SimpleNumberFormatFactory with the given locale.\r
842          * @stable ICU 3.2\r
843          */\r
844         public SimpleNumberFormatFactory(ULocale locale) {\r
845             this(locale, true);\r
846         }\r
847 \r
848         /**\r
849          * Constructs a SimpleNumberFormatFactory with the given locale and the\r
850          * visibility.\r
851          * @stable ICU 3.2\r
852          */\r
853         public SimpleNumberFormatFactory(ULocale locale, boolean visible) {\r
854             localeNames = Collections.singleton(locale.getBaseName());\r
855             this.visible = visible;\r
856         }\r
857 \r
858         /**\r
859          * {@inheritDoc}\r
860          * @stable ICU 2.6\r
861          */\r
862         public final boolean visible() {\r
863             return visible;\r
864         }\r
865 \r
866         /**\r
867          * {@inheritDoc}\r
868          * @stable ICU 2.6\r
869          */\r
870         public final Set<String> getSupportedLocaleNames() {\r
871             return localeNames;\r
872         }\r
873     }\r
874 \r
875     // shim so we can build without service code\r
876     static abstract class NumberFormatShim {\r
877         abstract Locale[] getAvailableLocales();\r
878         abstract ULocale[] getAvailableULocales();\r
879         abstract Object registerFactory(NumberFormatFactory f);\r
880         abstract boolean unregister(Object k);\r
881         abstract NumberFormat createInstance(ULocale l, int k);\r
882     }\r
883 \r
884     private static NumberFormatShim shim;\r
885     private static NumberFormatShim getShim() {\r
886         // Note: this instantiation is safe on loose-memory-model configurations\r
887         // despite lack of synchronization, since the shim instance has no state--\r
888         // it's all in the class init.  The worst problem is we might instantiate\r
889         // two shim instances, but they'll share the same state so that's ok.\r
890         if (shim == null) {\r
891             try {\r
892                 Class<?> cls = Class.forName("com.ibm.icu.text.NumberFormatServiceShim");\r
893                 shim = (NumberFormatShim)cls.newInstance();\r
894             }\r
895             ///CLOVER:OFF\r
896             catch (MissingResourceException e){\r
897                 throw e;\r
898             }\r
899             catch (Exception e) {\r
900                // e.printStackTrace();\r
901                 throw new RuntimeException(e.getMessage());\r
902             }\r
903             ///CLOVER:ON\r
904         }\r
905         return shim;\r
906     }\r
907 \r
908     /**\r
909      * Returns the list of Locales for which NumberFormats are available.\r
910      * @return the available locales\r
911      * @stable ICU 2.0\r
912      */\r
913     public static Locale[] getAvailableLocales() {\r
914         if (shim == null) {\r
915             return ICUResourceBundle.getAvailableLocales();\r
916         }\r
917         return getShim().getAvailableLocales();\r
918     }\r
919 \r
920     /**\r
921      * {@icu} Returns the list of Locales for which NumberFormats are available.\r
922      * @return the available locales\r
923      * @draft ICU 3.2 (retain)\r
924      * @provisional This API might change or be removed in a future release.\r
925      */\r
926     public static ULocale[] getAvailableULocales() {\r
927         if (shim == null) {\r
928             return ICUResourceBundle.getAvailableULocales();\r
929         }\r
930         return getShim().getAvailableULocales();\r
931     }\r
932 \r
933     /**\r
934      * {@icu} Registers a new NumberFormatFactory.  The factory is adopted by\r
935      * the service and must not be modified.  The returned object is a\r
936      * key that can be used to unregister this factory.\r
937      * @param factory the factory to register\r
938      * @return a key with which to unregister the factory\r
939      * @stable ICU 2.6\r
940      */\r
941     public static Object registerFactory(NumberFormatFactory factory) {\r
942         if (factory == null) {\r
943             throw new IllegalArgumentException("factory must not be null");\r
944         }\r
945         return getShim().registerFactory(factory);\r
946     }\r
947 \r
948     /**\r
949      * {@icu} Unregisters the factory or instance associated with this key (obtained from\r
950      * registerInstance or registerFactory).\r
951      * @param registryKey a key obtained from registerFactory\r
952      * @return true if the object was successfully unregistered\r
953      * @stable ICU 2.6\r
954      */\r
955     public static boolean unregister(Object registryKey) {\r
956         if (registryKey == null) {\r
957             throw new IllegalArgumentException("registryKey must not be null");\r
958         }\r
959 \r
960         if (shim == null) {\r
961             return false;\r
962         }\r
963 \r
964         return shim.unregister(registryKey);\r
965     }\r
966 \r
967     // ===== End of factory stuff =====\r
968 \r
969     /**\r
970      * Overrides hashCode.\r
971      * @stable ICU 2.0\r
972      */\r
973     public int hashCode() {\r
974         return maximumIntegerDigits * 37 + maxFractionDigits;\r
975         // just enough fields for a reasonable distribution\r
976     }\r
977 \r
978     /**\r
979      * Overrides equals.\r
980      * Two NumberFormats are equal if they are of the same class\r
981      * and the settings (groupingUsed, parseIntegerOnly, maximumIntegerDigits, etc.\r
982      * are equal.\r
983      * @param obj the object to compare against\r
984      * @return true if the object is equal to this.\r
985      * @stable ICU 2.0\r
986      */\r
987     public boolean equals(Object obj) {\r
988         if (obj == null) return false;\r
989         if (this == obj)\r
990             return true;\r
991         if (getClass() != obj.getClass())\r
992             return false;\r
993         NumberFormat other = (NumberFormat) obj;\r
994         return maximumIntegerDigits == other.maximumIntegerDigits\r
995             && minimumIntegerDigits == other.minimumIntegerDigits\r
996             && maximumFractionDigits == other.maximumFractionDigits\r
997             && minimumFractionDigits == other.minimumFractionDigits\r
998             && groupingUsed == other.groupingUsed\r
999             && parseIntegerOnly == other.parseIntegerOnly\r
1000             && parseStrict == other.parseStrict;\r
1001     }\r
1002 \r
1003     /**\r
1004      * Overrides clone.\r
1005      * @stable ICU 2.0\r
1006      */\r
1007     public Object clone() {\r
1008         NumberFormat other = (NumberFormat) super.clone();\r
1009         return other;\r
1010     }\r
1011 \r
1012     /**\r
1013      * Returns true if grouping is used in this format. For example, in the\r
1014      * en_US locale, with grouping on, the number 1234567 will be formatted\r
1015      * as "1,234,567". The grouping separator as well as the size of each group\r
1016      * is locale-dependent and is determined by subclasses of NumberFormat.\r
1017      * Grouping affects both parsing and formatting.\r
1018      * @return true if grouping is used\r
1019      * @see #setGroupingUsed\r
1020      * @stable ICU 2.0\r
1021      */\r
1022     public boolean isGroupingUsed() {\r
1023         return groupingUsed;\r
1024     }\r
1025 \r
1026     /**\r
1027      * Sets whether or not grouping will be used in this format.  Grouping\r
1028      * affects both parsing and formatting.\r
1029      * @see #isGroupingUsed\r
1030      * @param newValue true to use grouping.\r
1031      * @stable ICU 2.0\r
1032      */\r
1033     public void setGroupingUsed(boolean newValue) {\r
1034         groupingUsed = newValue;\r
1035     }\r
1036 \r
1037     /**\r
1038      * Returns the maximum number of digits allowed in the integer portion of a\r
1039      * number.  The default value is 40, which subclasses can override.\r
1040      * When formatting, the exact behavior when this value is exceeded is\r
1041      * subclass-specific.  When parsing, this has no effect.\r
1042      * @return the maximum number of integer digits\r
1043      * @see #setMaximumIntegerDigits\r
1044      * @stable ICU 2.0\r
1045      */\r
1046     public int getMaximumIntegerDigits() {\r
1047         return maximumIntegerDigits;\r
1048     }\r
1049 \r
1050     /**\r
1051      * Sets the maximum number of digits allowed in the integer portion of a\r
1052      * number. This must be >= minimumIntegerDigits.  If the\r
1053      * new value for maximumIntegerDigits is less than the current value\r
1054      * of minimumIntegerDigits, then minimumIntegerDigits will also be set to\r
1055      * the new value.\r
1056      * @param newValue the maximum number of integer digits to be shown; if\r
1057      * less than zero, then zero is used.  Subclasses might enforce an\r
1058      * upper limit to this value appropriate to the numeric type being formatted.\r
1059      * @see #getMaximumIntegerDigits\r
1060      * @stable ICU 2.0\r
1061      */\r
1062     public void setMaximumIntegerDigits(int newValue) {\r
1063         maximumIntegerDigits = Math.max(0,newValue);\r
1064         if (minimumIntegerDigits > maximumIntegerDigits)\r
1065             minimumIntegerDigits = maximumIntegerDigits;\r
1066     }\r
1067 \r
1068     /**\r
1069      * Returns the minimum number of digits allowed in the integer portion of a\r
1070      * number.  The default value is 1, which subclasses can override.\r
1071      * When formatting, if this value is not reached, numbers are padded on the\r
1072      * left with the locale-specific '0' character to ensure at least this\r
1073      * number of integer digits.  When parsing, this has no effect.\r
1074      * @return the minimum number of integer digits\r
1075      * @see #setMinimumIntegerDigits\r
1076      * @stable ICU 2.0\r
1077      */\r
1078     public int getMinimumIntegerDigits() {\r
1079         return minimumIntegerDigits;\r
1080     }\r
1081 \r
1082     /**\r
1083      * Sets the minimum number of digits allowed in the integer portion of a\r
1084      * number.  This must be <= maximumIntegerDigits.  If the\r
1085      * new value for minimumIntegerDigits is more than the current value\r
1086      * of maximumIntegerDigits, then maximumIntegerDigits will also be set to\r
1087      * the new value.\r
1088      * @param newValue the minimum number of integer digits to be shown; if\r
1089      * less than zero, then zero is used. Subclasses might enforce an\r
1090      * upper limit to this value appropriate to the numeric type being formatted.\r
1091      * @see #getMinimumIntegerDigits\r
1092      * @stable ICU 2.0\r
1093      */\r
1094     public void setMinimumIntegerDigits(int newValue) {\r
1095         minimumIntegerDigits = Math.max(0,newValue);\r
1096         if (minimumIntegerDigits > maximumIntegerDigits)\r
1097             maximumIntegerDigits = minimumIntegerDigits;\r
1098     }\r
1099 \r
1100     /**\r
1101      * Returns the maximum number of digits allowed in the fraction\r
1102      * portion of a number.  The default value is 3, which subclasses\r
1103      * can override.  When formatting, the exact behavior when this\r
1104      * value is exceeded is subclass-specific.  When parsing, this has\r
1105      * no effect.\r
1106      * @return the maximum number of fraction digits\r
1107      * @see #setMaximumFractionDigits\r
1108      * @stable ICU 2.0\r
1109      */\r
1110     public int getMaximumFractionDigits() {\r
1111         return maximumFractionDigits;\r
1112     }\r
1113 \r
1114     /**\r
1115      * Sets the maximum number of digits allowed in the fraction portion of a\r
1116      * number. This must be >= minimumFractionDigits.  If the\r
1117      * new value for maximumFractionDigits is less than the current value\r
1118      * of minimumFractionDigits, then minimumFractionDigits will also be set to\r
1119      * the new value.\r
1120      * @param newValue the maximum number of fraction digits to be shown; if\r
1121      * less than zero, then zero is used. The concrete subclass may enforce an\r
1122      * upper limit to this value appropriate to the numeric type being formatted.\r
1123      * @see #getMaximumFractionDigits\r
1124      * @stable ICU 2.0\r
1125      */\r
1126     public void setMaximumFractionDigits(int newValue) {\r
1127         maximumFractionDigits = Math.max(0,newValue);\r
1128         if (maximumFractionDigits < minimumFractionDigits)\r
1129             minimumFractionDigits = maximumFractionDigits;\r
1130     }\r
1131 \r
1132     /**\r
1133      * Returns the minimum number of digits allowed in the fraction portion of a\r
1134      * number.  The default value is 0, which subclasses can override.\r
1135      * When formatting, if this value is not reached, numbers are padded on\r
1136      * the right with the locale-specific '0' character to ensure at least\r
1137      * this number of fraction digits.  When parsing, this has no effect.\r
1138      * @return the minimum number of fraction digits\r
1139      * @see #setMinimumFractionDigits\r
1140      * @stable ICU 2.0\r
1141      */\r
1142     public int getMinimumFractionDigits() {\r
1143         return minimumFractionDigits;\r
1144     }\r
1145 \r
1146     /**\r
1147      * Sets the minimum number of digits allowed in the fraction portion of a\r
1148      * number.  This must be <= maximumFractionDigits.  If the\r
1149      * new value for minimumFractionDigits exceeds the current value\r
1150      * of maximumFractionDigits, then maximumFractionDigits will also be set to\r
1151      * the new value.\r
1152      * @param newValue the minimum number of fraction digits to be shown; if\r
1153      * less than zero, then zero is used.  Subclasses might enforce an\r
1154      * upper limit to this value appropriate to the numeric type being formatted.\r
1155      * @see #getMinimumFractionDigits\r
1156      * @stable ICU 2.0\r
1157      */\r
1158     public void setMinimumFractionDigits(int newValue) {\r
1159         minimumFractionDigits = Math.max(0,newValue);\r
1160         if (maximumFractionDigits < minimumFractionDigits)\r
1161             maximumFractionDigits = minimumFractionDigits;\r
1162     }\r
1163 \r
1164     /**\r
1165      * Sets the <tt>Currency</tt> object used to display currency\r
1166      * amounts.  This takes effect immediately, if this format is a\r
1167      * currency format.  If this format is not a currency format, then\r
1168      * the currency object is used if and when this object becomes a\r
1169      * currency format.\r
1170      * @param theCurrency new currency object to use.  May be null for\r
1171      * some subclasses.\r
1172      * @stable ICU 2.6\r
1173      */\r
1174     public void setCurrency(Currency theCurrency) {\r
1175         currency = theCurrency;\r
1176     }\r
1177 \r
1178     /**\r
1179      * Returns the <tt>Currency</tt> object used to display currency\r
1180      * amounts.  This may be null.\r
1181      * @stable ICU 2.6\r
1182      */\r
1183     public Currency getCurrency() {\r
1184         return currency;\r
1185     }\r
1186 \r
1187     /**\r
1188      * Returns the currency in effect for this formatter.  Subclasses\r
1189      * should override this method as needed.  Unlike getCurrency(),\r
1190      * this method should never return null.\r
1191      * @return a non-null Currency\r
1192      * @internal\r
1193      * @deprecated This API is ICU internal only.\r
1194      */\r
1195     protected Currency getEffectiveCurrency() {\r
1196         Currency c = getCurrency();\r
1197         if (c == null) {\r
1198             ULocale uloc = getLocale(ULocale.VALID_LOCALE);\r
1199             if (uloc == null) {\r
1200                 uloc = ULocale.getDefault();\r
1201             }\r
1202             c = Currency.getInstance(uloc);\r
1203         }\r
1204         return c;\r
1205     }\r
1206 \r
1207     /**\r
1208      * Returns the rounding mode used in this NumberFormat.  The default implementation of\r
1209      * tis method in NumberFormat always throws <code>UnsupportedOperationException</code>.\r
1210      * @return A rounding mode, between <code>BigDecimal.ROUND_UP</code>\r
1211      * and <code>BigDecimal.ROUND_UNNECESSARY</code>.\r
1212      * @see #setRoundingMode(int)\r
1213      * @stable ICU 4.0\r
1214      */\r
1215     public int getRoundingMode() {\r
1216         throw new UnsupportedOperationException(\r
1217             "getRoundingMode must be implemented by the subclass implementation.");\r
1218     }\r
1219 \r
1220     /**\r
1221      * Set the rounding mode used in this NumberFormat.  The default implementation of\r
1222      * tis method in NumberFormat always throws <code>UnsupportedOperationException</code>.\r
1223      * @param roundingMode A rounding mode, between\r
1224      * <code>BigDecimal.ROUND_UP</code> and\r
1225      * <code>BigDecimal.ROUND_UNNECESSARY</code>.\r
1226      * @see #getRoundingMode()\r
1227      * @stable ICU 4.0\r
1228      */\r
1229     public void setRoundingMode(int roundingMode) {\r
1230         throw new UnsupportedOperationException(\r
1231             "setRoundingMode must be implemented by the subclass implementation.");\r
1232     }\r
1233 \r
1234 \r
1235     /**\r
1236      * Returns a specific style number format for a specific locale.\r
1237      * @param desiredLocale  the specific locale.\r
1238      * @param choice         number format style\r
1239      * @throws IllegalArgumentException  if choice is not one of\r
1240      *                                   NUMBERSTYLE, CURRENCYSTYLE,\r
1241      *                                   PERCENTSTYLE, SCIENTIFICSTYLE,\r
1242      *                                   INTEGERSTYLE,\r
1243      *                                   ISOCURRENCYSTYLE, PLURALCURRENCYSTYLE,\r
1244      * @stable ICU 4.2\r
1245      */\r
1246     public static NumberFormat getInstance(ULocale desiredLocale, int choice) {\r
1247         if (choice < NUMBERSTYLE || choice > PLURALCURRENCYSTYLE) {\r
1248             throw new IllegalArgumentException(\r
1249                 "choice should be from NUMBERSTYLE to PLURALCURRENCYSTYLE");\r
1250         }\r
1251 //          if (shim == null) {\r
1252 //              return createInstance(desiredLocale, choice);\r
1253 //          } else {\r
1254 //              // TODO: shims must call setLocale() on object they create\r
1255 //              return getShim().createInstance(desiredLocale, choice);\r
1256 //          }\r
1257         return getShim().createInstance(desiredLocale, choice);\r
1258     }\r
1259 \r
1260     // =======================privates===============================\r
1261     // Hook for service\r
1262     static NumberFormat createInstance(ULocale desiredLocale, int choice) {\r
1263         // If the choice is PLURALCURRENCYSTYLE, the pattern is not a single\r
1264         // pattern, it is a pattern set, so we do not need to get them here.\r
1265         // If the choice is ISOCURRENCYSTYLE, the pattern is the currrency\r
1266         // pattern in the locale but by replacing the single currency sign\r
1267         // with double currency sign.\r
1268         String pattern = getPattern(desiredLocale, choice);\r
1269         DecimalFormatSymbols symbols = new DecimalFormatSymbols(desiredLocale);\r
1270 \r
1271         // Here we assume that the locale passed in is in the canonical\r
1272         // form, e.g: pt_PT_@currency=PTE not pt_PT_PREEURO\r
1273         // This style wont work for currency plural format.\r
1274         // For currency plural format, the pattern is get from\r
1275         // the locale (from CurrencyUnitPatterns) without override.\r
1276         if(choice == CURRENCYSTYLE || choice == ISOCURRENCYSTYLE){\r
1277             String temp = symbols.getCurrencyPattern();\r
1278             if(temp!=null){\r
1279                 pattern = temp;\r
1280             }\r
1281         }\r
1282 \r
1283         // replace single currency sign in the pattern with double currency sign\r
1284         // if the choice is ISOCURRENCYSTYLE.\r
1285         if (choice == ISOCURRENCYSTYLE) {\r
1286             pattern = pattern.replace("\u00A4", doubleCurrencyStr);\r
1287         }\r
1288 \r
1289         // Get the numbering system\r
1290         NumberingSystem ns = NumberingSystem.getInstance(desiredLocale);\r
1291         if ( ns == null ) {\r
1292             return null;\r
1293         }\r
1294 \r
1295         NumberFormat format;\r
1296 \r
1297         if ( ns != null && ns.isAlgorithmic()) {\r
1298             String nsDesc;\r
1299             String nsRuleSetGroup;\r
1300             String nsRuleSetName;\r
1301             ULocale nsLoc;\r
1302             int desiredRulesType = RuleBasedNumberFormat.NUMBERING_SYSTEM;\r
1303 \r
1304             nsDesc = ns.getDescription();\r
1305             int firstSlash = nsDesc.indexOf("/");\r
1306             int lastSlash = nsDesc.lastIndexOf("/");\r
1307 \r
1308             if ( lastSlash > firstSlash ) {\r
1309                String nsLocID = nsDesc.substring(0,firstSlash);\r
1310                nsRuleSetGroup = nsDesc.substring(firstSlash+1,lastSlash);\r
1311                nsRuleSetName = nsDesc.substring(lastSlash+1);\r
1312 \r
1313                nsLoc = new ULocale(nsLocID);\r
1314                if ( nsRuleSetGroup.equals("SpelloutRules")) {\r
1315                    desiredRulesType = RuleBasedNumberFormat.SPELLOUT;\r
1316                }\r
1317             } else {\r
1318                 nsLoc = desiredLocale;\r
1319                 nsRuleSetName = nsDesc;\r
1320             }\r
1321 \r
1322             RuleBasedNumberFormat r = new RuleBasedNumberFormat(nsLoc,desiredRulesType);\r
1323             r.setDefaultRuleSet(nsRuleSetName);\r
1324             format = r;\r
1325         } else {\r
1326             DecimalFormat f = new DecimalFormat(pattern, symbols, choice);\r
1327             // System.out.println("loc: " + desiredLocale + " choice: " + choice + " pat: " + pattern + " sym: " + symbols + " result: " + format);\r
1328 \r
1329             /*Bug 4408066\r
1330              Add codes for the new method getIntegerInstance() [Richard/GCL]\r
1331             */\r
1332             // TODO: revisit this -- this is almost certainly not the way we want\r
1333             // to do this.  aliu 1/6/2004\r
1334             if (choice == INTEGERSTYLE) {\r
1335                 f.setMaximumFractionDigits(0);\r
1336                 f.setDecimalSeparatorAlwaysShown(false);\r
1337                 f.setParseIntegerOnly(true);\r
1338             }\r
1339             format = f;\r
1340        }\r
1341         // TODO: the actual locale of the *pattern* may differ from that\r
1342         // for the *symbols*.  For now, we use the data for the symbols.\r
1343         // Revisit this.\r
1344         ULocale valid = symbols.getLocale(ULocale.VALID_LOCALE);\r
1345         ULocale actual = symbols.getLocale(ULocale.ACTUAL_LOCALE);\r
1346         format.setLocale(valid, actual);\r
1347 \r
1348         return format;\r
1349     }\r
1350 \r
1351     /**\r
1352      * Returns the pattern for the provided locale and choice.\r
1353      * @param forLocale the locale of the data.\r
1354      * @param choice the pattern format.\r
1355      * @return the pattern\r
1356      * @deprecated ICU 3.4 subclassers should override getPattern(ULocale, int) instead of this method.\r
1357      */\r
1358     protected static String getPattern(Locale forLocale, int choice) {\r
1359         return getPattern(ULocale.forLocale(forLocale), choice);\r
1360     }\r
1361 \r
1362     /**\r
1363      * Returns the pattern for the provided locale and choice.\r
1364      * @param forLocale the locale of the data.\r
1365      * @param choice the pattern format.\r
1366      * @return the pattern\r
1367      * @stable ICU 3.2\r
1368      */\r
1369     protected static String getPattern(ULocale forLocale, int choice) {\r
1370 \r
1371         /* The following code takes care of a few cases where the\r
1372          * resource data in the underlying JDK lags the new features\r
1373          * we have added to ICU4J: scientific notation, rounding, and\r
1374          * secondary grouping.\r
1375          *\r
1376          * We detect these cases here and return various hard-coded\r
1377          * resource data.  This is the simplest solution for now, but\r
1378          * it is not a good long-term mechanism.\r
1379          *\r
1380          * We should replace this code with a data-driven mechanism\r
1381          * that reads the bundle com.ibm.icu.impl.data.LocaleElements\r
1382          * and parses an exception table that overrides the standard\r
1383          * data at java.text.resource.LocaleElements*.java.\r
1384          * Alternatively, we should create our own copy of the\r
1385          * resource data, and use that exclusively.\r
1386          */\r
1387 \r
1388         // TEMPORARY, until we get scientific patterns into the main\r
1389         // resources:  Retrieve scientific patterns from our resources.\r
1390         if (choice == SCIENTIFICSTYLE) {\r
1391             // Temporarily hard code; retrieve from resource later\r
1392             /*For ICU compatibility [Richard/GCL]*/\r
1393             return "#E0";\r
1394             // return NumberFormat.getBaseStringArray("NumberPatterns")[SCIENTIFICSTYLE];\r
1395         }\r
1396         // TEMPORARY: Use rounding for Swiss currency\r
1397         //if (choice == CURRENCYSTYLE &&\r
1398         //    forLocale.getCountry().equals("CH")) {\r
1399         //    return "'Fr. '#,##0.05;'Fr.-'#,##0.05";\r
1400         //}\r
1401         // TEMPORARY: Special case IN number format\r
1402         //if (choice == NUMBERSTYLE &&\r
1403         //    forLocale.getCountry().equals("IN")) {\r
1404         //    return "#,##,##0.###";\r
1405         //}\r
1406 \r
1407         // {dlf}\r
1408         ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.\r
1409             getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, forLocale);\r
1410         String[] numberPatterns = rb.getStringArray("NumberPatterns");\r
1411 \r
1412         /* {dlf}\r
1413         // Try the cache first\r
1414         String[] numberPatterns = (String[]) cachedLocaleData.get(forLocale);\r
1415         if (numberPatterns == null) {\r
1416             OverlayBundle resource = new OverlayBundle(new String[]\r
1417                 { "com.ibm.icu.impl.data.LocaleElements", RESOURCE_BASE }, forLocale);\r
1418             numberPatterns = resource.getStringArray("NumberPatterns");\r
1419             // Update the cache\r
1420             cachedLocaleData.put(forLocale, numberPatterns);\r
1421         }\r
1422         */\r
1423 \r
1424         /*Bug 4408066\r
1425          Add codes for the new method getIntegerInstance() [Richard/GCL]\r
1426         */\r
1427         /* for ISOCURRENCYSTYLE and PLURALCURRENCYSTYLE,\r
1428          * the pattern is the same as the pattern of CURRENCYSTYLE\r
1429          * but by replacing the single currency sign with\r
1430          * double currency sign or triple currency sign.\r
1431          */\r
1432         int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE :\r
1433                 ((choice == ISOCURRENCYSTYLE || choice == PLURALCURRENCYSTYLE)?\r
1434                 CURRENCYSTYLE : choice); //[Richard/GCL]\r
1435         return numberPatterns[entry]; //[Richard/GCL]\r
1436     }\r
1437 \r
1438     /**\r
1439      * First, read in the default serializable data.\r
1440      *\r
1441      * Then, if <code>serialVersionOnStream</code> is less than 1, indicating that\r
1442      * the stream was written by JDK 1.1,\r
1443      * set the <code>int</code> fields such as <code>maximumIntegerDigits</code>\r
1444      * to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>,\r
1445      * since the <code>int</code> fields were not present in JDK 1.1.\r
1446      * Finally, set serialVersionOnStream back to the maximum allowed value so that\r
1447      * default serialization will work properly if this object is streamed out again.\r
1448      */\r
1449     private void readObject(ObjectInputStream stream)\r
1450          throws IOException, ClassNotFoundException\r
1451     {\r
1452         stream.defaultReadObject();\r
1453         ///CLOVER:OFF\r
1454         // we don't have serialization data for this format\r
1455         if (serialVersionOnStream < 1) {\r
1456             // Didn't have additional int fields, reassign to use them.\r
1457             maximumIntegerDigits = maxIntegerDigits;\r
1458             minimumIntegerDigits = minIntegerDigits;\r
1459             maximumFractionDigits = maxFractionDigits;\r
1460             minimumFractionDigits = minFractionDigits;\r
1461         }\r
1462         ///CLOVER:ON\r
1463         /*Bug 4185761\r
1464           Validate the min and max fields [Richard/GCL]\r
1465         */\r
1466         if (minimumIntegerDigits > maximumIntegerDigits ||\r
1467             minimumFractionDigits > maximumFractionDigits ||\r
1468             minimumIntegerDigits < 0 || minimumFractionDigits < 0) {\r
1469             throw new InvalidObjectException("Digit count range invalid");\r
1470         }\r
1471         serialVersionOnStream = currentSerialVersion;\r
1472     }\r
1473 \r
1474     /**\r
1475      * Write out the default serializable data, after first setting\r
1476      * the <code>byte</code> fields such as <code>maxIntegerDigits</code> to be\r
1477      * equal to the <code>int</code> fields such as <code>maximumIntegerDigits</code>\r
1478      * (or to <code>Byte.MAX_VALUE</code>, whichever is smaller), for compatibility\r
1479      * with the JDK 1.1 version of the stream format.\r
1480      */\r
1481     private void writeObject(ObjectOutputStream stream)\r
1482          throws IOException\r
1483     {\r
1484         maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE :\r
1485             (byte)maximumIntegerDigits;\r
1486         minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE :\r
1487             (byte)minimumIntegerDigits;\r
1488         maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE :\r
1489             (byte)maximumFractionDigits;\r
1490         minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ? Byte.MAX_VALUE :\r
1491             (byte)minimumFractionDigits;\r
1492         stream.defaultWriteObject();\r
1493     }\r
1494 \r
1495 // Unused -- Alan 2003-05\r
1496 //    /**\r
1497 //     * Cache to hold the NumberPatterns of a Locale.\r
1498 //     */\r
1499 //    private static final Hashtable cachedLocaleData = new Hashtable(3);\r
1500 \r
1501       private static final char[] doubleCurrencySign = {0xA4, 0xA4};\r
1502       private static final String doubleCurrencyStr = new String(doubleCurrencySign);\r
1503 \r
1504     /*Bug 4408066\r
1505       Add Field for the new method getIntegerInstance() [Richard/GCL]\r
1506     */\r
1507 \r
1508 \r
1509     /**\r
1510      * True if the the grouping (i.e. thousands) separator is used when\r
1511      * formatting and parsing numbers.\r
1512      *\r
1513      * @serial\r
1514      * @see #isGroupingUsed\r
1515      */\r
1516     private boolean groupingUsed = true;\r
1517 \r
1518     /**\r
1519      * The maximum number of digits allowed in the integer portion of a\r
1520      * number.  <code>maxIntegerDigits</code> must be greater than or equal to\r
1521      * <code>minIntegerDigits</code>.\r
1522      * <p>\r
1523      * <strong>Note:</strong> This field exists only for serialization\r
1524      * compatibility with JDK 1.1.  In JDK 1.2 and higher, the new\r
1525      * <code>int</code> field <code>maximumIntegerDigits</code> is used instead.\r
1526      * When writing to a stream, <code>maxIntegerDigits</code> is set to\r
1527      * <code>maximumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,\r
1528      * whichever is smaller.  When reading from a stream, this field is used\r
1529      * only if <code>serialVersionOnStream</code> is less than 1.\r
1530      *\r
1531      * @serial\r
1532      * @see #getMaximumIntegerDigits\r
1533      */\r
1534     private byte    maxIntegerDigits = 40;\r
1535 \r
1536     /**\r
1537      * The minimum number of digits allowed in the integer portion of a\r
1538      * number.  <code>minimumIntegerDigits</code> must be less than or equal to\r
1539      * <code>maximumIntegerDigits</code>.\r
1540      * <p>\r
1541      * <strong>Note:</strong> This field exists only for serialization\r
1542      * compatibility with JDK 1.1.  In JDK 1.2 and higher, the new\r
1543      * <code>int</code> field <code>minimumIntegerDigits</code> is used instead.\r
1544      * When writing to a stream, <code>minIntegerDigits</code> is set to\r
1545      * <code>minimumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,\r
1546      * whichever is smaller.  When reading from a stream, this field is used\r
1547      * only if <code>serialVersionOnStream</code> is less than 1.\r
1548      *\r
1549      * @serial\r
1550      * @see #getMinimumIntegerDigits\r
1551      */\r
1552     private byte    minIntegerDigits = 1;\r
1553 \r
1554     /**\r
1555      * The maximum number of digits allowed in the fractional portion of a\r
1556      * number.  <code>maximumFractionDigits</code> must be greater than or equal to\r
1557      * <code>minimumFractionDigits</code>.\r
1558      * <p>\r
1559      * <strong>Note:</strong> This field exists only for serialization\r
1560      * compatibility with JDK 1.1.  In JDK 1.2 and higher, the new\r
1561      * <code>int</code> field <code>maximumFractionDigits</code> is used instead.\r
1562      * When writing to a stream, <code>maxFractionDigits</code> is set to\r
1563      * <code>maximumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,\r
1564      * whichever is smaller.  When reading from a stream, this field is used\r
1565      * only if <code>serialVersionOnStream</code> is less than 1.\r
1566      *\r
1567      * @serial\r
1568      * @see #getMaximumFractionDigits\r
1569      */\r
1570     private byte    maxFractionDigits = 3;    // invariant, >= minFractionDigits\r
1571 \r
1572     /**\r
1573      * The minimum number of digits allowed in the fractional portion of a\r
1574      * number.  <code>minimumFractionDigits</code> must be less than or equal to\r
1575      * <code>maximumFractionDigits</code>.\r
1576      * <p>\r
1577      * <strong>Note:</strong> This field exists only for serialization\r
1578      * compatibility with JDK 1.1.  In JDK 1.2 and higher, the new\r
1579      * <code>int</code> field <code>minimumFractionDigits</code> is used instead.\r
1580      * When writing to a stream, <code>minFractionDigits</code> is set to\r
1581      * <code>minimumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,\r
1582      * whichever is smaller.  When reading from a stream, this field is used\r
1583      * only if <code>serialVersionOnStream</code> is less than 1.\r
1584      *\r
1585      * @serial\r
1586      * @see #getMinimumFractionDigits\r
1587      */\r
1588     private byte    minFractionDigits = 0;\r
1589 \r
1590     /**\r
1591      * True if this format will parse numbers as integers only.\r
1592      *\r
1593      * @serial\r
1594      * @see #isParseIntegerOnly\r
1595      */\r
1596     private boolean parseIntegerOnly = false;\r
1597 \r
1598     // new fields for 1.2.  byte is too small for integer digits.\r
1599 \r
1600     /**\r
1601      * The maximum number of digits allowed in the integer portion of a\r
1602      * number.  <code>maximumIntegerDigits</code> must be greater than or equal to\r
1603      * <code>minimumIntegerDigits</code>.\r
1604      *\r
1605      * @serial\r
1606      * @see #getMaximumIntegerDigits\r
1607      */\r
1608     private int    maximumIntegerDigits = 40;\r
1609 \r
1610     /**\r
1611      * The minimum number of digits allowed in the integer portion of a\r
1612      * number.  <code>minimumIntegerDigits</code> must be less than or equal to\r
1613      * <code>maximumIntegerDigits</code>.\r
1614      *\r
1615      * @serial\r
1616      * @see #getMinimumIntegerDigits\r
1617      */\r
1618     private int    minimumIntegerDigits = 1;\r
1619 \r
1620     /**\r
1621      * The maximum number of digits allowed in the fractional portion of a\r
1622      * number.  <code>maximumFractionDigits</code> must be greater than or equal to\r
1623      * <code>minimumFractionDigits</code>.\r
1624      *\r
1625      * @serial\r
1626      * @see #getMaximumFractionDigits\r
1627      */\r
1628     private int    maximumFractionDigits = 3;    // invariant, >= minFractionDigits\r
1629 \r
1630     /**\r
1631      * The minimum number of digits allowed in the fractional portion of a\r
1632      * number.  <code>minimumFractionDigits</code> must be less than or equal to\r
1633      * <code>maximumFractionDigits</code>.\r
1634      *\r
1635      * @serial\r
1636      * @see #getMinimumFractionDigits\r
1637      */\r
1638     private int    minimumFractionDigits = 0;\r
1639 \r
1640     /**\r
1641      * Currency object used to format currencies.  Subclasses may\r
1642      * ignore this if they are not currency formats.  This will be\r
1643      * null unless a subclass sets it to a non-null value.\r
1644      * @since ICU 2.6\r
1645      */\r
1646     private Currency currency;\r
1647 \r
1648     static final int currentSerialVersion = 1;\r
1649 \r
1650     /**\r
1651      * Describes the version of <code>NumberFormat</code> present on the stream.\r
1652      * Possible values are:\r
1653      * <ul>\r
1654      * <li><b>0</b> (or uninitialized): the JDK 1.1 version of the stream format.\r
1655      *     In this version, the <code>int</code> fields such as\r
1656      *     <code>maximumIntegerDigits</code> were not present, and the <code>byte</code>\r
1657      *     fields such as <code>maxIntegerDigits</code> are used instead.\r
1658      *\r
1659      * <li><b>1</b>: the JDK 1.2 version of the stream format.  The values of the\r
1660      *     <code>byte</code> fields such as <code>maxIntegerDigits</code> are ignored,\r
1661      *     and the <code>int</code> fields such as <code>maximumIntegerDigits</code>\r
1662      *     are used instead.\r
1663      * </ul>\r
1664      * When streaming out a <code>NumberFormat</code>, the most recent format\r
1665      * (corresponding to the highest allowable <code>serialVersionOnStream</code>)\r
1666      * is always written.\r
1667      *\r
1668      * @serial\r
1669      */\r
1670     private int serialVersionOnStream = currentSerialVersion;\r
1671 \r
1672     // Removed "implements Cloneable" clause.  Needs to update serialization\r
1673     // ID for backward compatibility.\r
1674     private static final long serialVersionUID = -2308460125733713944L;\r
1675 \r
1676     /**\r
1677      * Empty constructor.  Public for compatibily with JDK which lets the\r
1678      * compiler generate a default public constructor even though this is\r
1679      * an abstract class.\r
1680      * @stable ICU 2.6\r
1681      */\r
1682     public NumberFormat() {\r
1683     }\r
1684 \r
1685     // new in ICU4J 3.6\r
1686     private boolean parseStrict;\r
1687 \r
1688     /**\r
1689      * The instances of this inner class are used as attribute keys and values\r
1690      * in AttributedCharacterIterator that\r
1691      * NumberFormat.formatToCharacterIterator() method returns.\r
1692      * <p>\r
1693      * There is no public constructor to this class, the only instances are the\r
1694      * constants defined here.\r
1695      * <p>\r
1696      * @stable ICU 3.6\r
1697      */\r
1698     public static class Field extends Format.Field {\r
1699         // generated by serialver from JDK 1.4.1_01\r
1700         static final long serialVersionUID = -4516273749929385842L;\r
1701 \r
1702         /**\r
1703          * @stable ICU 3.6\r
1704          */\r
1705         public static final Field SIGN = new Field("sign");\r
1706 \r
1707         /**\r
1708          * @stable ICU 3.6\r
1709          */\r
1710         public static final Field INTEGER = new Field("integer");\r
1711 \r
1712         /**\r
1713          * @stable ICU 3.6\r
1714          */\r
1715         public static final Field FRACTION = new Field("fraction");\r
1716 \r
1717         /**\r
1718          * @stable ICU 3.6\r
1719          */\r
1720         public static final Field EXPONENT = new Field("exponent");\r
1721 \r
1722         /**\r
1723          * @stable ICU 3.6\r
1724          */\r
1725         public static final Field EXPONENT_SIGN = new Field("exponent sign");\r
1726 \r
1727         /**\r
1728          * @stable ICU 3.6\r
1729          */\r
1730         public static final Field EXPONENT_SYMBOL = new Field("exponent symbol");\r
1731 \r
1732         /**\r
1733          * @stable ICU 3.6\r
1734          */\r
1735         public static final Field DECIMAL_SEPARATOR = new Field("decimal separator");\r
1736         /**\r
1737          * @stable ICU 3.6\r
1738          */\r
1739         public static final Field GROUPING_SEPARATOR = new Field("grouping separator");\r
1740 \r
1741         /**\r
1742          * @stable ICU 3.6\r
1743          */\r
1744         public static final Field PERCENT = new Field("percent");\r
1745 \r
1746         /**\r
1747          * @stable ICU 3.6\r
1748          */\r
1749         public static final Field PERMILLE = new Field("per mille");\r
1750 \r
1751         /**\r
1752          * @stable ICU 3.6\r
1753          */\r
1754         public static final Field CURRENCY = new Field("currency");\r
1755 \r
1756         /**\r
1757          * Constructs a new instance of NumberFormat.Field with the given field\r
1758          * name.\r
1759          * @stable ICU 3.6\r
1760          */\r
1761         protected Field(String fieldName) {\r
1762             super(fieldName);\r
1763         }\r
1764 \r
1765         /**\r
1766          * serizalization method resolve instances to the constant\r
1767          * NumberFormat.Field values\r
1768          * @stable ICU 3.6\r
1769          */\r
1770         protected Object readResolve() throws InvalidObjectException {\r
1771             if (this.getName().equals(INTEGER.getName()))\r
1772                 return INTEGER;\r
1773             if (this.getName().equals(FRACTION.getName()))\r
1774                 return FRACTION;\r
1775             if (this.getName().equals(EXPONENT.getName()))\r
1776                 return EXPONENT;\r
1777             if (this.getName().equals(EXPONENT_SIGN.getName()))\r
1778                 return EXPONENT_SIGN;\r
1779             if (this.getName().equals(EXPONENT_SYMBOL.getName()))\r
1780                 return EXPONENT_SYMBOL;\r
1781             if (this.getName().equals(CURRENCY.getName()))\r
1782                 return CURRENCY;\r
1783             if (this.getName().equals(DECIMAL_SEPARATOR.getName()))\r
1784                 return DECIMAL_SEPARATOR;\r
1785             if (this.getName().equals(GROUPING_SEPARATOR.getName()))\r
1786                 return GROUPING_SEPARATOR;\r
1787             if (this.getName().equals(PERCENT.getName()))\r
1788                 return PERCENT;\r
1789             if (this.getName().equals(PERMILLE.getName()))\r
1790                 return PERMILLE;\r
1791             if (this.getName().equals(SIGN.getName()))\r
1792                 return SIGN;\r
1793 \r
1794             throw new InvalidObjectException("An invalid object.");\r
1795         }\r
1796     }\r
1797 }\r