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