1 /*
   2  * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  28  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  29  *
  30  *   The original version of this source code and documentation is copyrighted
  31  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  32  * materials are provided under terms of a License Agreement between Taligent
  33  * and Sun. This technology is protected by multiple US and International
  34  * patents. This notice and attribution to Taligent may not be removed.
  35  *   Taligent is a registered trademark of Taligent, Inc.
  36  *
  37  */
  38 
  39 package java.text;
  40 
  41 import java.io.InvalidObjectException;
  42 import java.io.IOException;
  43 import java.io.ObjectInputStream;
  44 import java.io.ObjectOutputStream;
  45 import java.math.BigInteger;
  46 import java.math.RoundingMode;
  47 import java.text.spi.NumberFormatProvider;
  48 import java.util.Currency;
  49 import java.util.HashMap;
  50 import java.util.Locale;
  51 import java.util.Map;
  52 import java.util.Objects;
  53 import java.util.concurrent.atomic.AtomicInteger;
  54 import java.util.concurrent.atomic.AtomicLong;
  55 import sun.util.locale.provider.LocaleProviderAdapter;
  56 import sun.util.locale.provider.LocaleServiceProviderPool;
  57 
  58 /**
  59  * <code>NumberFormat</code> is the abstract base class for all number
  60  * formats. This class provides the interface for formatting and parsing
  61  * numbers. <code>NumberFormat</code> also provides methods for determining
  62  * which locales have number formats, and what their names are.
  63  *
  64  * <p>
  65  * <code>NumberFormat</code> helps you to format and parse numbers for any locale.
  66  * Your code can be completely independent of the locale conventions for
  67  * decimal points, thousands-separators, or even the particular decimal
  68  * digits used, or whether the number format is even decimal.
  69  *
  70  * <p>
  71  * To format a number for the current Locale, use one of the factory
  72  * class methods:
  73  * <blockquote>
  74  * <pre>{@code
  75  * myString = NumberFormat.getInstance().format(myNumber);
  76  * }</pre>
  77  * </blockquote>
  78  * If you are formatting multiple numbers, it is
  79  * more efficient to get the format and use it multiple times so that
  80  * the system doesn't have to fetch the information about the local
  81  * language and country conventions multiple times.
  82  * <blockquote>
  83  * <pre>{@code
  84  * NumberFormat nf = NumberFormat.getInstance();
  85  * for (int i = 0; i < myNumber.length; ++i) {
  86  *     output.println(nf.format(myNumber[i]) + "; ");
  87  * }
  88  * }</pre>
  89  * </blockquote>
  90  * To format a number for a different Locale, specify it in the
  91  * call to <code>getInstance</code>.
  92  * <blockquote>
  93  * <pre>{@code
  94  * NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
  95  * }</pre>
  96  * </blockquote>
  97  *
  98  * <p>If the locale contains "nu" (numbers) and/or "rg" (region override)
  99  * <a href="../util/Locale.html#def_locale_extension">Unicode extensions</a>,
 100  * the decimal digits, and/or the country used for formatting are overridden.
 101  * If both "nu" and "rg" are specified, the decimal digits from the "nu"
 102  * extension supersedes the implicit one from the "rg" extension.
 103  *
 104  * <p>You can also use a {@code NumberFormat} to parse numbers:
 105  * <blockquote>
 106  * <pre>{@code
 107  * myNumber = nf.parse(myString);
 108  * }</pre>
 109  * </blockquote>
 110  * Use <code>getInstance</code> or <code>getNumberInstance</code> to get the
 111  * normal number format. Use <code>getIntegerInstance</code> to get an
 112  * integer number format. Use <code>getCurrencyInstance</code> to get the
 113  * currency number format. Use {@code getCompactNumberInstance} to get the
 114  * compact number format to format a number in shorter form. For example,
 115  * {@code 2000} can be formatted as {@code "2K"} in
 116  * {@link java.util.Locale#US US locale}. Use <code>getPercentInstance</code>
 117  * to get a format for displaying percentages. With this format, a fraction
 118  * like 0.53 is displayed as 53%.
 119  *
 120  * <p>
 121  * You can also control the display of numbers with such methods as
 122  * <code>setMinimumFractionDigits</code>.
 123  * If you want even more control over the format or parsing,
 124  * or want to give your users more control,
 125  * you can try casting the <code>NumberFormat</code> you get from the factory methods
 126  * to a {@code DecimalFormat} or {@code CompactNumberFormat} depending on
 127  * the factory method used. This will work for the vast majority of locales;
 128  * just remember to put it in a <code>try</code> block in case you encounter
 129  * an unusual one.
 130  *
 131  * <p>
 132  * NumberFormat and DecimalFormat are designed such that some controls
 133  * work for formatting and others work for parsing.  The following is
 134  * the detailed description for each these control methods,
 135  * <p>
 136  * setParseIntegerOnly : only affects parsing, e.g.
 137  * if true,  "3456.78" &rarr; 3456 (and leaves the parse position just after index 6)
 138  * if false, "3456.78" &rarr; 3456.78 (and leaves the parse position just after index 8)
 139  * This is independent of formatting.  If you want to not show a decimal point
 140  * where there might be no digits after the decimal point, use
 141  * setDecimalSeparatorAlwaysShown.
 142  * <p>
 143  * setDecimalSeparatorAlwaysShown : only affects formatting, and only where
 144  * there might be no digits after the decimal point, such as with a pattern
 145  * like "#,##0.##", e.g.,
 146  * if true,  3456.00 &rarr; "3,456."
 147  * if false, 3456.00 &rarr; "3456"
 148  * This is independent of parsing.  If you want parsing to stop at the decimal
 149  * point, use setParseIntegerOnly.
 150  *
 151  * <p>
 152  * You can also use forms of the <code>parse</code> and <code>format</code>
 153  * methods with <code>ParsePosition</code> and <code>FieldPosition</code> to
 154  * allow you to:
 155  * <ul>
 156  * <li> progressively parse through pieces of a string
 157  * <li> align the decimal point and other areas
 158  * </ul>
 159  * For example, you can align numbers in two ways:
 160  * <ol>
 161  * <li> If you are using a monospaced font with spacing for alignment,
 162  *      you can pass the <code>FieldPosition</code> in your format call, with
 163  *      <code>field</code> = <code>INTEGER_FIELD</code>. On output,
 164  *      <code>getEndIndex</code> will be set to the offset between the
 165  *      last character of the integer and the decimal. Add
 166  *      (desiredSpaceCount - getEndIndex) spaces at the front of the string.
 167  *
 168  * <li> If you are using proportional fonts,
 169  *      instead of padding with spaces, measure the width
 170  *      of the string in pixels from the start to <code>getEndIndex</code>.
 171  *      Then move the pen by
 172  *      (desiredPixelWidth - widthToAlignmentPoint) before drawing the text.
 173  *      It also works where there is no decimal, but possibly additional
 174  *      characters at the end, e.g., with parentheses in negative
 175  *      numbers: "(12)" for -12.
 176  * </ol>
 177  *
 178  * <h3><a id="synchronization">Synchronization</a></h3>
 179  *
 180  * <p>
 181  * Number formats are generally not synchronized.
 182  * It is recommended to create separate format instances for each thread.
 183  * If multiple threads access a format concurrently, it must be synchronized
 184  * externally.
 185  *
 186  * @implSpec The {@link #format(double, StringBuffer, FieldPosition)},
 187  * {@link #format(long, StringBuffer, FieldPosition)} and
 188  * {@link #parse(String, ParsePosition)} methods may throw
 189  * {@code NullPointerException}, if any of their parameter is {@code null}.
 190  * The subclass may provide its own implementation and specification about
 191  * {@code NullPointerException}.
 192  *
 193  * <p>
 194  * The default implementation provides rounding modes defined
 195  * in {@link java.math.RoundingMode} for formatting numbers. It
 196  * uses the {@linkplain java.math.RoundingMode#HALF_EVEN
 197  * round half-even algorithm}. To change the rounding mode use
 198  * {@link #setRoundingMode(java.math.RoundingMode) setRoundingMode}.
 199  * The {@code NumberFormat} returned by the static factory methods is
 200  * configured to round floating point numbers using half-even
 201  * rounding (see {@link java.math.RoundingMode#HALF_EVEN
 202  * RoundingMode.HALF_EVEN}) for formatting.
 203  *
 204  * @see          DecimalFormat
 205  * @see          ChoiceFormat
 206  * @see          CompactNumberFormat
 207  * @author       Mark Davis
 208  * @author       Helena Shih
 209  * @since 1.1
 210  */
 211 public abstract class NumberFormat extends Format  {
 212 
 213     /**
 214      * Field constant used to construct a FieldPosition object. Signifies that
 215      * the position of the integer part of a formatted number should be returned.
 216      * @see java.text.FieldPosition
 217      */
 218     public static final int INTEGER_FIELD = 0;
 219 
 220     /**
 221      * Field constant used to construct a FieldPosition object. Signifies that
 222      * the position of the fraction part of a formatted number should be returned.
 223      * @see java.text.FieldPosition
 224      */
 225     public static final int FRACTION_FIELD = 1;
 226 
 227     /**
 228      * Sole constructor.  (For invocation by subclass constructors, typically
 229      * implicit.)
 230      */
 231     protected NumberFormat() {
 232     }
 233 
 234     /**
 235      * Formats a number and appends the resulting text to the given string
 236      * buffer.
 237      * The number can be of any subclass of {@link java.lang.Number}.
 238      * <p>
 239      * This implementation extracts the number's value using
 240      * {@link java.lang.Number#longValue()} for all integral type values that
 241      * can be converted to <code>long</code> without loss of information,
 242      * including <code>BigInteger</code> values with a
 243      * {@link java.math.BigInteger#bitLength() bit length} of less than 64,
 244      * and {@link java.lang.Number#doubleValue()} for all other types. It
 245      * then calls
 246      * {@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}
 247      * or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.
 248      * This may result in loss of magnitude information and precision for
 249      * <code>BigInteger</code> and <code>BigDecimal</code> values.
 250      * @param number     the number to format
 251      * @param toAppendTo the <code>StringBuffer</code> to which the formatted
 252      *                   text is to be appended
 253      * @param pos        keeps track on the position of the field within the
 254      *                   returned string. For example, for formatting a number
 255      *                   {@code 1234567.89} in {@code Locale.US} locale,
 256      *                   if the given {@code fieldPosition} is
 257      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 258      *                   and end index of {@code fieldPosition} will be set
 259      *                   to 0 and 9, respectively for the output string
 260      *                   {@code 1,234,567.89}.
 261      * @return           the value passed in as <code>toAppendTo</code>
 262      * @exception        IllegalArgumentException if <code>number</code> is
 263      *                   null or not an instance of <code>Number</code>.
 264      * @exception        NullPointerException if <code>toAppendTo</code> or
 265      *                   <code>pos</code> is null
 266      * @exception        ArithmeticException if rounding is needed with rounding
 267      *                   mode being set to RoundingMode.UNNECESSARY
 268      * @see              java.text.FieldPosition
 269      */
 270     @Override
 271     public StringBuffer format(Object number,
 272                                StringBuffer toAppendTo,
 273                                FieldPosition pos) {
 274         if (number instanceof Long || number instanceof Integer ||
 275             number instanceof Short || number instanceof Byte ||
 276             number instanceof AtomicInteger || number instanceof AtomicLong ||
 277             (number instanceof BigInteger &&
 278              ((BigInteger)number).bitLength() < 64)) {
 279             return format(((Number)number).longValue(), toAppendTo, pos);
 280         } else if (number instanceof Number) {
 281             return format(((Number)number).doubleValue(), toAppendTo, pos);
 282         } else {
 283             throw new IllegalArgumentException("Cannot format given Object as a Number");
 284         }
 285     }
 286 
 287     /**
 288      * Parses text from a string to produce a <code>Number</code>.
 289      * <p>
 290      * The method attempts to parse text starting at the index given by
 291      * <code>pos</code>.
 292      * If parsing succeeds, then the index of <code>pos</code> is updated
 293      * to the index after the last character used (parsing does not necessarily
 294      * use all characters up to the end of the string), and the parsed
 295      * number is returned. The updated <code>pos</code> can be used to
 296      * indicate the starting point for the next call to this method.
 297      * If an error occurs, then the index of <code>pos</code> is not
 298      * changed, the error index of <code>pos</code> is set to the index of
 299      * the character where the error occurred, and null is returned.
 300      * <p>
 301      * See the {@link #parse(String, ParsePosition)} method for more information
 302      * on number parsing.
 303      *
 304      * @param source A <code>String</code>, part of which should be parsed.
 305      * @param pos A <code>ParsePosition</code> object with index and error
 306      *            index information as described above.
 307      * @return A <code>Number</code> parsed from the string. In case of
 308      *         error, returns null.
 309      * @throws NullPointerException if {@code source} or {@code pos} is null.
 310      */
 311     @Override
 312     public final Object parseObject(String source, ParsePosition pos) {
 313         return parse(source, pos);
 314     }
 315 
 316    /**
 317      * Specialization of format.
 318      *
 319      * @param number the double number to format
 320      * @return the formatted String
 321      * @exception        ArithmeticException if rounding is needed with rounding
 322      *                   mode being set to RoundingMode.UNNECESSARY
 323      * @see java.text.Format#format
 324      */
 325     public final String format(double number) {
 326         // Use fast-path for double result if that works
 327         String result = fastFormat(number);
 328         if (result != null)
 329             return result;
 330 
 331         return format(number, new StringBuffer(),
 332                       DontCareFieldPosition.INSTANCE).toString();
 333     }
 334 
 335     /*
 336      * fastFormat() is supposed to be implemented in concrete subclasses only.
 337      * Default implem always returns null.
 338      */
 339     String fastFormat(double number) { return null; }
 340 
 341    /**
 342      * Specialization of format.
 343      *
 344      * @param number the long number to format
 345      * @return the formatted String
 346      * @exception        ArithmeticException if rounding is needed with rounding
 347      *                   mode being set to RoundingMode.UNNECESSARY
 348      * @see java.text.Format#format
 349      */
 350     public final String format(long number) {
 351         return format(number, new StringBuffer(),
 352                       DontCareFieldPosition.INSTANCE).toString();
 353     }
 354 
 355    /**
 356      * Specialization of format.
 357      *
 358      * @param number     the double number to format
 359      * @param toAppendTo the StringBuffer to which the formatted text is to be
 360      *                   appended
 361      * @param pos        keeps track on the position of the field within the
 362      *                   returned string. For example, for formatting a number
 363      *                   {@code 1234567.89} in {@code Locale.US} locale,
 364      *                   if the given {@code fieldPosition} is
 365      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 366      *                   and end index of {@code fieldPosition} will be set
 367      *                   to 0 and 9, respectively for the output string
 368      *                   {@code 1,234,567.89}.
 369      * @return the formatted StringBuffer
 370      * @exception        ArithmeticException if rounding is needed with rounding
 371      *                   mode being set to RoundingMode.UNNECESSARY
 372      * @see java.text.Format#format
 373      */
 374     public abstract StringBuffer format(double number,
 375                                         StringBuffer toAppendTo,
 376                                         FieldPosition pos);
 377 
 378    /**
 379      * Specialization of format.
 380      *
 381      * @param number     the long number to format
 382      * @param toAppendTo the StringBuffer to which the formatted text is to be
 383      *                   appended
 384      * @param pos        keeps track on the position of the field within the
 385      *                   returned string. For example, for formatting a number
 386      *                   {@code 123456789} in {@code Locale.US} locale,
 387      *                   if the given {@code fieldPosition} is
 388      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 389      *                   and end index of {@code fieldPosition} will be set
 390      *                   to 0 and 11, respectively for the output string
 391      *                   {@code 123,456,789}.
 392      * @return the formatted StringBuffer
 393      * @exception        ArithmeticException if rounding is needed with rounding
 394      *                   mode being set to RoundingMode.UNNECESSARY
 395      * @see java.text.Format#format
 396      */
 397     public abstract StringBuffer format(long number,
 398                                         StringBuffer toAppendTo,
 399                                         FieldPosition pos);
 400 
 401    /**
 402      * Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,
 403      * Long.MAX_VALUE] and with no decimals), otherwise a Double.
 404      * If IntegerOnly is set, will stop at a decimal
 405      * point (or equivalent; e.g., for rational numbers "1 2/3", will stop
 406      * after the 1).
 407      * Does not throw an exception; if no object can be parsed, index is
 408      * unchanged!
 409      *
 410      * @param source the String to parse
 411      * @param parsePosition the parse position
 412      * @return the parsed value
 413      * @see java.text.NumberFormat#isParseIntegerOnly
 414      * @see java.text.Format#parseObject
 415      */
 416     public abstract Number parse(String source, ParsePosition parsePosition);
 417 
 418     /**
 419      * Parses text from the beginning of the given string to produce a number.
 420      * The method may not use the entire text of the given string.
 421      * <p>
 422      * See the {@link #parse(String, ParsePosition)} method for more information
 423      * on number parsing.
 424      *
 425      * @param source A <code>String</code> whose beginning should be parsed.
 426      * @return A <code>Number</code> parsed from the string.
 427      * @exception ParseException if the beginning of the specified string
 428      *            cannot be parsed.
 429      */
 430     public Number parse(String source) throws ParseException {
 431         ParsePosition parsePosition = new ParsePosition(0);
 432         Number result = parse(source, parsePosition);
 433         if (parsePosition.index == 0) {
 434             throw new ParseException("Unparseable number: \"" + source + "\"",
 435                                      parsePosition.errorIndex);
 436         }
 437         return result;
 438     }
 439 
 440     /**
 441      * Returns true if this format will parse numbers as integers only.
 442      * For example in the English locale, with ParseIntegerOnly true, the
 443      * string "1234." would be parsed as the integer value 1234 and parsing
 444      * would stop at the "." character.  Of course, the exact format accepted
 445      * by the parse operation is locale dependent and determined by sub-classes
 446      * of NumberFormat.
 447      *
 448      * @return {@code true} if numbers should be parsed as integers only;
 449      *         {@code false} otherwise
 450      */
 451     public boolean isParseIntegerOnly() {
 452         return parseIntegerOnly;
 453     }
 454 
 455     /**
 456      * Sets whether or not numbers should be parsed as integers only.
 457      *
 458      * @param value {@code true} if numbers should be parsed as integers only;
 459      *              {@code false} otherwise
 460      * @see #isParseIntegerOnly
 461      */
 462     public void setParseIntegerOnly(boolean value) {
 463         parseIntegerOnly = value;
 464     }
 465 
 466     //============== Locale Stuff =====================
 467 
 468     /**
 469      * Returns a general-purpose number format for the current default
 470      * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
 471      * This is the same as calling
 472      * {@link #getNumberInstance() getNumberInstance()}.
 473      *
 474      * @return the {@code NumberFormat} instance for general-purpose number
 475      * formatting
 476      */
 477     public static final NumberFormat getInstance() {
 478         return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, NUMBERSTYLE);
 479     }
 480 
 481     /**
 482      * Returns a general-purpose number format for the specified locale.
 483      * This is the same as calling
 484      * {@link #getNumberInstance(java.util.Locale) getNumberInstance(inLocale)}.
 485      *
 486      * @param inLocale the desired locale
 487      * @return the {@code NumberFormat} instance for general-purpose number
 488      * formatting
 489      */
 490     public static NumberFormat getInstance(Locale inLocale) {
 491         return getInstance(inLocale, null, NUMBERSTYLE);
 492     }
 493 
 494     /**
 495      * Returns a general-purpose number format for the current default
 496      * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
 497      * <p>This is equivalent to calling
 498      * {@link #getNumberInstance(Locale)
 499      *     getNumberInstance(Locale.getDefault(Locale.Category.FORMAT))}.
 500      *
 501      * @return the {@code NumberFormat} instance for general-purpose number
 502      * formatting
 503      * @see java.util.Locale#getDefault(java.util.Locale.Category)
 504      * @see java.util.Locale.Category#FORMAT
 505      */
 506     public static final NumberFormat getNumberInstance() {
 507         return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, NUMBERSTYLE);
 508     }
 509 
 510     /**
 511      * Returns a general-purpose number format for the specified locale.
 512      *
 513      * @param inLocale the desired locale
 514      * @return the {@code NumberFormat} instance for general-purpose number
 515      * formatting
 516      */
 517     public static NumberFormat getNumberInstance(Locale inLocale) {
 518         return getInstance(inLocale, null, NUMBERSTYLE);
 519     }
 520 
 521     /**
 522      * Returns an integer number format for the current default
 523      * {@link java.util.Locale.Category#FORMAT FORMAT} locale. The
 524      * returned number format is configured to round floating point numbers
 525      * to the nearest integer using half-even rounding (see {@link
 526      * java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}) for formatting,
 527      * and to parse only the integer part of an input string (see {@link
 528      * #isParseIntegerOnly isParseIntegerOnly}).
 529      * <p>This is equivalent to calling
 530      * {@link #getIntegerInstance(Locale)
 531      *     getIntegerInstance(Locale.getDefault(Locale.Category.FORMAT))}.
 532      *
 533      * @see #getRoundingMode()
 534      * @see java.util.Locale#getDefault(java.util.Locale.Category)
 535      * @see java.util.Locale.Category#FORMAT
 536      * @return a number format for integer values
 537      * @since 1.4
 538      */
 539     public static final NumberFormat getIntegerInstance() {
 540         return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, INTEGERSTYLE);
 541     }
 542 
 543     /**
 544      * Returns an integer number format for the specified locale. The
 545      * returned number format is configured to round floating point numbers
 546      * to the nearest integer using half-even rounding (see {@link
 547      * java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}) for formatting,
 548      * and to parse only the integer part of an input string (see {@link
 549      * #isParseIntegerOnly isParseIntegerOnly}).
 550      *
 551      * @param inLocale the desired locale
 552      * @see #getRoundingMode()
 553      * @return a number format for integer values
 554      * @since 1.4
 555      */
 556     public static NumberFormat getIntegerInstance(Locale inLocale) {
 557         return getInstance(inLocale, null, INTEGERSTYLE);
 558     }
 559 
 560     /**
 561      * Returns a currency format for the current default
 562      * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
 563      * <p>This is equivalent to calling
 564      * {@link #getCurrencyInstance(Locale)
 565      *     getCurrencyInstance(Locale.getDefault(Locale.Category.FORMAT))}.
 566      *
 567      * @return the {@code NumberFormat} instance for currency formatting
 568      * @see java.util.Locale#getDefault(java.util.Locale.Category)
 569      * @see java.util.Locale.Category#FORMAT
 570      */
 571     public static final NumberFormat getCurrencyInstance() {
 572         return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, CURRENCYSTYLE);
 573     }
 574 
 575     /**
 576      * Returns a currency format for the specified locale.
 577      *
 578      * @param inLocale the desired locale
 579      * @return the {@code NumberFormat} instance for currency formatting
 580      */
 581     public static NumberFormat getCurrencyInstance(Locale inLocale) {
 582         return getInstance(inLocale, null, CURRENCYSTYLE);
 583     }
 584 
 585     /**
 586      * Returns a percentage format for the current default
 587      * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
 588      * <p>This is equivalent to calling
 589      * {@link #getPercentInstance(Locale)
 590      *     getPercentInstance(Locale.getDefault(Locale.Category.FORMAT))}.
 591      *
 592      * @return the {@code NumberFormat} instance for percentage formatting
 593      * @see java.util.Locale#getDefault(java.util.Locale.Category)
 594      * @see java.util.Locale.Category#FORMAT
 595      */
 596     public static final NumberFormat getPercentInstance() {
 597         return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, PERCENTSTYLE);
 598     }
 599 
 600     /**
 601      * Returns a percentage format for the specified locale.
 602      *
 603      * @param inLocale the desired locale
 604      * @return the {@code NumberFormat} instance for percentage formatting
 605      */
 606     public static NumberFormat getPercentInstance(Locale inLocale) {
 607         return getInstance(inLocale, null, PERCENTSTYLE);
 608     }
 609 
 610     /**
 611      * Returns a scientific format for the current default locale.
 612      */
 613     /*public*/ final static NumberFormat getScientificInstance() {
 614         return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, SCIENTIFICSTYLE);
 615     }
 616 
 617     /**
 618      * Returns a scientific format for the specified locale.
 619      *
 620      * @param inLocale the desired locale
 621      */
 622     /*public*/ static NumberFormat getScientificInstance(Locale inLocale) {
 623         return getInstance(inLocale, null, SCIENTIFICSTYLE);
 624     }
 625 
 626     /**
 627      * Returns a compact number format for the default
 628      * {@link java.util.Locale.Category#FORMAT FORMAT} locale with
 629      * {@link NumberFormat.Style#SHORT "SHORT"} format style.
 630      *
 631      * @return A {@code NumberFormat} instance for compact number
 632      *         formatting
 633      *
 634      * @see CompactNumberFormat
 635      * @see NumberFormat.Style
 636      * @see java.util.Locale#getDefault(java.util.Locale.Category)
 637      * @see java.util.Locale.Category#FORMAT
 638      * @since 12
 639      */
 640     public static NumberFormat getCompactNumberInstance() {
 641         return getInstance(Locale.getDefault(
 642                 Locale.Category.FORMAT), NumberFormat.Style.SHORT, COMPACTSTYLE);
 643     }
 644 
 645     /**
 646      * Returns a compact number format for the specified {@link java.util.Locale locale}
 647      * and {@link NumberFormat.Style formatStyle}.
 648      *
 649      * @param locale the desired locale
 650      * @param formatStyle the style for formatting a number
 651      * @return A {@code NumberFormat} instance for compact number
 652      *         formatting
 653      * @throws NullPointerException if {@code locale} or {@code formatStyle}
 654      *                              is {@code null}
 655      *
 656      * @see CompactNumberFormat
 657      * @see NumberFormat.Style
 658      * @see java.util.Locale
 659      * @since 12
 660      */
 661     public static NumberFormat getCompactNumberInstance(Locale locale,
 662             NumberFormat.Style formatStyle) {
 663 
 664         Objects.requireNonNull(locale);
 665         Objects.requireNonNull(formatStyle);
 666         return getInstance(locale, formatStyle, COMPACTSTYLE);
 667     }
 668 
 669     /**
 670      * Returns an array of all locales for which the
 671      * <code>get*Instance</code> methods of this class can return
 672      * localized instances.
 673      * The returned array represents the union of locales supported by the Java
 674      * runtime and by installed
 675      * {@link java.text.spi.NumberFormatProvider NumberFormatProvider} implementations.
 676      * It must contain at least a <code>Locale</code> instance equal to
 677      * {@link java.util.Locale#US Locale.US}.
 678      *
 679      * @return An array of locales for which localized
 680      *         <code>NumberFormat</code> instances are available.
 681      */
 682     public static Locale[] getAvailableLocales() {
 683         LocaleServiceProviderPool pool =
 684             LocaleServiceProviderPool.getPool(NumberFormatProvider.class);
 685         return pool.getAvailableLocales();
 686     }
 687 
 688     /**
 689      * Overrides hashCode.
 690      */
 691     @Override
 692     public int hashCode() {
 693         return maximumIntegerDigits * 37 + maxFractionDigits;
 694         // just enough fields for a reasonable distribution
 695     }
 696 
 697     /**
 698      * Overrides equals.
 699      */
 700     @Override
 701     public boolean equals(Object obj) {
 702         if (obj == null) {
 703             return false;
 704         }
 705         if (this == obj) {
 706             return true;
 707         }
 708         if (getClass() != obj.getClass()) {
 709             return false;
 710         }
 711         NumberFormat other = (NumberFormat) obj;
 712         return (maximumIntegerDigits == other.maximumIntegerDigits
 713             && minimumIntegerDigits == other.minimumIntegerDigits
 714             && maximumFractionDigits == other.maximumFractionDigits
 715             && minimumFractionDigits == other.minimumFractionDigits
 716             && groupingUsed == other.groupingUsed
 717             && parseIntegerOnly == other.parseIntegerOnly);
 718     }
 719 
 720     /**
 721      * Overrides Cloneable.
 722      */
 723     @Override
 724     public Object clone() {
 725         NumberFormat other = (NumberFormat) super.clone();
 726         return other;
 727     }
 728 
 729     /**
 730      * Returns true if grouping is used in this format. For example, in the
 731      * English locale, with grouping on, the number 1234567 might be formatted
 732      * as "1,234,567". The grouping separator as well as the size of each group
 733      * is locale dependent and is determined by sub-classes of NumberFormat.
 734      *
 735      * @return {@code true} if grouping is used;
 736      *         {@code false} otherwise
 737      * @see #setGroupingUsed
 738      */
 739     public boolean isGroupingUsed() {
 740         return groupingUsed;
 741     }
 742 
 743     /**
 744      * Set whether or not grouping will be used in this format.
 745      *
 746      * @param newValue {@code true} if grouping is used;
 747      *                 {@code false} otherwise
 748      * @see #isGroupingUsed
 749      */
 750     public void setGroupingUsed(boolean newValue) {
 751         groupingUsed = newValue;
 752     }
 753 
 754     /**
 755      * Returns the maximum number of digits allowed in the integer portion of a
 756      * number.
 757      *
 758      * @return the maximum number of digits
 759      * @see #setMaximumIntegerDigits
 760      */
 761     public int getMaximumIntegerDigits() {
 762         return maximumIntegerDigits;
 763     }
 764 
 765     /**
 766      * Sets the maximum number of digits allowed in the integer portion of a
 767      * number. maximumIntegerDigits must be &ge; minimumIntegerDigits.  If the
 768      * new value for maximumIntegerDigits is less than the current value
 769      * of minimumIntegerDigits, then minimumIntegerDigits will also be set to
 770      * the new value.
 771      *
 772      * @param newValue the maximum number of integer digits to be shown; if
 773      * less than zero, then zero is used. The concrete subclass may enforce an
 774      * upper limit to this value appropriate to the numeric type being formatted.
 775      * @see #getMaximumIntegerDigits
 776      */
 777     public void setMaximumIntegerDigits(int newValue) {
 778         maximumIntegerDigits = Math.max(0,newValue);
 779         if (minimumIntegerDigits > maximumIntegerDigits) {
 780             minimumIntegerDigits = maximumIntegerDigits;
 781         }
 782     }
 783 
 784     /**
 785      * Returns the minimum number of digits allowed in the integer portion of a
 786      * number.
 787      *
 788      * @return the minimum number of digits
 789      * @see #setMinimumIntegerDigits
 790      */
 791     public int getMinimumIntegerDigits() {
 792         return minimumIntegerDigits;
 793     }
 794 
 795     /**
 796      * Sets the minimum number of digits allowed in the integer portion of a
 797      * number. minimumIntegerDigits must be &le; maximumIntegerDigits.  If the
 798      * new value for minimumIntegerDigits exceeds the current value
 799      * of maximumIntegerDigits, then maximumIntegerDigits will also be set to
 800      * the new value
 801      *
 802      * @param newValue the minimum number of integer digits to be shown; if
 803      * less than zero, then zero is used. The concrete subclass may enforce an
 804      * upper limit to this value appropriate to the numeric type being formatted.
 805      * @see #getMinimumIntegerDigits
 806      */
 807     public void setMinimumIntegerDigits(int newValue) {
 808         minimumIntegerDigits = Math.max(0,newValue);
 809         if (minimumIntegerDigits > maximumIntegerDigits) {
 810             maximumIntegerDigits = minimumIntegerDigits;
 811         }
 812     }
 813 
 814     /**
 815      * Returns the maximum number of digits allowed in the fraction portion of a
 816      * number.
 817      *
 818      * @return the maximum number of digits.
 819      * @see #setMaximumFractionDigits
 820      */
 821     public int getMaximumFractionDigits() {
 822         return maximumFractionDigits;
 823     }
 824 
 825     /**
 826      * Sets the maximum number of digits allowed in the fraction portion of a
 827      * number. maximumFractionDigits must be &ge; minimumFractionDigits.  If the
 828      * new value for maximumFractionDigits is less than the current value
 829      * of minimumFractionDigits, then minimumFractionDigits will also be set to
 830      * the new value.
 831      *
 832      * @param newValue the maximum number of fraction digits to be shown; if
 833      * less than zero, then zero is used. The concrete subclass may enforce an
 834      * upper limit to this value appropriate to the numeric type being formatted.
 835      * @see #getMaximumFractionDigits
 836      */
 837     public void setMaximumFractionDigits(int newValue) {
 838         maximumFractionDigits = Math.max(0,newValue);
 839         if (maximumFractionDigits < minimumFractionDigits) {
 840             minimumFractionDigits = maximumFractionDigits;
 841         }
 842     }
 843 
 844     /**
 845      * Returns the minimum number of digits allowed in the fraction portion of a
 846      * number.
 847      *
 848      * @return the minimum number of digits
 849      * @see #setMinimumFractionDigits
 850      */
 851     public int getMinimumFractionDigits() {
 852         return minimumFractionDigits;
 853     }
 854 
 855     /**
 856      * Sets the minimum number of digits allowed in the fraction portion of a
 857      * number. minimumFractionDigits must be &le; maximumFractionDigits.  If the
 858      * new value for minimumFractionDigits exceeds the current value
 859      * of maximumFractionDigits, then maximumIntegerDigits will also be set to
 860      * the new value
 861      *
 862      * @param newValue the minimum number of fraction digits to be shown; if
 863      * less than zero, then zero is used. The concrete subclass may enforce an
 864      * upper limit to this value appropriate to the numeric type being formatted.
 865      * @see #getMinimumFractionDigits
 866      */
 867     public void setMinimumFractionDigits(int newValue) {
 868         minimumFractionDigits = Math.max(0,newValue);
 869         if (maximumFractionDigits < minimumFractionDigits) {
 870             maximumFractionDigits = minimumFractionDigits;
 871         }
 872     }
 873 
 874     /**
 875      * Gets the currency used by this number format when formatting
 876      * currency values. The initial value is derived in a locale dependent
 877      * way. The returned value may be null if no valid
 878      * currency could be determined and no currency has been set using
 879      * {@link #setCurrency(java.util.Currency) setCurrency}.
 880      * <p>
 881      * The default implementation throws
 882      * <code>UnsupportedOperationException</code>.
 883      *
 884      * @return the currency used by this number format, or <code>null</code>
 885      * @exception UnsupportedOperationException if the number format class
 886      * doesn't implement currency formatting
 887      * @since 1.4
 888      */
 889     public Currency getCurrency() {
 890         throw new UnsupportedOperationException();
 891     }
 892 
 893     /**
 894      * Sets the currency used by this number format when formatting
 895      * currency values. This does not update the minimum or maximum
 896      * number of fraction digits used by the number format.
 897      * <p>
 898      * The default implementation throws
 899      * <code>UnsupportedOperationException</code>.
 900      *
 901      * @param currency the new currency to be used by this number format
 902      * @exception UnsupportedOperationException if the number format class
 903      * doesn't implement currency formatting
 904      * @exception NullPointerException if <code>currency</code> is null
 905      * @since 1.4
 906      */
 907     public void setCurrency(Currency currency) {
 908         throw new UnsupportedOperationException();
 909     }
 910 
 911     /**
 912      * Gets the {@link java.math.RoundingMode} used in this NumberFormat.
 913      * The default implementation of this method in NumberFormat
 914      * always throws {@link java.lang.UnsupportedOperationException}.
 915      * Subclasses which handle different rounding modes should override
 916      * this method.
 917      *
 918      * @exception UnsupportedOperationException The default implementation
 919      *     always throws this exception
 920      * @return The <code>RoundingMode</code> used for this NumberFormat.
 921      * @see #setRoundingMode(RoundingMode)
 922      * @since 1.6
 923      */
 924     public RoundingMode getRoundingMode() {
 925         throw new UnsupportedOperationException();
 926     }
 927 
 928     /**
 929      * Sets the {@link java.math.RoundingMode} used in this NumberFormat.
 930      * The default implementation of this method in NumberFormat always
 931      * throws {@link java.lang.UnsupportedOperationException}.
 932      * Subclasses which handle different rounding modes should override
 933      * this method.
 934      *
 935      * @exception UnsupportedOperationException The default implementation
 936      *     always throws this exception
 937      * @exception NullPointerException if <code>roundingMode</code> is null
 938      * @param roundingMode The <code>RoundingMode</code> to be used
 939      * @see #getRoundingMode()
 940      * @since 1.6
 941      */
 942     public void setRoundingMode(RoundingMode roundingMode) {
 943         throw new UnsupportedOperationException();
 944     }
 945 
 946     // =======================privates===============================
 947 
 948     private static NumberFormat getInstance(Locale desiredLocale,
 949                                             Style formatStyle, int choice) {
 950         LocaleProviderAdapter adapter;
 951         adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class,
 952                 desiredLocale);
 953         NumberFormat numberFormat = getInstance(adapter, desiredLocale,
 954                 formatStyle, choice);
 955         if (numberFormat == null) {
 956             numberFormat = getInstance(LocaleProviderAdapter.forJRE(),
 957                     desiredLocale, formatStyle, choice);
 958         }
 959         return numberFormat;
 960     }
 961 
 962     private static NumberFormat getInstance(LocaleProviderAdapter adapter,
 963                                             Locale locale, Style formatStyle,
 964                                             int choice) {
 965         NumberFormatProvider provider = adapter.getNumberFormatProvider();
 966         NumberFormat numberFormat = null;
 967         switch (choice) {
 968         case NUMBERSTYLE:
 969             numberFormat = provider.getNumberInstance(locale);
 970             break;
 971         case PERCENTSTYLE:
 972             numberFormat = provider.getPercentInstance(locale);
 973             break;
 974         case CURRENCYSTYLE:
 975             numberFormat = provider.getCurrencyInstance(locale);
 976             break;
 977         case INTEGERSTYLE:
 978             numberFormat = provider.getIntegerInstance(locale);
 979             break;
 980         case COMPACTSTYLE:
 981             numberFormat = provider.getCompactNumberInstance(locale, formatStyle);
 982             break;
 983         }
 984         return numberFormat;
 985     }
 986 
 987     /**
 988      * First, read in the default serializable data.
 989      *
 990      * Then, if <code>serialVersionOnStream</code> is less than 1, indicating that
 991      * the stream was written by JDK 1.1,
 992      * set the <code>int</code> fields such as <code>maximumIntegerDigits</code>
 993      * to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>,
 994      * since the <code>int</code> fields were not present in JDK 1.1.
 995      * Finally, set serialVersionOnStream back to the maximum allowed value so that
 996      * default serialization will work properly if this object is streamed out again.
 997      *
 998      * <p>If <code>minimumIntegerDigits</code> is greater than
 999      * <code>maximumIntegerDigits</code> or <code>minimumFractionDigits</code>
1000      * is greater than <code>maximumFractionDigits</code>, then the stream data
1001      * is invalid and this method throws an <code>InvalidObjectException</code>.
1002      * In addition, if any of these values is negative, then this method throws
1003      * an <code>InvalidObjectException</code>.
1004      *
1005      * @since 1.2
1006      */
1007     private void readObject(ObjectInputStream stream)
1008          throws IOException, ClassNotFoundException
1009     {
1010         stream.defaultReadObject();
1011         if (serialVersionOnStream < 1) {
1012             // Didn't have additional int fields, reassign to use them.
1013             maximumIntegerDigits = maxIntegerDigits;
1014             minimumIntegerDigits = minIntegerDigits;
1015             maximumFractionDigits = maxFractionDigits;
1016             minimumFractionDigits = minFractionDigits;
1017         }
1018         if (minimumIntegerDigits > maximumIntegerDigits ||
1019             minimumFractionDigits > maximumFractionDigits ||
1020             minimumIntegerDigits < 0 || minimumFractionDigits < 0) {
1021             throw new InvalidObjectException("Digit count range invalid");
1022         }
1023         serialVersionOnStream = currentSerialVersion;
1024     }
1025 
1026     /**
1027      * Write out the default serializable data, after first setting
1028      * the <code>byte</code> fields such as <code>maxIntegerDigits</code> to be
1029      * equal to the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1030      * (or to <code>Byte.MAX_VALUE</code>, whichever is smaller), for compatibility
1031      * with the JDK 1.1 version of the stream format.
1032      *
1033      * @since 1.2
1034      */
1035     private void writeObject(ObjectOutputStream stream)
1036          throws IOException
1037     {
1038         maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ?
1039                            Byte.MAX_VALUE : (byte)maximumIntegerDigits;
1040         minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ?
1041                            Byte.MAX_VALUE : (byte)minimumIntegerDigits;
1042         maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ?
1043                             Byte.MAX_VALUE : (byte)maximumFractionDigits;
1044         minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ?
1045                             Byte.MAX_VALUE : (byte)minimumFractionDigits;
1046         stream.defaultWriteObject();
1047     }
1048 
1049     // Constants used by factory methods to specify a style of format.
1050     private static final int NUMBERSTYLE = 0;
1051     private static final int CURRENCYSTYLE = 1;
1052     private static final int PERCENTSTYLE = 2;
1053     private static final int SCIENTIFICSTYLE = 3;
1054     private static final int INTEGERSTYLE = 4;
1055     private static final int COMPACTSTYLE = 5;
1056 
1057     /**
1058      * True if the grouping (i.e. thousands) separator is used when
1059      * formatting and parsing numbers.
1060      *
1061      * @serial
1062      * @see #isGroupingUsed
1063      */
1064     private boolean groupingUsed = true;
1065 
1066     /**
1067      * The maximum number of digits allowed in the integer portion of a
1068      * number.  <code>maxIntegerDigits</code> must be greater than or equal to
1069      * <code>minIntegerDigits</code>.
1070      * <p>
1071      * <strong>Note:</strong> This field exists only for serialization
1072      * compatibility with JDK 1.1.  In Java platform 2 v1.2 and higher, the new
1073      * <code>int</code> field <code>maximumIntegerDigits</code> is used instead.
1074      * When writing to a stream, <code>maxIntegerDigits</code> is set to
1075      * <code>maximumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,
1076      * whichever is smaller.  When reading from a stream, this field is used
1077      * only if <code>serialVersionOnStream</code> is less than 1.
1078      *
1079      * @serial
1080      * @see #getMaximumIntegerDigits
1081      */
1082     private byte    maxIntegerDigits = 40;
1083 
1084     /**
1085      * The minimum number of digits allowed in the integer portion of a
1086      * number.  <code>minimumIntegerDigits</code> must be less than or equal to
1087      * <code>maximumIntegerDigits</code>.
1088      * <p>
1089      * <strong>Note:</strong> This field exists only for serialization
1090      * compatibility with JDK 1.1.  In Java platform 2 v1.2 and higher, the new
1091      * <code>int</code> field <code>minimumIntegerDigits</code> is used instead.
1092      * When writing to a stream, <code>minIntegerDigits</code> is set to
1093      * <code>minimumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,
1094      * whichever is smaller.  When reading from a stream, this field is used
1095      * only if <code>serialVersionOnStream</code> is less than 1.
1096      *
1097      * @serial
1098      * @see #getMinimumIntegerDigits
1099      */
1100     private byte    minIntegerDigits = 1;
1101 
1102     /**
1103      * The maximum number of digits allowed in the fractional portion of a
1104      * number.  <code>maximumFractionDigits</code> must be greater than or equal to
1105      * <code>minimumFractionDigits</code>.
1106      * <p>
1107      * <strong>Note:</strong> This field exists only for serialization
1108      * compatibility with JDK 1.1.  In Java platform 2 v1.2 and higher, the new
1109      * <code>int</code> field <code>maximumFractionDigits</code> is used instead.
1110      * When writing to a stream, <code>maxFractionDigits</code> is set to
1111      * <code>maximumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,
1112      * whichever is smaller.  When reading from a stream, this field is used
1113      * only if <code>serialVersionOnStream</code> is less than 1.
1114      *
1115      * @serial
1116      * @see #getMaximumFractionDigits
1117      */
1118     private byte    maxFractionDigits = 3;    // invariant, >= minFractionDigits
1119 
1120     /**
1121      * The minimum number of digits allowed in the fractional portion of a
1122      * number.  <code>minimumFractionDigits</code> must be less than or equal to
1123      * <code>maximumFractionDigits</code>.
1124      * <p>
1125      * <strong>Note:</strong> This field exists only for serialization
1126      * compatibility with JDK 1.1.  In Java platform 2 v1.2 and higher, the new
1127      * <code>int</code> field <code>minimumFractionDigits</code> is used instead.
1128      * When writing to a stream, <code>minFractionDigits</code> is set to
1129      * <code>minimumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,
1130      * whichever is smaller.  When reading from a stream, this field is used
1131      * only if <code>serialVersionOnStream</code> is less than 1.
1132      *
1133      * @serial
1134      * @see #getMinimumFractionDigits
1135      */
1136     private byte    minFractionDigits = 0;
1137 
1138     /**
1139      * True if this format will parse numbers as integers only.
1140      *
1141      * @serial
1142      * @see #isParseIntegerOnly
1143      */
1144     private boolean parseIntegerOnly = false;
1145 
1146     // new fields for 1.2.  byte is too small for integer digits.
1147 
1148     /**
1149      * The maximum number of digits allowed in the integer portion of a
1150      * number.  <code>maximumIntegerDigits</code> must be greater than or equal to
1151      * <code>minimumIntegerDigits</code>.
1152      *
1153      * @serial
1154      * @since 1.2
1155      * @see #getMaximumIntegerDigits
1156      */
1157     private int    maximumIntegerDigits = 40;
1158 
1159     /**
1160      * The minimum number of digits allowed in the integer portion of a
1161      * number.  <code>minimumIntegerDigits</code> must be less than or equal to
1162      * <code>maximumIntegerDigits</code>.
1163      *
1164      * @serial
1165      * @since 1.2
1166      * @see #getMinimumIntegerDigits
1167      */
1168     private int    minimumIntegerDigits = 1;
1169 
1170     /**
1171      * The maximum number of digits allowed in the fractional portion of a
1172      * number.  <code>maximumFractionDigits</code> must be greater than or equal to
1173      * <code>minimumFractionDigits</code>.
1174      *
1175      * @serial
1176      * @since 1.2
1177      * @see #getMaximumFractionDigits
1178      */
1179     private int    maximumFractionDigits = 3;    // invariant, >= minFractionDigits
1180 
1181     /**
1182      * The minimum number of digits allowed in the fractional portion of a
1183      * number.  <code>minimumFractionDigits</code> must be less than or equal to
1184      * <code>maximumFractionDigits</code>.
1185      *
1186      * @serial
1187      * @since 1.2
1188      * @see #getMinimumFractionDigits
1189      */
1190     private int    minimumFractionDigits = 0;
1191 
1192     static final int currentSerialVersion = 1;
1193 
1194     /**
1195      * Describes the version of <code>NumberFormat</code> present on the stream.
1196      * Possible values are:
1197      * <ul>
1198      * <li><b>0</b> (or uninitialized): the JDK 1.1 version of the stream format.
1199      *     In this version, the <code>int</code> fields such as
1200      *     <code>maximumIntegerDigits</code> were not present, and the <code>byte</code>
1201      *     fields such as <code>maxIntegerDigits</code> are used instead.
1202      *
1203      * <li><b>1</b>: the 1.2 version of the stream format.  The values of the
1204      *     <code>byte</code> fields such as <code>maxIntegerDigits</code> are ignored,
1205      *     and the <code>int</code> fields such as <code>maximumIntegerDigits</code>
1206      *     are used instead.
1207      * </ul>
1208      * When streaming out a <code>NumberFormat</code>, the most recent format
1209      * (corresponding to the highest allowable <code>serialVersionOnStream</code>)
1210      * is always written.
1211      *
1212      * @serial
1213      * @since 1.2
1214      */
1215     private int serialVersionOnStream = currentSerialVersion;
1216 
1217     // Removed "implements Cloneable" clause.  Needs to update serialization
1218     // ID for backward compatibility.
1219     static final long serialVersionUID = -2308460125733713944L;
1220 
1221 
1222     //
1223     // class for AttributedCharacterIterator attributes
1224     //
1225     /**
1226      * Defines constants that are used as attribute keys in the
1227      * <code>AttributedCharacterIterator</code> returned
1228      * from <code>NumberFormat.formatToCharacterIterator</code> and as
1229      * field identifiers in <code>FieldPosition</code>.
1230      *
1231      * @since 1.4
1232      */
1233     public static class Field extends Format.Field {
1234 
1235         // Proclaim serial compatibility with 1.4 FCS
1236         private static final long serialVersionUID = 7494728892700160890L;
1237 
1238         // table of all instances in this class, used by readResolve
1239         private static final Map<String, Field> instanceMap = new HashMap<>(11);
1240 
1241         /**
1242          * Creates a Field instance with the specified
1243          * name.
1244          *
1245          * @param name Name of the attribute
1246          */
1247         protected Field(String name) {
1248             super(name);
1249             if (this.getClass() == NumberFormat.Field.class) {
1250                 instanceMap.put(name, this);
1251             }
1252         }
1253 
1254         /**
1255          * Resolves instances being deserialized to the predefined constants.
1256          *
1257          * @throws InvalidObjectException if the constant could not be resolved.
1258          * @return resolved NumberFormat.Field constant
1259          */
1260         @Override
1261         protected Object readResolve() throws InvalidObjectException {
1262             if (this.getClass() != NumberFormat.Field.class) {
1263                 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
1264             }
1265 
1266             Object instance = instanceMap.get(getName());
1267             if (instance != null) {
1268                 return instance;
1269             } else {
1270                 throw new InvalidObjectException("unknown attribute name");
1271             }
1272         }
1273 
1274         /**
1275          * Constant identifying the integer field.
1276          */
1277         public static final Field INTEGER = new Field("integer");
1278 
1279         /**
1280          * Constant identifying the fraction field.
1281          */
1282         public static final Field FRACTION = new Field("fraction");
1283 
1284         /**
1285          * Constant identifying the exponent field.
1286          */
1287         public static final Field EXPONENT = new Field("exponent");
1288 
1289         /**
1290          * Constant identifying the decimal separator field.
1291          */
1292         public static final Field DECIMAL_SEPARATOR =
1293                             new Field("decimal separator");
1294 
1295         /**
1296          * Constant identifying the sign field.
1297          */
1298         public static final Field SIGN = new Field("sign");
1299 
1300         /**
1301          * Constant identifying the grouping separator field.
1302          */
1303         public static final Field GROUPING_SEPARATOR =
1304                             new Field("grouping separator");
1305 
1306         /**
1307          * Constant identifying the exponent symbol field.
1308          */
1309         public static final Field EXPONENT_SYMBOL = new
1310                             Field("exponent symbol");
1311 
1312         /**
1313          * Constant identifying the percent field.
1314          */
1315         public static final Field PERCENT = new Field("percent");
1316 
1317         /**
1318          * Constant identifying the permille field.
1319          */
1320         public static final Field PERMILLE = new Field("per mille");
1321 
1322         /**
1323          * Constant identifying the currency field.
1324          */
1325         public static final Field CURRENCY = new Field("currency");
1326 
1327         /**
1328          * Constant identifying the exponent sign field.
1329          */
1330         public static final Field EXPONENT_SIGN = new Field("exponent sign");
1331 
1332         /**
1333          * Constant identifying the prefix field.
1334          *
1335          * @since 12
1336          */
1337         public static final Field PREFIX = new Field("prefix");
1338 
1339         /**
1340          * Constant identifying the suffix field.
1341          *
1342          * @since 12
1343          */
1344         public static final Field SUFFIX = new Field("suffix");
1345     }
1346 
1347     /**
1348      * A number format style.
1349      * <p>
1350      * {@code Style} is an enum which represents the style for formatting
1351      * a number within a given {@code NumberFormat} instance.
1352      *
1353      * @see CompactNumberFormat
1354      * @see NumberFormat#getCompactNumberInstance(Locale, Style)
1355      * @since 12
1356      */
1357     public enum Style {
1358 
1359         /**
1360          * The {@code SHORT} number format style.
1361          */
1362         SHORT,
1363 
1364         /**
1365          * The {@code LONG} number format style.
1366          */
1367         LONG
1368 
1369     }
1370 }