1 /*
   2  * Copyright (c) 1996, 2017, 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.IOException;
  42 import java.io.InvalidObjectException;
  43 import java.io.ObjectInputStream;
  44 import java.math.BigDecimal;
  45 import java.math.BigInteger;
  46 import java.math.RoundingMode;
  47 import java.text.spi.NumberFormatProvider;
  48 import java.util.ArrayList;
  49 import java.util.Currency;
  50 import java.util.Locale;
  51 import java.util.ResourceBundle;
  52 import java.util.concurrent.ConcurrentHashMap;
  53 import java.util.concurrent.ConcurrentMap;
  54 import java.util.concurrent.atomic.AtomicInteger;
  55 import java.util.concurrent.atomic.AtomicLong;
  56 import sun.util.locale.provider.LocaleProviderAdapter;
  57 import sun.util.locale.provider.ResourceBundleBasedAdapter;
  58 
  59 /**
  60  * <code>DecimalFormat</code> is a concrete subclass of
  61  * <code>NumberFormat</code> that formats decimal numbers. It has a variety of
  62  * features designed to make it possible to parse and format numbers in any
  63  * locale, including support for Western, Arabic, and Indic digits.  It also
  64  * supports different kinds of numbers, including integers (123), fixed-point
  65  * numbers (123.4), scientific notation (1.23E4), percentages (12%), and
  66  * currency amounts ($123).  All of these can be localized.
  67  *
  68  * <p>To obtain a <code>NumberFormat</code> for a specific locale, including the
  69  * default locale, call one of <code>NumberFormat</code>'s factory methods, such
  70  * as <code>getInstance()</code>.  In general, do not call the
  71  * <code>DecimalFormat</code> constructors directly, since the
  72  * <code>NumberFormat</code> factory methods may return subclasses other than
  73  * <code>DecimalFormat</code>. If you need to customize the format object, do
  74  * something like this:
  75  *
  76  * <blockquote><pre>
  77  * NumberFormat f = NumberFormat.getInstance(loc);
  78  * if (f instanceof DecimalFormat) {
  79  *     ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
  80  * }
  81  * </pre></blockquote>
  82  *
  83  * <p>A <code>DecimalFormat</code> comprises a <em>pattern</em> and a set of
  84  * <em>symbols</em>.  The pattern may be set directly using
  85  * <code>applyPattern()</code>, or indirectly using the API methods.  The
  86  * symbols are stored in a <code>DecimalFormatSymbols</code> object.  When using
  87  * the <code>NumberFormat</code> factory methods, the pattern and symbols are
  88  * read from localized <code>ResourceBundle</code>s.
  89  *
  90  * <h3>Patterns</h3>
  91  *
  92  * <code>DecimalFormat</code> patterns have the following syntax:
  93  * <blockquote><pre>
  94  * <i>Pattern:</i>
  95  *         <i>PositivePattern</i>
  96  *         <i>PositivePattern</i> ; <i>NegativePattern</i>
  97  * <i>PositivePattern:</i>
  98  *         <i>Prefix<sub>opt</sub></i> <i>Number</i> <i>Suffix<sub>opt</sub></i>
  99  * <i>NegativePattern:</i>
 100  *         <i>Prefix<sub>opt</sub></i> <i>Number</i> <i>Suffix<sub>opt</sub></i>
 101  * <i>Prefix:</i>
 102  *         any Unicode characters except \uFFFE, \uFFFF, and special characters
 103  * <i>Suffix:</i>
 104  *         any Unicode characters except \uFFFE, \uFFFF, and special characters
 105  * <i>Number:</i>
 106  *         <i>Integer</i> <i>Exponent<sub>opt</sub></i>
 107  *         <i>Integer</i> . <i>Fraction</i> <i>Exponent<sub>opt</sub></i>
 108  * <i>Integer:</i>
 109  *         <i>MinimumInteger</i>
 110  *         #
 111  *         # <i>Integer</i>
 112  *         # , <i>Integer</i>
 113  * <i>MinimumInteger:</i>
 114  *         0
 115  *         0 <i>MinimumInteger</i>
 116  *         0 , <i>MinimumInteger</i>
 117  * <i>Fraction:</i>
 118  *         <i>MinimumFraction<sub>opt</sub></i> <i>OptionalFraction<sub>opt</sub></i>
 119  * <i>MinimumFraction:</i>
 120  *         0 <i>MinimumFraction<sub>opt</sub></i>
 121  * <i>OptionalFraction:</i>
 122  *         # <i>OptionalFraction<sub>opt</sub></i>
 123  * <i>Exponent:</i>
 124  *         E <i>MinimumExponent</i>
 125  * <i>MinimumExponent:</i>
 126  *         0 <i>MinimumExponent<sub>opt</sub></i>
 127  * </pre></blockquote>
 128  *
 129  * <p>A <code>DecimalFormat</code> pattern contains a positive and negative
 130  * subpattern, for example, <code>"#,##0.00;(#,##0.00)"</code>.  Each
 131  * subpattern has a prefix, numeric part, and suffix. The negative subpattern
 132  * is optional; if absent, then the positive subpattern prefixed with the
 133  * localized minus sign (<code>'-'</code> in most locales) is used as the
 134  * negative subpattern. That is, <code>"0.00"</code> alone is equivalent to
 135  * <code>"0.00;-0.00"</code>.  If there is an explicit negative subpattern, it
 136  * serves only to specify the negative prefix and suffix; the number of digits,
 137  * minimal digits, and other characteristics are all the same as the positive
 138  * pattern. That means that <code>"#,##0.0#;(#)"</code> produces precisely
 139  * the same behavior as <code>"#,##0.0#;(#,##0.0#)"</code>.
 140  *
 141  * <p>The prefixes, suffixes, and various symbols used for infinity, digits,
 142  * thousands separators, decimal separators, etc. may be set to arbitrary
 143  * values, and they will appear properly during formatting.  However, care must
 144  * be taken that the symbols and strings do not conflict, or parsing will be
 145  * unreliable.  For example, either the positive and negative prefixes or the
 146  * suffixes must be distinct for <code>DecimalFormat.parse()</code> to be able
 147  * to distinguish positive from negative values.  (If they are identical, then
 148  * <code>DecimalFormat</code> will behave as if no negative subpattern was
 149  * specified.)  Another example is that the decimal separator and thousands
 150  * separator should be distinct characters, or parsing will be impossible.
 151  *
 152  * <p>The grouping separator is commonly used for thousands, but in some
 153  * countries it separates ten-thousands. The grouping size is a constant number
 154  * of digits between the grouping characters, such as 3 for 100,000,000 or 4 for
 155  * 1,0000,0000.  If you supply a pattern with multiple grouping characters, the
 156  * interval between the last one and the end of the integer is the one that is
 157  * used. So <code>"#,##,###,####"</code> == <code>"######,####"</code> ==
 158  * <code>"##,####,####"</code>.
 159  *
 160  * <h4>Special Pattern Characters</h4>
 161  *
 162  * <p>Many characters in a pattern are taken literally; they are matched during
 163  * parsing and output unchanged during formatting.  Special characters, on the
 164  * other hand, stand for other characters, strings, or classes of characters.
 165  * They must be quoted, unless noted otherwise, if they are to appear in the
 166  * prefix or suffix as literals.
 167  *
 168  * <p>The characters listed here are used in non-localized patterns.  Localized
 169  * patterns use the corresponding characters taken from this formatter's
 170  * <code>DecimalFormatSymbols</code> object instead, and these characters lose
 171  * their special status.  Two exceptions are the currency sign and quote, which
 172  * are not localized.
 173  *
 174  * <blockquote>
 175  * <table border=0 cellspacing=3 cellpadding=0 summary="Chart showing symbol,
 176  *  location, localized, and meaning.">
 177  *     <tr style="background-color: rgb(204, 204, 255);">
 178  *          <th style="text-align:left">Symbol
 179  *          <th style="text-align:left">Location
 180  *          <th style="text-align:left">Localized?
 181  *          <th style="text-align:left">Meaning
 182  *     <tr style="vertical-align:top">
 183  *          <td><code>0</code>
 184  *          <td>Number
 185  *          <td>Yes
 186  *          <td>Digit
 187  *     <tr style="vertical-align: top; background-color: rgb(238, 238, 255);">
 188  *          <td><code>#</code>
 189  *          <td>Number
 190  *          <td>Yes
 191  *          <td>Digit, zero shows as absent
 192  *     <tr style="vertical-align:top">
 193  *          <td><code>.</code>
 194  *          <td>Number
 195  *          <td>Yes
 196  *          <td>Decimal separator or monetary decimal separator
 197  *     <tr style="vertical-align: top; background-color: rgb(238, 238, 255);">
 198  *          <td><code>-</code>
 199  *          <td>Number
 200  *          <td>Yes
 201  *          <td>Minus sign
 202  *     <tr style="vertical-align:top">
 203  *          <td><code>,</code>
 204  *          <td>Number
 205  *          <td>Yes
 206  *          <td>Grouping separator
 207  *     <tr style="vertical-align: top; background-color: rgb(238, 238, 255);">
 208  *          <td><code>E</code>
 209  *          <td>Number
 210  *          <td>Yes
 211  *          <td>Separates mantissa and exponent in scientific notation.
 212  *              <em>Need not be quoted in prefix or suffix.</em>
 213  *     <tr style="vertical-align:top">
 214  *          <td><code>;</code>
 215  *          <td>Subpattern boundary
 216  *          <td>Yes
 217  *          <td>Separates positive and negative subpatterns
 218  *     <tr style="vertical-align: top; background-color: rgb(238, 238, 255);">
 219  *          <td><code>%</code>
 220  *          <td>Prefix or suffix
 221  *          <td>Yes
 222  *          <td>Multiply by 100 and show as percentage
 223  *     <tr style="vertical-align:top">
 224  *          <td><code>\u2030</code>
 225  *          <td>Prefix or suffix
 226  *          <td>Yes
 227  *          <td>Multiply by 1000 and show as per mille value
 228  *     <tr style="vertical-align: top; background-color: rgb(238, 238, 255);">
 229  *          <td><code>¤</code> (<code>\u00A4</code>)
 230  *          <td>Prefix or suffix
 231  *          <td>No
 232  *          <td>Currency sign, replaced by currency symbol.  If
 233  *              doubled, replaced by international currency symbol.
 234  *              If present in a pattern, the monetary decimal separator
 235  *              is used instead of the decimal separator.
 236  *     <tr style="vertical-align:top">
 237  *          <td><code>'</code>
 238  *          <td>Prefix or suffix
 239  *          <td>No
 240  *          <td>Used to quote special characters in a prefix or suffix,
 241  *              for example, <code>"'#'#"</code> formats 123 to
 242  *              <code>"#123"</code>.  To create a single quote
 243  *              itself, use two in a row: <code>"# o''clock"</code>.
 244  * </table>
 245  * </blockquote>
 246  *
 247  * <h4>Scientific Notation</h4>
 248  *
 249  * <p>Numbers in scientific notation are expressed as the product of a mantissa
 250  * and a power of ten, for example, 1234 can be expressed as 1.234 x 10^3.  The
 251  * mantissa is often in the range 1.0 &le; x {@literal <} 10.0, but it need not
 252  * be.
 253  * <code>DecimalFormat</code> can be instructed to format and parse scientific
 254  * notation <em>only via a pattern</em>; there is currently no factory method
 255  * that creates a scientific notation format.  In a pattern, the exponent
 256  * character immediately followed by one or more digit characters indicates
 257  * scientific notation.  Example: <code>"0.###E0"</code> formats the number
 258  * 1234 as <code>"1.234E3"</code>.
 259  *
 260  * <ul>
 261  * <li>The number of digit characters after the exponent character gives the
 262  * minimum exponent digit count.  There is no maximum.  Negative exponents are
 263  * formatted using the localized minus sign, <em>not</em> the prefix and suffix
 264  * from the pattern.  This allows patterns such as <code>"0.###E0 m/s"</code>.
 265  *
 266  * <li>The minimum and maximum number of integer digits are interpreted
 267  * together:
 268  *
 269  * <ul>
 270  * <li>If the maximum number of integer digits is greater than their minimum number
 271  * and greater than 1, it forces the exponent to be a multiple of the maximum
 272  * number of integer digits, and the minimum number of integer digits to be
 273  * interpreted as 1.  The most common use of this is to generate
 274  * <em>engineering notation</em>, in which the exponent is a multiple of three,
 275  * e.g., <code>"##0.#####E0"</code>. Using this pattern, the number 12345
 276  * formats to <code>"12.345E3"</code>, and 123456 formats to
 277  * <code>"123.456E3"</code>.
 278  *
 279  * <li>Otherwise, the minimum number of integer digits is achieved by adjusting the
 280  * exponent.  Example: 0.00123 formatted with <code>"00.###E0"</code> yields
 281  * <code>"12.3E-4"</code>.
 282  * </ul>
 283  *
 284  * <li>The number of significant digits in the mantissa is the sum of the
 285  * <em>minimum integer</em> and <em>maximum fraction</em> digits, and is
 286  * unaffected by the maximum integer digits.  For example, 12345 formatted with
 287  * <code>"##0.##E0"</code> is <code>"12.3E3"</code>. To show all digits, set
 288  * the significant digits count to zero.  The number of significant digits
 289  * does not affect parsing.
 290  *
 291  * <li>Exponential patterns may not contain grouping separators.
 292  * </ul>
 293  *
 294  * <h4>Rounding</h4>
 295  *
 296  * <code>DecimalFormat</code> provides rounding modes defined in
 297  * {@link java.math.RoundingMode} for formatting.  By default, it uses
 298  * {@link java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}.
 299  *
 300  * <h4>Digits</h4>
 301  *
 302  * For formatting, <code>DecimalFormat</code> uses the ten consecutive
 303  * characters starting with the localized zero digit defined in the
 304  * <code>DecimalFormatSymbols</code> object as digits. For parsing, these
 305  * digits as well as all Unicode decimal digits, as defined by
 306  * {@link Character#digit Character.digit}, are recognized.
 307  *
 308  * <h4>Special Values</h4>
 309  *
 310  * <p><code>NaN</code> is formatted as a string, which typically has a single character
 311  * <code>\uFFFD</code>.  This string is determined by the
 312  * <code>DecimalFormatSymbols</code> object.  This is the only value for which
 313  * the prefixes and suffixes are not used.
 314  *
 315  * <p>Infinity is formatted as a string, which typically has a single character
 316  * <code>\u221E</code>, with the positive or negative prefixes and suffixes
 317  * applied.  The infinity string is determined by the
 318  * <code>DecimalFormatSymbols</code> object.
 319  *
 320  * <p>Negative zero (<code>"-0"</code>) parses to
 321  * <ul>
 322  * <li><code>BigDecimal(0)</code> if <code>isParseBigDecimal()</code> is
 323  * true,
 324  * <li><code>Long(0)</code> if <code>isParseBigDecimal()</code> is false
 325  *     and <code>isParseIntegerOnly()</code> is true,
 326  * <li><code>Double(-0.0)</code> if both <code>isParseBigDecimal()</code>
 327  * and <code>isParseIntegerOnly()</code> are false.
 328  * </ul>
 329  *
 330  * <h4><a id="synchronization">Synchronization</a></h4>
 331  *
 332  * <p>
 333  * Decimal formats are generally not synchronized.
 334  * It is recommended to create separate format instances for each thread.
 335  * If multiple threads access a format concurrently, it must be synchronized
 336  * externally.
 337  *
 338  * <h4>Example</h4>
 339  *
 340  * <blockquote><pre>{@code
 341  * <strong>// Print out a number using the localized number, integer, currency,
 342  * // and percent format for each locale</strong>
 343  * Locale[] locales = NumberFormat.getAvailableLocales();
 344  * double myNumber = -1234.56;
 345  * NumberFormat form;
 346  * for (int j = 0; j < 4; ++j) {
 347  *     System.out.println("FORMAT");
 348  *     for (int i = 0; i < locales.length; ++i) {
 349  *         if (locales[i].getCountry().length() == 0) {
 350  *            continue; // Skip language-only locales
 351  *         }
 352  *         System.out.print(locales[i].getDisplayName());
 353  *         switch (j) {
 354  *         case 0:
 355  *             form = NumberFormat.getInstance(locales[i]); break;
 356  *         case 1:
 357  *             form = NumberFormat.getIntegerInstance(locales[i]); break;
 358  *         case 2:
 359  *             form = NumberFormat.getCurrencyInstance(locales[i]); break;
 360  *         default:
 361  *             form = NumberFormat.getPercentInstance(locales[i]); break;
 362  *         }
 363  *         if (form instanceof DecimalFormat) {
 364  *             System.out.print(": " + ((DecimalFormat) form).toPattern());
 365  *         }
 366  *         System.out.print(" -> " + form.format(myNumber));
 367  *         try {
 368  *             System.out.println(" -> " + form.parse(form.format(myNumber)));
 369  *         } catch (ParseException e) {}
 370  *     }
 371  * }
 372  * }</pre></blockquote>
 373  *
 374  * @see          <a href="http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html">Java Tutorial</a>
 375  * @see          NumberFormat
 376  * @see          DecimalFormatSymbols
 377  * @see          ParsePosition
 378  * @author       Mark Davis
 379  * @author       Alan Liu
 380  */
 381 public class DecimalFormat extends NumberFormat {
 382 
 383     /**
 384      * Creates a DecimalFormat using the default pattern and symbols
 385      * for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
 386      * This is a convenient way to obtain a
 387      * DecimalFormat when internationalization is not the main concern.
 388      * <p>
 389      * To obtain standard formats for a given locale, use the factory methods
 390      * on NumberFormat such as getNumberInstance. These factories will
 391      * return the most appropriate sub-class of NumberFormat for a given
 392      * locale.
 393      *
 394      * @see java.text.NumberFormat#getInstance
 395      * @see java.text.NumberFormat#getNumberInstance
 396      * @see java.text.NumberFormat#getCurrencyInstance
 397      * @see java.text.NumberFormat#getPercentInstance
 398      */
 399     public DecimalFormat() {
 400         // Get the pattern for the default locale.
 401         Locale def = Locale.getDefault(Locale.Category.FORMAT);
 402         LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class, def);
 403         if (!(adapter instanceof ResourceBundleBasedAdapter)) {
 404             adapter = LocaleProviderAdapter.getResourceBundleBased();
 405         }
 406         String[] all = adapter.getLocaleResources(def).getNumberPatterns();
 407 
 408         // Always applyPattern after the symbols are set
 409         this.symbols = DecimalFormatSymbols.getInstance(def);
 410         applyPattern(all[0], false);
 411     }
 412 
 413 
 414     /**
 415      * Creates a DecimalFormat using the given pattern and the symbols
 416      * for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
 417      * This is a convenient way to obtain a
 418      * DecimalFormat when internationalization is not the main concern.
 419      * <p>
 420      * To obtain standard formats for a given locale, use the factory methods
 421      * on NumberFormat such as getNumberInstance. These factories will
 422      * return the most appropriate sub-class of NumberFormat for a given
 423      * locale.
 424      *
 425      * @param pattern a non-localized pattern string.
 426      * @exception NullPointerException if <code>pattern</code> is null
 427      * @exception IllegalArgumentException if the given pattern is invalid.
 428      * @see java.text.NumberFormat#getInstance
 429      * @see java.text.NumberFormat#getNumberInstance
 430      * @see java.text.NumberFormat#getCurrencyInstance
 431      * @see java.text.NumberFormat#getPercentInstance
 432      */
 433     public DecimalFormat(String pattern) {
 434         // Always applyPattern after the symbols are set
 435         this.symbols = DecimalFormatSymbols.getInstance(Locale.getDefault(Locale.Category.FORMAT));
 436         applyPattern(pattern, false);
 437     }
 438 
 439 
 440     /**
 441      * Creates a DecimalFormat using the given pattern and symbols.
 442      * Use this constructor when you need to completely customize the
 443      * behavior of the format.
 444      * <p>
 445      * To obtain standard formats for a given
 446      * locale, use the factory methods on NumberFormat such as
 447      * getInstance or getCurrencyInstance. If you need only minor adjustments
 448      * to a standard format, you can modify the format returned by
 449      * a NumberFormat factory method.
 450      *
 451      * @param pattern a non-localized pattern string
 452      * @param symbols the set of symbols to be used
 453      * @exception NullPointerException if any of the given arguments is null
 454      * @exception IllegalArgumentException if the given pattern is invalid
 455      * @see java.text.NumberFormat#getInstance
 456      * @see java.text.NumberFormat#getNumberInstance
 457      * @see java.text.NumberFormat#getCurrencyInstance
 458      * @see java.text.NumberFormat#getPercentInstance
 459      * @see java.text.DecimalFormatSymbols
 460      */
 461     public DecimalFormat (String pattern, DecimalFormatSymbols symbols) {
 462         // Always applyPattern after the symbols are set
 463         this.symbols = (DecimalFormatSymbols)symbols.clone();
 464         applyPattern(pattern, false);
 465     }
 466 
 467 
 468     // Overrides
 469     /**
 470      * Formats a number and appends the resulting text to the given string
 471      * buffer.
 472      * The number can be of any subclass of {@link java.lang.Number}.
 473      * <p>
 474      * This implementation uses the maximum precision permitted.
 475      * @param number     the number to format
 476      * @param toAppendTo the <code>StringBuffer</code> to which the formatted
 477      *                   text is to be appended
 478      * @param pos        On input: an alignment field, if desired.
 479      *                   On output: the offsets of the alignment field.
 480      * @return           the value passed in as <code>toAppendTo</code>
 481      * @exception        IllegalArgumentException if <code>number</code> is
 482      *                   null or not an instance of <code>Number</code>.
 483      * @exception        NullPointerException if <code>toAppendTo</code> or
 484      *                   <code>pos</code> is null
 485      * @exception        ArithmeticException if rounding is needed with rounding
 486      *                   mode being set to RoundingMode.UNNECESSARY
 487      * @see              java.text.FieldPosition
 488      */
 489     @Override
 490     public final StringBuffer format(Object number,
 491                                      StringBuffer toAppendTo,
 492                                      FieldPosition pos) {
 493         if (number instanceof Long || number instanceof Integer ||
 494                    number instanceof Short || number instanceof Byte ||
 495                    number instanceof AtomicInteger ||
 496                    number instanceof AtomicLong ||
 497                    (number instanceof BigInteger &&
 498                     ((BigInteger)number).bitLength () < 64)) {
 499             return format(((Number)number).longValue(), toAppendTo, pos);
 500         } else if (number instanceof BigDecimal) {
 501             return format((BigDecimal)number, toAppendTo, pos);
 502         } else if (number instanceof BigInteger) {
 503             return format((BigInteger)number, toAppendTo, pos);
 504         } else if (number instanceof Number) {
 505             return format(((Number)number).doubleValue(), toAppendTo, pos);
 506         } else {
 507             throw new IllegalArgumentException("Cannot format given Object as a Number");
 508         }
 509     }
 510 
 511     /**
 512      * Formats a double to produce a string.
 513      * @param number    The double to format
 514      * @param result    where the text is to be appended
 515      * @param fieldPosition    On input: an alignment field, if desired.
 516      * On output: the offsets of the alignment field.
 517      * @exception NullPointerException if {@code result} or
 518      *            {@code fieldPosition} is {@code null}
 519      * @exception ArithmeticException if rounding is needed with rounding
 520      *            mode being set to RoundingMode.UNNECESSARY
 521      * @return The formatted number string
 522      * @see java.text.FieldPosition
 523      */
 524     @Override
 525     public StringBuffer format(double number, StringBuffer result,
 526                                FieldPosition fieldPosition) {
 527         // If fieldPosition is a DontCareFieldPosition instance we can
 528         // try to go to fast-path code.
 529         boolean tryFastPath = false;
 530         if (fieldPosition == DontCareFieldPosition.INSTANCE)
 531             tryFastPath = true;
 532         else {
 533             fieldPosition.setBeginIndex(0);
 534             fieldPosition.setEndIndex(0);
 535         }
 536 
 537         if (tryFastPath) {
 538             String tempResult = fastFormat(number);
 539             if (tempResult != null) {
 540                 result.append(tempResult);
 541                 return result;
 542             }
 543         }
 544 
 545         // if fast-path could not work, we fallback to standard code.
 546         return format(number, result, fieldPosition.getFieldDelegate());
 547     }
 548 
 549     /**
 550      * Formats a double to produce a string.
 551      * @param number    The double to format
 552      * @param result    where the text is to be appended
 553      * @param delegate notified of locations of sub fields
 554      * @exception       ArithmeticException if rounding is needed with rounding
 555      *                  mode being set to RoundingMode.UNNECESSARY
 556      * @return The formatted number string
 557      */
 558     private StringBuffer format(double number, StringBuffer result,
 559                                 FieldDelegate delegate) {
 560         if (Double.isNaN(number) ||
 561            (Double.isInfinite(number) && multiplier == 0)) {
 562             int iFieldStart = result.length();
 563             result.append(symbols.getNaN());
 564             delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
 565                                iFieldStart, result.length(), result);
 566             return result;
 567         }
 568 
 569         /* Detecting whether a double is negative is easy with the exception of
 570          * the value -0.0.  This is a double which has a zero mantissa (and
 571          * exponent), but a negative sign bit.  It is semantically distinct from
 572          * a zero with a positive sign bit, and this distinction is important
 573          * to certain kinds of computations.  However, it's a little tricky to
 574          * detect, since (-0.0 == 0.0) and !(-0.0 < 0.0).  How then, you may
 575          * ask, does it behave distinctly from +0.0?  Well, 1/(-0.0) ==
 576          * -Infinity.  Proper detection of -0.0 is needed to deal with the
 577          * issues raised by bugs 4106658, 4106667, and 4147706.  Liu 7/6/98.
 578          */
 579         boolean isNegative = ((number < 0.0) || (number == 0.0 && 1/number < 0.0)) ^ (multiplier < 0);
 580 
 581         if (multiplier != 1) {
 582             number *= multiplier;
 583         }
 584 
 585         if (Double.isInfinite(number)) {
 586             if (isNegative) {
 587                 append(result, negativePrefix, delegate,
 588                        getNegativePrefixFieldPositions(), Field.SIGN);
 589             } else {
 590                 append(result, positivePrefix, delegate,
 591                        getPositivePrefixFieldPositions(), Field.SIGN);
 592             }
 593 
 594             int iFieldStart = result.length();
 595             result.append(symbols.getInfinity());
 596             delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
 597                                iFieldStart, result.length(), result);
 598 
 599             if (isNegative) {
 600                 append(result, negativeSuffix, delegate,
 601                        getNegativeSuffixFieldPositions(), Field.SIGN);
 602             } else {
 603                 append(result, positiveSuffix, delegate,
 604                        getPositiveSuffixFieldPositions(), Field.SIGN);
 605             }
 606 
 607             return result;
 608         }
 609 
 610         if (isNegative) {
 611             number = -number;
 612         }
 613 
 614         // at this point we are guaranteed a nonnegative finite number.
 615         assert(number >= 0 && !Double.isInfinite(number));
 616 
 617         synchronized(digitList) {
 618             int maxIntDigits = super.getMaximumIntegerDigits();
 619             int minIntDigits = super.getMinimumIntegerDigits();
 620             int maxFraDigits = super.getMaximumFractionDigits();
 621             int minFraDigits = super.getMinimumFractionDigits();
 622 
 623             digitList.set(isNegative, number, useExponentialNotation ?
 624                           maxIntDigits + maxFraDigits : maxFraDigits,
 625                           !useExponentialNotation);
 626             return subformat(result, delegate, isNegative, false,
 627                        maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
 628         }
 629     }
 630 
 631     /**
 632      * Format a long to produce a string.
 633      * @param number    The long to format
 634      * @param result    where the text is to be appended
 635      * @param fieldPosition    On input: an alignment field, if desired.
 636      * On output: the offsets of the alignment field.
 637      * @exception       NullPointerException if {@code result} or
 638      *                  {@code fieldPosition} is {@code null}
 639      * @exception       ArithmeticException if rounding is needed with rounding
 640      *                  mode being set to RoundingMode.UNNECESSARY
 641      * @return The formatted number string
 642      * @see java.text.FieldPosition
 643      */
 644     @Override
 645     public StringBuffer format(long number, StringBuffer result,
 646                                FieldPosition fieldPosition) {
 647         fieldPosition.setBeginIndex(0);
 648         fieldPosition.setEndIndex(0);
 649 
 650         return format(number, result, fieldPosition.getFieldDelegate());
 651     }
 652 
 653     /**
 654      * Format a long to produce a string.
 655      * @param number    The long to format
 656      * @param result    where the text is to be appended
 657      * @param delegate notified of locations of sub fields
 658      * @return The formatted number string
 659      * @exception        ArithmeticException if rounding is needed with rounding
 660      *                   mode being set to RoundingMode.UNNECESSARY
 661      * @see java.text.FieldPosition
 662      */
 663     private StringBuffer format(long number, StringBuffer result,
 664                                FieldDelegate delegate) {
 665         boolean isNegative = (number < 0);
 666         if (isNegative) {
 667             number = -number;
 668         }
 669 
 670         // In general, long values always represent real finite numbers, so
 671         // we don't have to check for +/- Infinity or NaN.  However, there
 672         // is one case we have to be careful of:  The multiplier can push
 673         // a number near MIN_VALUE or MAX_VALUE outside the legal range.  We
 674         // check for this before multiplying, and if it happens we use
 675         // BigInteger instead.
 676         boolean useBigInteger = false;
 677         if (number < 0) { // This can only happen if number == Long.MIN_VALUE.
 678             if (multiplier != 0) {
 679                 useBigInteger = true;
 680             }
 681         } else if (multiplier != 1 && multiplier != 0) {
 682             long cutoff = Long.MAX_VALUE / multiplier;
 683             if (cutoff < 0) {
 684                 cutoff = -cutoff;
 685             }
 686             useBigInteger = (number > cutoff);
 687         }
 688 
 689         if (useBigInteger) {
 690             if (isNegative) {
 691                 number = -number;
 692             }
 693             BigInteger bigIntegerValue = BigInteger.valueOf(number);
 694             return format(bigIntegerValue, result, delegate, true);
 695         }
 696 
 697         number *= multiplier;
 698         if (number == 0) {
 699             isNegative = false;
 700         } else {
 701             if (multiplier < 0) {
 702                 number = -number;
 703                 isNegative = !isNegative;
 704             }
 705         }
 706 
 707         synchronized(digitList) {
 708             int maxIntDigits = super.getMaximumIntegerDigits();
 709             int minIntDigits = super.getMinimumIntegerDigits();
 710             int maxFraDigits = super.getMaximumFractionDigits();
 711             int minFraDigits = super.getMinimumFractionDigits();
 712 
 713             digitList.set(isNegative, number,
 714                      useExponentialNotation ? maxIntDigits + maxFraDigits : 0);
 715 
 716             return subformat(result, delegate, isNegative, true,
 717                        maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
 718         }
 719     }
 720 
 721     /**
 722      * Formats a BigDecimal to produce a string.
 723      * @param number    The BigDecimal to format
 724      * @param result    where the text is to be appended
 725      * @param fieldPosition    On input: an alignment field, if desired.
 726      * On output: the offsets of the alignment field.
 727      * @return The formatted number string
 728      * @exception        ArithmeticException if rounding is needed with rounding
 729      *                   mode being set to RoundingMode.UNNECESSARY
 730      * @see java.text.FieldPosition
 731      */
 732     private StringBuffer format(BigDecimal number, StringBuffer result,
 733                                 FieldPosition fieldPosition) {
 734         fieldPosition.setBeginIndex(0);
 735         fieldPosition.setEndIndex(0);
 736         return format(number, result, fieldPosition.getFieldDelegate());
 737     }
 738 
 739     /**
 740      * Formats a BigDecimal to produce a string.
 741      * @param number    The BigDecimal to format
 742      * @param result    where the text is to be appended
 743      * @param delegate notified of locations of sub fields
 744      * @exception        ArithmeticException if rounding is needed with rounding
 745      *                   mode being set to RoundingMode.UNNECESSARY
 746      * @return The formatted number string
 747      */
 748     private StringBuffer format(BigDecimal number, StringBuffer result,
 749                                 FieldDelegate delegate) {
 750         if (multiplier != 1) {
 751             number = number.multiply(getBigDecimalMultiplier());
 752         }
 753         boolean isNegative = number.signum() == -1;
 754         if (isNegative) {
 755             number = number.negate();
 756         }
 757 
 758         synchronized(digitList) {
 759             int maxIntDigits = getMaximumIntegerDigits();
 760             int minIntDigits = getMinimumIntegerDigits();
 761             int maxFraDigits = getMaximumFractionDigits();
 762             int minFraDigits = getMinimumFractionDigits();
 763             int maximumDigits = maxIntDigits + maxFraDigits;
 764 
 765             digitList.set(isNegative, number, useExponentialNotation ?
 766                 ((maximumDigits < 0) ? Integer.MAX_VALUE : maximumDigits) :
 767                 maxFraDigits, !useExponentialNotation);
 768 
 769             return subformat(result, delegate, isNegative, false,
 770                 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
 771         }
 772     }
 773 
 774     /**
 775      * Format a BigInteger to produce a string.
 776      * @param number    The BigInteger to format
 777      * @param result    where the text is to be appended
 778      * @param fieldPosition    On input: an alignment field, if desired.
 779      * On output: the offsets of the alignment field.
 780      * @return The formatted number string
 781      * @exception        ArithmeticException if rounding is needed with rounding
 782      *                   mode being set to RoundingMode.UNNECESSARY
 783      * @see java.text.FieldPosition
 784      */
 785     private StringBuffer format(BigInteger number, StringBuffer result,
 786                                FieldPosition fieldPosition) {
 787         fieldPosition.setBeginIndex(0);
 788         fieldPosition.setEndIndex(0);
 789 
 790         return format(number, result, fieldPosition.getFieldDelegate(), false);
 791     }
 792 
 793     /**
 794      * Format a BigInteger to produce a string.
 795      * @param number    The BigInteger to format
 796      * @param result    where the text is to be appended
 797      * @param delegate notified of locations of sub fields
 798      * @return The formatted number string
 799      * @exception        ArithmeticException if rounding is needed with rounding
 800      *                   mode being set to RoundingMode.UNNECESSARY
 801      * @see java.text.FieldPosition
 802      */
 803     private StringBuffer format(BigInteger number, StringBuffer result,
 804                                FieldDelegate delegate, boolean formatLong) {
 805         if (multiplier != 1) {
 806             number = number.multiply(getBigIntegerMultiplier());
 807         }
 808         boolean isNegative = number.signum() == -1;
 809         if (isNegative) {
 810             number = number.negate();
 811         }
 812 
 813         synchronized(digitList) {
 814             int maxIntDigits, minIntDigits, maxFraDigits, minFraDigits, maximumDigits;
 815             if (formatLong) {
 816                 maxIntDigits = super.getMaximumIntegerDigits();
 817                 minIntDigits = super.getMinimumIntegerDigits();
 818                 maxFraDigits = super.getMaximumFractionDigits();
 819                 minFraDigits = super.getMinimumFractionDigits();
 820                 maximumDigits = maxIntDigits + maxFraDigits;
 821             } else {
 822                 maxIntDigits = getMaximumIntegerDigits();
 823                 minIntDigits = getMinimumIntegerDigits();
 824                 maxFraDigits = getMaximumFractionDigits();
 825                 minFraDigits = getMinimumFractionDigits();
 826                 maximumDigits = maxIntDigits + maxFraDigits;
 827                 if (maximumDigits < 0) {
 828                     maximumDigits = Integer.MAX_VALUE;
 829                 }
 830             }
 831 
 832             digitList.set(isNegative, number,
 833                           useExponentialNotation ? maximumDigits : 0);
 834 
 835             return subformat(result, delegate, isNegative, true,
 836                 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
 837         }
 838     }
 839 
 840     /**
 841      * Formats an Object producing an <code>AttributedCharacterIterator</code>.
 842      * You can use the returned <code>AttributedCharacterIterator</code>
 843      * to build the resulting String, as well as to determine information
 844      * about the resulting String.
 845      * <p>
 846      * Each attribute key of the AttributedCharacterIterator will be of type
 847      * <code>NumberFormat.Field</code>, with the attribute value being the
 848      * same as the attribute key.
 849      *
 850      * @exception NullPointerException if obj is null.
 851      * @exception IllegalArgumentException when the Format cannot format the
 852      *            given object.
 853      * @exception        ArithmeticException if rounding is needed with rounding
 854      *                   mode being set to RoundingMode.UNNECESSARY
 855      * @param obj The object to format
 856      * @return AttributedCharacterIterator describing the formatted value.
 857      * @since 1.4
 858      */
 859     @Override
 860     public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
 861         CharacterIteratorFieldDelegate delegate =
 862                          new CharacterIteratorFieldDelegate();
 863         StringBuffer sb = new StringBuffer();
 864 
 865         if (obj instanceof Double || obj instanceof Float) {
 866             format(((Number)obj).doubleValue(), sb, delegate);
 867         } else if (obj instanceof Long || obj instanceof Integer ||
 868                    obj instanceof Short || obj instanceof Byte ||
 869                    obj instanceof AtomicInteger || obj instanceof AtomicLong) {
 870             format(((Number)obj).longValue(), sb, delegate);
 871         } else if (obj instanceof BigDecimal) {
 872             format((BigDecimal)obj, sb, delegate);
 873         } else if (obj instanceof BigInteger) {
 874             format((BigInteger)obj, sb, delegate, false);
 875         } else if (obj == null) {
 876             throw new NullPointerException(
 877                 "formatToCharacterIterator must be passed non-null object");
 878         } else {
 879             throw new IllegalArgumentException(
 880                 "Cannot format given Object as a Number");
 881         }
 882         return delegate.getIterator(sb.toString());
 883     }
 884 
 885     // ==== Begin fast-path formating logic for double =========================
 886 
 887     /* Fast-path formatting will be used for format(double ...) methods iff a
 888      * number of conditions are met (see checkAndSetFastPathStatus()):
 889      * - Only if instance properties meet the right predefined conditions.
 890      * - The abs value of the double to format is <= Integer.MAX_VALUE.
 891      *
 892      * The basic approach is to split the binary to decimal conversion of a
 893      * double value into two phases:
 894      * * The conversion of the integer portion of the double.
 895      * * The conversion of the fractional portion of the double
 896      *   (limited to two or three digits).
 897      *
 898      * The isolation and conversion of the integer portion of the double is
 899      * straightforward. The conversion of the fraction is more subtle and relies
 900      * on some rounding properties of double to the decimal precisions in
 901      * question.  Using the terminology of BigDecimal, this fast-path algorithm
 902      * is applied when a double value has a magnitude less than Integer.MAX_VALUE
 903      * and rounding is to nearest even and the destination format has two or
 904      * three digits of *scale* (digits after the decimal point).
 905      *
 906      * Under a rounding to nearest even policy, the returned result is a digit
 907      * string of a number in the (in this case decimal) destination format
 908      * closest to the exact numerical value of the (in this case binary) input
 909      * value.  If two destination format numbers are equally distant, the one
 910      * with the last digit even is returned.  To compute such a correctly rounded
 911      * value, some information about digits beyond the smallest returned digit
 912      * position needs to be consulted.
 913      *
 914      * In general, a guard digit, a round digit, and a sticky *bit* are needed
 915      * beyond the returned digit position.  If the discarded portion of the input
 916      * is sufficiently large, the returned digit string is incremented.  In round
 917      * to nearest even, this threshold to increment occurs near the half-way
 918      * point between digits.  The sticky bit records if there are any remaining
 919      * trailing digits of the exact input value in the new format; the sticky bit
 920      * is consulted only in close to half-way rounding cases.
 921      *
 922      * Given the computation of the digit and bit values, rounding is then
 923      * reduced to a table lookup problem.  For decimal, the even/odd cases look
 924      * like this:
 925      *
 926      * Last   Round   Sticky
 927      * 6      5       0      => 6   // exactly halfway, return even digit.
 928      * 6      5       1      => 7   // a little bit more than halfway, round up.
 929      * 7      5       0      => 8   // exactly halfway, round up to even.
 930      * 7      5       1      => 8   // a little bit more than halfway, round up.
 931      * With analogous entries for other even and odd last-returned digits.
 932      *
 933      * However, decimal negative powers of 5 smaller than 0.5 are *not* exactly
 934      * representable as binary fraction.  In particular, 0.005 (the round limit
 935      * for a two-digit scale) and 0.0005 (the round limit for a three-digit
 936      * scale) are not representable. Therefore, for input values near these cases
 937      * the sticky bit is known to be set which reduces the rounding logic to:
 938      *
 939      * Last   Round   Sticky
 940      * 6      5       1      => 7   // a little bit more than halfway, round up.
 941      * 7      5       1      => 8   // a little bit more than halfway, round up.
 942      *
 943      * In other words, if the round digit is 5, the sticky bit is known to be
 944      * set.  If the round digit is something other than 5, the sticky bit is not
 945      * relevant.  Therefore, some of the logic about whether or not to increment
 946      * the destination *decimal* value can occur based on tests of *binary*
 947      * computations of the binary input number.
 948      */
 949 
 950     /**
 951      * Check validity of using fast-path for this instance. If fast-path is valid
 952      * for this instance, sets fast-path state as true and initializes fast-path
 953      * utility fields as needed.
 954      *
 955      * This method is supposed to be called rarely, otherwise that will break the
 956      * fast-path performance. That means avoiding frequent changes of the
 957      * properties of the instance, since for most properties, each time a change
 958      * happens, a call to this method is needed at the next format call.
 959      *
 960      * FAST-PATH RULES:
 961      *  Similar to the default DecimalFormat instantiation case.
 962      *  More precisely:
 963      *  - HALF_EVEN rounding mode,
 964      *  - isGroupingUsed() is true,
 965      *  - groupingSize of 3,
 966      *  - multiplier is 1,
 967      *  - Decimal separator not mandatory,
 968      *  - No use of exponential notation,
 969      *  - minimumIntegerDigits is exactly 1 and maximumIntegerDigits at least 10
 970      *  - For number of fractional digits, the exact values found in the default case:
 971      *     Currency : min = max = 2.
 972      *     Decimal  : min = 0. max = 3.
 973      *
 974      */
 975     private boolean checkAndSetFastPathStatus() {
 976 
 977         boolean fastPathWasOn = isFastPath;
 978 
 979         if ((roundingMode == RoundingMode.HALF_EVEN) &&
 980             (isGroupingUsed()) &&
 981             (groupingSize == 3) &&
 982             (multiplier == 1) &&
 983             (!decimalSeparatorAlwaysShown) &&
 984             (!useExponentialNotation)) {
 985 
 986             // The fast-path algorithm is semi-hardcoded against
 987             //  minimumIntegerDigits and maximumIntegerDigits.
 988             isFastPath = ((minimumIntegerDigits == 1) &&
 989                           (maximumIntegerDigits >= 10));
 990 
 991             // The fast-path algorithm is hardcoded against
 992             //  minimumFractionDigits and maximumFractionDigits.
 993             if (isFastPath) {
 994                 if (isCurrencyFormat) {
 995                     if ((minimumFractionDigits != 2) ||
 996                         (maximumFractionDigits != 2))
 997                         isFastPath = false;
 998                 } else if ((minimumFractionDigits != 0) ||
 999                            (maximumFractionDigits != 3))
1000                     isFastPath = false;
1001             }
1002         } else
1003             isFastPath = false;
1004 
1005         resetFastPathData(fastPathWasOn);
1006         fastPathCheckNeeded = false;
1007 
1008         /*
1009          * Returns true after successfully checking the fast path condition and
1010          * setting the fast path data. The return value is used by the
1011          * fastFormat() method to decide whether to call the resetFastPathData
1012          * method to reinitialize fast path data or is it already initialized
1013          * in this method.
1014          */
1015         return true;
1016     }
1017 
1018     private void resetFastPathData(boolean fastPathWasOn) {
1019         // Since some instance properties may have changed while still falling
1020         // in the fast-path case, we need to reinitialize fastPathData anyway.
1021         if (isFastPath) {
1022             // We need to instantiate fastPathData if not already done.
1023             if (fastPathData == null) {
1024                 fastPathData = new FastPathData();
1025             }
1026 
1027             // Sets up the locale specific constants used when formatting.
1028             // '0' is our default representation of zero.
1029             fastPathData.zeroDelta = symbols.getZeroDigit() - '0';
1030             fastPathData.groupingChar = symbols.getGroupingSeparator();
1031 
1032             // Sets up fractional constants related to currency/decimal pattern.
1033             fastPathData.fractionalMaxIntBound = (isCurrencyFormat)
1034                     ? 99 : 999;
1035             fastPathData.fractionalScaleFactor = (isCurrencyFormat)
1036                     ? 100.0d : 1000.0d;
1037 
1038             // Records the need for adding prefix or suffix
1039             fastPathData.positiveAffixesRequired
1040                     = (positivePrefix.length() != 0)
1041                         || (positiveSuffix.length() != 0);
1042             fastPathData.negativeAffixesRequired
1043                     = (negativePrefix.length() != 0)
1044                         || (negativeSuffix.length() != 0);
1045 
1046             // Creates a cached char container for result, with max possible size.
1047             int maxNbIntegralDigits = 10;
1048             int maxNbGroups = 3;
1049             int containerSize
1050                     = Math.max(positivePrefix.length(), negativePrefix.length())
1051                     + maxNbIntegralDigits + maxNbGroups + 1
1052                     + maximumFractionDigits
1053                     + Math.max(positiveSuffix.length(), negativeSuffix.length());
1054 
1055             fastPathData.fastPathContainer = new char[containerSize];
1056 
1057             // Sets up prefix and suffix char arrays constants.
1058             fastPathData.charsPositiveSuffix = positiveSuffix.toCharArray();
1059             fastPathData.charsNegativeSuffix = negativeSuffix.toCharArray();
1060             fastPathData.charsPositivePrefix = positivePrefix.toCharArray();
1061             fastPathData.charsNegativePrefix = negativePrefix.toCharArray();
1062 
1063             // Sets up fixed index positions for integral and fractional digits.
1064             // Sets up decimal point in cached result container.
1065             int longestPrefixLength
1066                     = Math.max(positivePrefix.length(),
1067                             negativePrefix.length());
1068             int decimalPointIndex
1069                     = maxNbIntegralDigits + maxNbGroups + longestPrefixLength;
1070 
1071             fastPathData.integralLastIndex = decimalPointIndex - 1;
1072             fastPathData.fractionalFirstIndex = decimalPointIndex + 1;
1073             fastPathData.fastPathContainer[decimalPointIndex]
1074                     = isCurrencyFormat
1075                             ? symbols.getMonetaryDecimalSeparator()
1076                             : symbols.getDecimalSeparator();
1077 
1078         } else if (fastPathWasOn) {
1079             // Previous state was fast-path and is no more.
1080             // Resets cached array constants.
1081             fastPathData.fastPathContainer = null;
1082             fastPathData.charsPositiveSuffix = null;
1083             fastPathData.charsNegativeSuffix = null;
1084             fastPathData.charsPositivePrefix = null;
1085             fastPathData.charsNegativePrefix = null;
1086         }
1087     }
1088 
1089     /**
1090      * Returns true if rounding-up must be done on {@code scaledFractionalPartAsInt},
1091      * false otherwise.
1092      *
1093      * This is a utility method that takes correct half-even rounding decision on
1094      * passed fractional value at the scaled decimal point (2 digits for currency
1095      * case and 3 for decimal case), when the approximated fractional part after
1096      * scaled decimal point is exactly 0.5d.  This is done by means of exact
1097      * calculations on the {@code fractionalPart} floating-point value.
1098      *
1099      * This method is supposed to be called by private {@code fastDoubleFormat}
1100      * method only.
1101      *
1102      * The algorithms used for the exact calculations are :
1103      *
1104      * The <b><i>FastTwoSum</i></b> algorithm, from T.J.Dekker, described in the
1105      * papers  "<i>A  Floating-Point   Technique  for  Extending  the  Available
1106      * Precision</i>"  by Dekker, and  in "<i>Adaptive  Precision Floating-Point
1107      * Arithmetic and Fast Robust Geometric Predicates</i>" from J.Shewchuk.
1108      *
1109      * A modified version of <b><i>Sum2S</i></b> cascaded summation described in
1110      * "<i>Accurate Sum and Dot Product</i>" from Takeshi Ogita and All.  As
1111      * Ogita says in this paper this is an equivalent of the Kahan-Babuska's
1112      * summation algorithm because we order the terms by magnitude before summing
1113      * them. For this reason we can use the <i>FastTwoSum</i> algorithm rather
1114      * than the more expensive Knuth's <i>TwoSum</i>.
1115      *
1116      * We do this to avoid a more expensive exact "<i>TwoProduct</i>" algorithm,
1117      * like those described in Shewchuk's paper above. See comments in the code
1118      * below.
1119      *
1120      * @param  fractionalPart The  fractional value  on which  we  take rounding
1121      * decision.
1122      * @param scaledFractionalPartAsInt The integral part of the scaled
1123      * fractional value.
1124      *
1125      * @return the decision that must be taken regarding half-even rounding.
1126      */
1127     private boolean exactRoundUp(double fractionalPart,
1128                                  int scaledFractionalPartAsInt) {
1129 
1130         /* exactRoundUp() method is called by fastDoubleFormat() only.
1131          * The precondition expected to be verified by the passed parameters is :
1132          * scaledFractionalPartAsInt ==
1133          *     (int) (fractionalPart * fastPathData.fractionalScaleFactor).
1134          * This is ensured by fastDoubleFormat() code.
1135          */
1136 
1137         /* We first calculate roundoff error made by fastDoubleFormat() on
1138          * the scaled fractional part. We do this with exact calculation on the
1139          * passed fractionalPart. Rounding decision will then be taken from roundoff.
1140          */
1141 
1142         /* ---- TwoProduct(fractionalPart, scale factor (i.e. 1000.0d or 100.0d)).
1143          *
1144          * The below is an optimized exact "TwoProduct" calculation of passed
1145          * fractional part with scale factor, using Ogita's Sum2S cascaded
1146          * summation adapted as Kahan-Babuska equivalent by using FastTwoSum
1147          * (much faster) rather than Knuth's TwoSum.
1148          *
1149          * We can do this because we order the summation from smallest to
1150          * greatest, so that FastTwoSum can be used without any additional error.
1151          *
1152          * The "TwoProduct" exact calculation needs 17 flops. We replace this by
1153          * a cascaded summation of FastTwoSum calculations, each involving an
1154          * exact multiply by a power of 2.
1155          *
1156          * Doing so saves overall 4 multiplications and 1 addition compared to
1157          * using traditional "TwoProduct".
1158          *
1159          * The scale factor is either 100 (currency case) or 1000 (decimal case).
1160          * - when 1000, we replace it by (1024 - 16 - 8) = 1000.
1161          * - when 100,  we replace it by (128  - 32 + 4) =  100.
1162          * Every multiplication by a power of 2 (1024, 128, 32, 16, 8, 4) is exact.
1163          *
1164          */
1165         double approxMax;    // Will always be positive.
1166         double approxMedium; // Will always be negative.
1167         double approxMin;
1168 
1169         double fastTwoSumApproximation = 0.0d;
1170         double fastTwoSumRoundOff = 0.0d;
1171         double bVirtual = 0.0d;
1172 
1173         if (isCurrencyFormat) {
1174             // Scale is 100 = 128 - 32 + 4.
1175             // Multiply by 2**n is a shift. No roundoff. No error.
1176             approxMax    = fractionalPart * 128.00d;
1177             approxMedium = - (fractionalPart * 32.00d);
1178             approxMin    = fractionalPart * 4.00d;
1179         } else {
1180             // Scale is 1000 = 1024 - 16 - 8.
1181             // Multiply by 2**n is a shift. No roundoff. No error.
1182             approxMax    = fractionalPart * 1024.00d;
1183             approxMedium = - (fractionalPart * 16.00d);
1184             approxMin    = - (fractionalPart * 8.00d);
1185         }
1186 
1187         // Shewchuk/Dekker's FastTwoSum(approxMedium, approxMin).
1188         assert(-approxMedium >= Math.abs(approxMin));
1189         fastTwoSumApproximation = approxMedium + approxMin;
1190         bVirtual = fastTwoSumApproximation - approxMedium;
1191         fastTwoSumRoundOff = approxMin - bVirtual;
1192         double approxS1 = fastTwoSumApproximation;
1193         double roundoffS1 = fastTwoSumRoundOff;
1194 
1195         // Shewchuk/Dekker's FastTwoSum(approxMax, approxS1);
1196         assert(approxMax >= Math.abs(approxS1));
1197         fastTwoSumApproximation = approxMax + approxS1;
1198         bVirtual = fastTwoSumApproximation - approxMax;
1199         fastTwoSumRoundOff = approxS1 - bVirtual;
1200         double roundoff1000 = fastTwoSumRoundOff;
1201         double approx1000 = fastTwoSumApproximation;
1202         double roundoffTotal = roundoffS1 + roundoff1000;
1203 
1204         // Shewchuk/Dekker's FastTwoSum(approx1000, roundoffTotal);
1205         assert(approx1000 >= Math.abs(roundoffTotal));
1206         fastTwoSumApproximation = approx1000 + roundoffTotal;
1207         bVirtual = fastTwoSumApproximation - approx1000;
1208 
1209         // Now we have got the roundoff for the scaled fractional
1210         double scaledFractionalRoundoff = roundoffTotal - bVirtual;
1211 
1212         // ---- TwoProduct(fractionalPart, scale (i.e. 1000.0d or 100.0d)) end.
1213 
1214         /* ---- Taking the rounding decision
1215          *
1216          * We take rounding decision based on roundoff and half-even rounding
1217          * rule.
1218          *
1219          * The above TwoProduct gives us the exact roundoff on the approximated
1220          * scaled fractional, and we know that this approximation is exactly
1221          * 0.5d, since that has already been tested by the caller
1222          * (fastDoubleFormat).
1223          *
1224          * Decision comes first from the sign of the calculated exact roundoff.
1225          * - Since being exact roundoff, it cannot be positive with a scaled
1226          *   fractional less than 0.5d, as well as negative with a scaled
1227          *   fractional greater than 0.5d. That leaves us with following 3 cases.
1228          * - positive, thus scaled fractional == 0.500....0fff ==> round-up.
1229          * - negative, thus scaled fractional == 0.499....9fff ==> don't round-up.
1230          * - is zero,  thus scaled fractioanl == 0.5 ==> half-even rounding applies :
1231          *    we round-up only if the integral part of the scaled fractional is odd.
1232          *
1233          */
1234         if (scaledFractionalRoundoff > 0.0) {
1235             return true;
1236         } else if (scaledFractionalRoundoff < 0.0) {
1237             return false;
1238         } else if ((scaledFractionalPartAsInt & 1) != 0) {
1239             return true;
1240         }
1241 
1242         return false;
1243 
1244         // ---- Taking the rounding decision end
1245     }
1246 
1247     /**
1248      * Collects integral digits from passed {@code number}, while setting
1249      * grouping chars as needed. Updates {@code firstUsedIndex} accordingly.
1250      *
1251      * Loops downward starting from {@code backwardIndex} position (inclusive).
1252      *
1253      * @param number  The int value from which we collect digits.
1254      * @param digitsBuffer The char array container where digits and grouping chars
1255      *  are stored.
1256      * @param backwardIndex the position from which we start storing digits in
1257      *  digitsBuffer.
1258      *
1259      */
1260     private void collectIntegralDigits(int number,
1261                                        char[] digitsBuffer,
1262                                        int backwardIndex) {
1263         int index = backwardIndex;
1264         int q;
1265         int r;
1266         while (number > 999) {
1267             // Generates 3 digits per iteration.
1268             q = number / 1000;
1269             r = number - (q << 10) + (q << 4) + (q << 3); // -1024 +16 +8 = 1000.
1270             number = q;
1271 
1272             digitsBuffer[index--] = DigitArrays.DigitOnes1000[r];
1273             digitsBuffer[index--] = DigitArrays.DigitTens1000[r];
1274             digitsBuffer[index--] = DigitArrays.DigitHundreds1000[r];
1275             digitsBuffer[index--] = fastPathData.groupingChar;
1276         }
1277 
1278         // Collects last 3 or less digits.
1279         digitsBuffer[index] = DigitArrays.DigitOnes1000[number];
1280         if (number > 9) {
1281             digitsBuffer[--index]  = DigitArrays.DigitTens1000[number];
1282             if (number > 99)
1283                 digitsBuffer[--index]   = DigitArrays.DigitHundreds1000[number];
1284         }
1285 
1286         fastPathData.firstUsedIndex = index;
1287     }
1288 
1289     /**
1290      * Collects the 2 (currency) or 3 (decimal) fractional digits from passed
1291      * {@code number}, starting at {@code startIndex} position
1292      * inclusive.  There is no punctuation to set here (no grouping chars).
1293      * Updates {@code fastPathData.lastFreeIndex} accordingly.
1294      *
1295      *
1296      * @param number  The int value from which we collect digits.
1297      * @param digitsBuffer The char array container where digits are stored.
1298      * @param startIndex the position from which we start storing digits in
1299      *  digitsBuffer.
1300      *
1301      */
1302     private void collectFractionalDigits(int number,
1303                                          char[] digitsBuffer,
1304                                          int startIndex) {
1305         int index = startIndex;
1306 
1307         char digitOnes = DigitArrays.DigitOnes1000[number];
1308         char digitTens = DigitArrays.DigitTens1000[number];
1309 
1310         if (isCurrencyFormat) {
1311             // Currency case. Always collects fractional digits.
1312             digitsBuffer[index++] = digitTens;
1313             digitsBuffer[index++] = digitOnes;
1314         } else if (number != 0) {
1315             // Decimal case. Hundreds will always be collected
1316             digitsBuffer[index++] = DigitArrays.DigitHundreds1000[number];
1317 
1318             // Ending zeros won't be collected.
1319             if (digitOnes != '0') {
1320                 digitsBuffer[index++] = digitTens;
1321                 digitsBuffer[index++] = digitOnes;
1322             } else if (digitTens != '0')
1323                 digitsBuffer[index++] = digitTens;
1324 
1325         } else
1326             // This is decimal pattern and fractional part is zero.
1327             // We must remove decimal point from result.
1328             index--;
1329 
1330         fastPathData.lastFreeIndex = index;
1331     }
1332 
1333     /**
1334      * Internal utility.
1335      * Adds the passed {@code prefix} and {@code suffix} to {@code container}.
1336      *
1337      * @param container  Char array container which to prepend/append the
1338      *  prefix/suffix.
1339      * @param prefix     Char sequence to prepend as a prefix.
1340      * @param suffix     Char sequence to append as a suffix.
1341      *
1342      */
1343     //    private void addAffixes(boolean isNegative, char[] container) {
1344     private void addAffixes(char[] container, char[] prefix, char[] suffix) {
1345 
1346         // We add affixes only if needed (affix length > 0).
1347         int pl = prefix.length;
1348         int sl = suffix.length;
1349         if (pl != 0) prependPrefix(prefix, pl, container);
1350         if (sl != 0) appendSuffix(suffix, sl, container);
1351 
1352     }
1353 
1354     /**
1355      * Prepends the passed {@code prefix} chars to given result
1356      * {@code container}.  Updates {@code fastPathData.firstUsedIndex}
1357      * accordingly.
1358      *
1359      * @param prefix The prefix characters to prepend to result.
1360      * @param len The number of chars to prepend.
1361      * @param container Char array container which to prepend the prefix
1362      */
1363     private void prependPrefix(char[] prefix,
1364                                int len,
1365                                char[] container) {
1366 
1367         fastPathData.firstUsedIndex -= len;
1368         int startIndex = fastPathData.firstUsedIndex;
1369 
1370         // If prefix to prepend is only 1 char long, just assigns this char.
1371         // If prefix is less or equal 4, we use a dedicated algorithm that
1372         //  has shown to run faster than System.arraycopy.
1373         // If more than 4, we use System.arraycopy.
1374         if (len == 1)
1375             container[startIndex] = prefix[0];
1376         else if (len <= 4) {
1377             int dstLower = startIndex;
1378             int dstUpper = dstLower + len - 1;
1379             int srcUpper = len - 1;
1380             container[dstLower] = prefix[0];
1381             container[dstUpper] = prefix[srcUpper];
1382 
1383             if (len > 2)
1384                 container[++dstLower] = prefix[1];
1385             if (len == 4)
1386                 container[--dstUpper] = prefix[2];
1387         } else
1388             System.arraycopy(prefix, 0, container, startIndex, len);
1389     }
1390 
1391     /**
1392      * Appends the passed {@code suffix} chars to given result
1393      * {@code container}.  Updates {@code fastPathData.lastFreeIndex}
1394      * accordingly.
1395      *
1396      * @param suffix The suffix characters to append to result.
1397      * @param len The number of chars to append.
1398      * @param container Char array container which to append the suffix
1399      */
1400     private void appendSuffix(char[] suffix,
1401                               int len,
1402                               char[] container) {
1403 
1404         int startIndex = fastPathData.lastFreeIndex;
1405 
1406         // If suffix to append is only 1 char long, just assigns this char.
1407         // If suffix is less or equal 4, we use a dedicated algorithm that
1408         //  has shown to run faster than System.arraycopy.
1409         // If more than 4, we use System.arraycopy.
1410         if (len == 1)
1411             container[startIndex] = suffix[0];
1412         else if (len <= 4) {
1413             int dstLower = startIndex;
1414             int dstUpper = dstLower + len - 1;
1415             int srcUpper = len - 1;
1416             container[dstLower] = suffix[0];
1417             container[dstUpper] = suffix[srcUpper];
1418 
1419             if (len > 2)
1420                 container[++dstLower] = suffix[1];
1421             if (len == 4)
1422                 container[--dstUpper] = suffix[2];
1423         } else
1424             System.arraycopy(suffix, 0, container, startIndex, len);
1425 
1426         fastPathData.lastFreeIndex += len;
1427     }
1428 
1429     /**
1430      * Converts digit chars from {@code digitsBuffer} to current locale.
1431      *
1432      * Must be called before adding affixes since we refer to
1433      * {@code fastPathData.firstUsedIndex} and {@code fastPathData.lastFreeIndex},
1434      * and do not support affixes (for speed reason).
1435      *
1436      * We loop backward starting from last used index in {@code fastPathData}.
1437      *
1438      * @param digitsBuffer The char array container where the digits are stored.
1439      */
1440     private void localizeDigits(char[] digitsBuffer) {
1441 
1442         // We will localize only the digits, using the groupingSize,
1443         // and taking into account fractional part.
1444 
1445         // First take into account fractional part.
1446         int digitsCounter =
1447             fastPathData.lastFreeIndex - fastPathData.fractionalFirstIndex;
1448 
1449         // The case when there is no fractional digits.
1450         if (digitsCounter < 0)
1451             digitsCounter = groupingSize;
1452 
1453         // Only the digits remains to localize.
1454         for (int cursor = fastPathData.lastFreeIndex - 1;
1455              cursor >= fastPathData.firstUsedIndex;
1456              cursor--) {
1457             if (digitsCounter != 0) {
1458                 // This is a digit char, we must localize it.
1459                 digitsBuffer[cursor] += fastPathData.zeroDelta;
1460                 digitsCounter--;
1461             } else {
1462                 // Decimal separator or grouping char. Reinit counter only.
1463                 digitsCounter = groupingSize;
1464             }
1465         }
1466     }
1467 
1468     /**
1469      * This is the main entry point for the fast-path format algorithm.
1470      *
1471      * At this point we are sure to be in the expected conditions to run it.
1472      * This algorithm builds the formatted result and puts it in the dedicated
1473      * {@code fastPathData.fastPathContainer}.
1474      *
1475      * @param d the double value to be formatted.
1476      * @param negative Flag precising if {@code d} is negative.
1477      */
1478     private void fastDoubleFormat(double d,
1479                                   boolean negative) {
1480 
1481         char[] container = fastPathData.fastPathContainer;
1482 
1483         /*
1484          * The principle of the algorithm is to :
1485          * - Break the passed double into its integral and fractional parts
1486          *    converted into integers.
1487          * - Then decide if rounding up must be applied or not by following
1488          *    the half-even rounding rule, first using approximated scaled
1489          *    fractional part.
1490          * - For the difficult cases (approximated scaled fractional part
1491          *    being exactly 0.5d), we refine the rounding decision by calling
1492          *    exactRoundUp utility method that both calculates the exact roundoff
1493          *    on the approximation and takes correct rounding decision.
1494          * - We round-up the fractional part if needed, possibly propagating the
1495          *    rounding to integral part if we meet a "all-nine" case for the
1496          *    scaled fractional part.
1497          * - We then collect digits from the resulting integral and fractional
1498          *   parts, also setting the required grouping chars on the fly.
1499          * - Then we localize the collected digits if needed, and
1500          * - Finally prepend/append prefix/suffix if any is needed.
1501          */
1502 
1503         // Exact integral part of d.
1504         int integralPartAsInt = (int) d;
1505 
1506         // Exact fractional part of d (since we subtract it's integral part).
1507         double exactFractionalPart = d - (double) integralPartAsInt;
1508 
1509         // Approximated scaled fractional part of d (due to multiplication).
1510         double scaledFractional =
1511             exactFractionalPart * fastPathData.fractionalScaleFactor;
1512 
1513         // Exact integral part of scaled fractional above.
1514         int fractionalPartAsInt = (int) scaledFractional;
1515 
1516         // Exact fractional part of scaled fractional above.
1517         scaledFractional = scaledFractional - (double) fractionalPartAsInt;
1518 
1519         // Only when scaledFractional is exactly 0.5d do we have to do exact
1520         // calculations and take fine-grained rounding decision, since
1521         // approximated results above may lead to incorrect decision.
1522         // Otherwise comparing against 0.5d (strictly greater or less) is ok.
1523         boolean roundItUp = false;
1524         if (scaledFractional >= 0.5d) {
1525             if (scaledFractional == 0.5d)
1526                 // Rounding need fine-grained decision.
1527                 roundItUp = exactRoundUp(exactFractionalPart, fractionalPartAsInt);
1528             else
1529                 roundItUp = true;
1530 
1531             if (roundItUp) {
1532                 // Rounds up both fractional part (and also integral if needed).
1533                 if (fractionalPartAsInt < fastPathData.fractionalMaxIntBound) {
1534                     fractionalPartAsInt++;
1535                 } else {
1536                     // Propagates rounding to integral part since "all nines" case.
1537                     fractionalPartAsInt = 0;
1538                     integralPartAsInt++;
1539                 }
1540             }
1541         }
1542 
1543         // Collecting digits.
1544         collectFractionalDigits(fractionalPartAsInt, container,
1545                                 fastPathData.fractionalFirstIndex);
1546         collectIntegralDigits(integralPartAsInt, container,
1547                               fastPathData.integralLastIndex);
1548 
1549         // Localizing digits.
1550         if (fastPathData.zeroDelta != 0)
1551             localizeDigits(container);
1552 
1553         // Adding prefix and suffix.
1554         if (negative) {
1555             if (fastPathData.negativeAffixesRequired)
1556                 addAffixes(container,
1557                            fastPathData.charsNegativePrefix,
1558                            fastPathData.charsNegativeSuffix);
1559         } else if (fastPathData.positiveAffixesRequired)
1560             addAffixes(container,
1561                        fastPathData.charsPositivePrefix,
1562                        fastPathData.charsPositiveSuffix);
1563     }
1564 
1565     /**
1566      * A fast-path shortcut of format(double) to be called by NumberFormat, or by
1567      * format(double, ...) public methods.
1568      *
1569      * If instance can be applied fast-path and passed double is not NaN or
1570      * Infinity, is in the integer range, we call {@code fastDoubleFormat}
1571      * after changing {@code d} to its positive value if necessary.
1572      *
1573      * Otherwise returns null by convention since fast-path can't be exercized.
1574      *
1575      * @param d The double value to be formatted
1576      *
1577      * @return the formatted result for {@code d} as a string.
1578      */
1579     String fastFormat(double d) {
1580         boolean isDataSet = false;
1581         // (Re-)Evaluates fast-path status if needed.
1582         if (fastPathCheckNeeded) {
1583             isDataSet = checkAndSetFastPathStatus();
1584         }
1585 
1586         if (!isFastPath )
1587             // DecimalFormat instance is not in a fast-path state.
1588             return null;
1589 
1590         if (!Double.isFinite(d))
1591             // Should not use fast-path for Infinity and NaN.
1592             return null;
1593 
1594         // Extracts and records sign of double value, possibly changing it
1595         // to a positive one, before calling fastDoubleFormat().
1596         boolean negative = false;
1597         if (d < 0.0d) {
1598             negative = true;
1599             d = -d;
1600         } else if (d == 0.0d) {
1601             negative = (Math.copySign(1.0d, d) == -1.0d);
1602             d = +0.0d;
1603         }
1604 
1605         if (d > MAX_INT_AS_DOUBLE)
1606             // Filters out values that are outside expected fast-path range
1607             return null;
1608         else {
1609             if (!isDataSet) {
1610                 /*
1611                  * If the fast path data is not set through
1612                  * checkAndSetFastPathStatus() and fulfil the
1613                  * fast path conditions then reset the data
1614                  * directly through resetFastPathData()
1615                  */
1616                 resetFastPathData(isFastPath);
1617             }
1618             fastDoubleFormat(d, negative);
1619 
1620         }
1621 
1622 
1623         // Returns a new string from updated fastPathContainer.
1624         return new String(fastPathData.fastPathContainer,
1625                           fastPathData.firstUsedIndex,
1626                           fastPathData.lastFreeIndex - fastPathData.firstUsedIndex);
1627 
1628     }
1629 
1630     // ======== End fast-path formating logic for double =========================
1631 
1632     /**
1633      * Complete the formatting of a finite number.  On entry, the digitList must
1634      * be filled in with the correct digits.
1635      */
1636     private StringBuffer subformat(StringBuffer result, FieldDelegate delegate,
1637                                    boolean isNegative, boolean isInteger,
1638                                    int maxIntDigits, int minIntDigits,
1639                                    int maxFraDigits, int minFraDigits) {
1640         // NOTE: This isn't required anymore because DigitList takes care of this.
1641         //
1642         //  // The negative of the exponent represents the number of leading
1643         //  // zeros between the decimal and the first non-zero digit, for
1644         //  // a value < 0.1 (e.g., for 0.00123, -fExponent == 2).  If this
1645         //  // is more than the maximum fraction digits, then we have an underflow
1646         //  // for the printed representation.  We recognize this here and set
1647         //  // the DigitList representation to zero in this situation.
1648         //
1649         //  if (-digitList.decimalAt >= getMaximumFractionDigits())
1650         //  {
1651         //      digitList.count = 0;
1652         //  }
1653 
1654         char zero = symbols.getZeroDigit();
1655         int zeroDelta = zero - '0'; // '0' is the DigitList representation of zero
1656         char grouping = symbols.getGroupingSeparator();
1657         char decimal = isCurrencyFormat ?
1658             symbols.getMonetaryDecimalSeparator() :
1659             symbols.getDecimalSeparator();
1660 
1661         /* Per bug 4147706, DecimalFormat must respect the sign of numbers which
1662          * format as zero.  This allows sensible computations and preserves
1663          * relations such as signum(1/x) = signum(x), where x is +Infinity or
1664          * -Infinity.  Prior to this fix, we always formatted zero values as if
1665          * they were positive.  Liu 7/6/98.
1666          */
1667         if (digitList.isZero()) {
1668             digitList.decimalAt = 0; // Normalize
1669         }
1670 
1671         if (isNegative) {
1672             append(result, negativePrefix, delegate,
1673                    getNegativePrefixFieldPositions(), Field.SIGN);
1674         } else {
1675             append(result, positivePrefix, delegate,
1676                    getPositivePrefixFieldPositions(), Field.SIGN);
1677         }
1678 
1679         if (useExponentialNotation) {
1680             int iFieldStart = result.length();
1681             int iFieldEnd = -1;
1682             int fFieldStart = -1;
1683 
1684             // Minimum integer digits are handled in exponential format by
1685             // adjusting the exponent.  For example, 0.01234 with 3 minimum
1686             // integer digits is "123.4E-4".
1687 
1688             // Maximum integer digits are interpreted as indicating the
1689             // repeating range.  This is useful for engineering notation, in
1690             // which the exponent is restricted to a multiple of 3.  For
1691             // example, 0.01234 with 3 maximum integer digits is "12.34e-3".
1692             // If maximum integer digits are > 1 and are larger than
1693             // minimum integer digits, then minimum integer digits are
1694             // ignored.
1695             int exponent = digitList.decimalAt;
1696             int repeat = maxIntDigits;
1697             int minimumIntegerDigits = minIntDigits;
1698             if (repeat > 1 && repeat > minIntDigits) {
1699                 // A repeating range is defined; adjust to it as follows.
1700                 // If repeat == 3, we have 6,5,4=>3; 3,2,1=>0; 0,-1,-2=>-3;
1701                 // -3,-4,-5=>-6, etc. This takes into account that the
1702                 // exponent we have here is off by one from what we expect;
1703                 // it is for the format 0.MMMMMx10^n.
1704                 if (exponent >= 1) {
1705                     exponent = ((exponent - 1) / repeat) * repeat;
1706                 } else {
1707                     // integer division rounds towards 0
1708                     exponent = ((exponent - repeat) / repeat) * repeat;
1709                 }
1710                 minimumIntegerDigits = 1;
1711             } else {
1712                 // No repeating range is defined; use minimum integer digits.
1713                 exponent -= minimumIntegerDigits;
1714             }
1715 
1716             // We now output a minimum number of digits, and more if there
1717             // are more digits, up to the maximum number of digits.  We
1718             // place the decimal point after the "integer" digits, which
1719             // are the first (decimalAt - exponent) digits.
1720             int minimumDigits = minIntDigits + minFraDigits;
1721             if (minimumDigits < 0) {    // overflow?
1722                 minimumDigits = Integer.MAX_VALUE;
1723             }
1724 
1725             // The number of integer digits is handled specially if the number
1726             // is zero, since then there may be no digits.
1727             int integerDigits = digitList.isZero() ? minimumIntegerDigits :
1728                     digitList.decimalAt - exponent;
1729             if (minimumDigits < integerDigits) {
1730                 minimumDigits = integerDigits;
1731             }
1732             int totalDigits = digitList.count;
1733             if (minimumDigits > totalDigits) {
1734                 totalDigits = minimumDigits;
1735             }
1736             boolean addedDecimalSeparator = false;
1737 
1738             for (int i=0; i<totalDigits; ++i) {
1739                 if (i == integerDigits) {
1740                     // Record field information for caller.
1741                     iFieldEnd = result.length();
1742 
1743                     result.append(decimal);
1744                     addedDecimalSeparator = true;
1745 
1746                     // Record field information for caller.
1747                     fFieldStart = result.length();
1748                 }
1749                 result.append((i < digitList.count) ?
1750                               (char)(digitList.digits[i] + zeroDelta) :
1751                               zero);
1752             }
1753 
1754             if (decimalSeparatorAlwaysShown && totalDigits == integerDigits) {
1755                 // Record field information for caller.
1756                 iFieldEnd = result.length();
1757 
1758                 result.append(decimal);
1759                 addedDecimalSeparator = true;
1760 
1761                 // Record field information for caller.
1762                 fFieldStart = result.length();
1763             }
1764 
1765             // Record field information
1766             if (iFieldEnd == -1) {
1767                 iFieldEnd = result.length();
1768             }
1769             delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
1770                                iFieldStart, iFieldEnd, result);
1771             if (addedDecimalSeparator) {
1772                 delegate.formatted(Field.DECIMAL_SEPARATOR,
1773                                    Field.DECIMAL_SEPARATOR,
1774                                    iFieldEnd, fFieldStart, result);
1775             }
1776             if (fFieldStart == -1) {
1777                 fFieldStart = result.length();
1778             }
1779             delegate.formatted(FRACTION_FIELD, Field.FRACTION, Field.FRACTION,
1780                                fFieldStart, result.length(), result);
1781 
1782             // The exponent is output using the pattern-specified minimum
1783             // exponent digits.  There is no maximum limit to the exponent
1784             // digits, since truncating the exponent would result in an
1785             // unacceptable inaccuracy.
1786             int fieldStart = result.length();
1787 
1788             result.append(symbols.getExponentSeparator());
1789 
1790             delegate.formatted(Field.EXPONENT_SYMBOL, Field.EXPONENT_SYMBOL,
1791                                fieldStart, result.length(), result);
1792 
1793             // For zero values, we force the exponent to zero.  We
1794             // must do this here, and not earlier, because the value
1795             // is used to determine integer digit count above.
1796             if (digitList.isZero()) {
1797                 exponent = 0;
1798             }
1799 
1800             boolean negativeExponent = exponent < 0;
1801             if (negativeExponent) {
1802                 exponent = -exponent;
1803                 fieldStart = result.length();
1804                 result.append(symbols.getMinusSign());
1805                 delegate.formatted(Field.EXPONENT_SIGN, Field.EXPONENT_SIGN,
1806                                    fieldStart, result.length(), result);
1807             }
1808             digitList.set(negativeExponent, exponent);
1809 
1810             int eFieldStart = result.length();
1811 
1812             for (int i=digitList.decimalAt; i<minExponentDigits; ++i) {
1813                 result.append(zero);
1814             }
1815             for (int i=0; i<digitList.decimalAt; ++i) {
1816                 result.append((i < digitList.count) ?
1817                           (char)(digitList.digits[i] + zeroDelta) : zero);
1818             }
1819             delegate.formatted(Field.EXPONENT, Field.EXPONENT, eFieldStart,
1820                                result.length(), result);
1821         } else {
1822             int iFieldStart = result.length();
1823 
1824             // Output the integer portion.  Here 'count' is the total
1825             // number of integer digits we will display, including both
1826             // leading zeros required to satisfy getMinimumIntegerDigits,
1827             // and actual digits present in the number.
1828             int count = minIntDigits;
1829             int digitIndex = 0; // Index into digitList.fDigits[]
1830             if (digitList.decimalAt > 0 && count < digitList.decimalAt) {
1831                 count = digitList.decimalAt;
1832             }
1833 
1834             // Handle the case where getMaximumIntegerDigits() is smaller
1835             // than the real number of integer digits.  If this is so, we
1836             // output the least significant max integer digits.  For example,
1837             // the value 1997 printed with 2 max integer digits is just "97".
1838             if (count > maxIntDigits) {
1839                 count = maxIntDigits;
1840                 digitIndex = digitList.decimalAt - count;
1841             }
1842 
1843             int sizeBeforeIntegerPart = result.length();
1844             for (int i=count-1; i>=0; --i) {
1845                 if (i < digitList.decimalAt && digitIndex < digitList.count) {
1846                     // Output a real digit
1847                     result.append((char)(digitList.digits[digitIndex++] + zeroDelta));
1848                 } else {
1849                     // Output a leading zero
1850                     result.append(zero);
1851                 }
1852 
1853                 // Output grouping separator if necessary.  Don't output a
1854                 // grouping separator if i==0 though; that's at the end of
1855                 // the integer part.
1856                 if (isGroupingUsed() && i>0 && (groupingSize != 0) &&
1857                     (i % groupingSize == 0)) {
1858                     int gStart = result.length();
1859                     result.append(grouping);
1860                     delegate.formatted(Field.GROUPING_SEPARATOR,
1861                                        Field.GROUPING_SEPARATOR, gStart,
1862                                        result.length(), result);
1863                 }
1864             }
1865 
1866             // Determine whether or not there are any printable fractional
1867             // digits.  If we've used up the digits we know there aren't.
1868             boolean fractionPresent = (minFraDigits > 0) ||
1869                 (!isInteger && digitIndex < digitList.count);
1870 
1871             // If there is no fraction present, and we haven't printed any
1872             // integer digits, then print a zero.  Otherwise we won't print
1873             // _any_ digits, and we won't be able to parse this string.
1874             if (!fractionPresent && result.length() == sizeBeforeIntegerPart) {
1875                 result.append(zero);
1876             }
1877 
1878             delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
1879                                iFieldStart, result.length(), result);
1880 
1881             // Output the decimal separator if we always do so.
1882             int sStart = result.length();
1883             if (decimalSeparatorAlwaysShown || fractionPresent) {
1884                 result.append(decimal);
1885             }
1886 
1887             if (sStart != result.length()) {
1888                 delegate.formatted(Field.DECIMAL_SEPARATOR,
1889                                    Field.DECIMAL_SEPARATOR,
1890                                    sStart, result.length(), result);
1891             }
1892             int fFieldStart = result.length();
1893 
1894             for (int i=0; i < maxFraDigits; ++i) {
1895                 // Here is where we escape from the loop.  We escape if we've
1896                 // output the maximum fraction digits (specified in the for
1897                 // expression above).
1898                 // We also stop when we've output the minimum digits and either:
1899                 // we have an integer, so there is no fractional stuff to
1900                 // display, or we're out of significant digits.
1901                 if (i >= minFraDigits &&
1902                     (isInteger || digitIndex >= digitList.count)) {
1903                     break;
1904                 }
1905 
1906                 // Output leading fractional zeros. These are zeros that come
1907                 // after the decimal but before any significant digits. These
1908                 // are only output if abs(number being formatted) < 1.0.
1909                 if (-1-i > (digitList.decimalAt-1)) {
1910                     result.append(zero);
1911                     continue;
1912                 }
1913 
1914                 // Output a digit, if we have any precision left, or a
1915                 // zero if we don't.  We don't want to output noise digits.
1916                 if (!isInteger && digitIndex < digitList.count) {
1917                     result.append((char)(digitList.digits[digitIndex++] + zeroDelta));
1918                 } else {
1919                     result.append(zero);
1920                 }
1921             }
1922 
1923             // Record field information for caller.
1924             delegate.formatted(FRACTION_FIELD, Field.FRACTION, Field.FRACTION,
1925                                fFieldStart, result.length(), result);
1926         }
1927 
1928         if (isNegative) {
1929             append(result, negativeSuffix, delegate,
1930                    getNegativeSuffixFieldPositions(), Field.SIGN);
1931         } else {
1932             append(result, positiveSuffix, delegate,
1933                    getPositiveSuffixFieldPositions(), Field.SIGN);
1934         }
1935 
1936         return result;
1937     }
1938 
1939     /**
1940      * Appends the String <code>string</code> to <code>result</code>.
1941      * <code>delegate</code> is notified of all  the
1942      * <code>FieldPosition</code>s in <code>positions</code>.
1943      * <p>
1944      * If one of the <code>FieldPosition</code>s in <code>positions</code>
1945      * identifies a <code>SIGN</code> attribute, it is mapped to
1946      * <code>signAttribute</code>. This is used
1947      * to map the <code>SIGN</code> attribute to the <code>EXPONENT</code>
1948      * attribute as necessary.
1949      * <p>
1950      * This is used by <code>subformat</code> to add the prefix/suffix.
1951      */
1952     private void append(StringBuffer result, String string,
1953                         FieldDelegate delegate,
1954                         FieldPosition[] positions,
1955                         Format.Field signAttribute) {
1956         int start = result.length();
1957 
1958         if (string.length() > 0) {
1959             result.append(string);
1960             for (int counter = 0, max = positions.length; counter < max;
1961                  counter++) {
1962                 FieldPosition fp = positions[counter];
1963                 Format.Field attribute = fp.getFieldAttribute();
1964 
1965                 if (attribute == Field.SIGN) {
1966                     attribute = signAttribute;
1967                 }
1968                 delegate.formatted(attribute, attribute,
1969                                    start + fp.getBeginIndex(),
1970                                    start + fp.getEndIndex(), result);
1971             }
1972         }
1973     }
1974 
1975     /**
1976      * Parses text from a string to produce a <code>Number</code>.
1977      * <p>
1978      * The method attempts to parse text starting at the index given by
1979      * <code>pos</code>.
1980      * If parsing succeeds, then the index of <code>pos</code> is updated
1981      * to the index after the last character used (parsing does not necessarily
1982      * use all characters up to the end of the string), and the parsed
1983      * number is returned. The updated <code>pos</code> can be used to
1984      * indicate the starting point for the next call to this method.
1985      * If an error occurs, then the index of <code>pos</code> is not
1986      * changed, the error index of <code>pos</code> is set to the index of
1987      * the character where the error occurred, and null is returned.
1988      * <p>
1989      * The subclass returned depends on the value of {@link #isParseBigDecimal}
1990      * as well as on the string being parsed.
1991      * <ul>
1992      *   <li>If <code>isParseBigDecimal()</code> is false (the default),
1993      *       most integer values are returned as <code>Long</code>
1994      *       objects, no matter how they are written: <code>"17"</code> and
1995      *       <code>"17.000"</code> both parse to <code>Long(17)</code>.
1996      *       Values that cannot fit into a <code>Long</code> are returned as
1997      *       <code>Double</code>s. This includes values with a fractional part,
1998      *       infinite values, <code>NaN</code>, and the value -0.0.
1999      *       <code>DecimalFormat</code> does <em>not</em> decide whether to
2000      *       return a <code>Double</code> or a <code>Long</code> based on the
2001      *       presence of a decimal separator in the source string. Doing so
2002      *       would prevent integers that overflow the mantissa of a double,
2003      *       such as <code>"-9,223,372,036,854,775,808.00"</code>, from being
2004      *       parsed accurately.
2005      *       <p>
2006      *       Callers may use the <code>Number</code> methods
2007      *       <code>doubleValue</code>, <code>longValue</code>, etc., to obtain
2008      *       the type they want.
2009      *   <li>If <code>isParseBigDecimal()</code> is true, values are returned
2010      *       as <code>BigDecimal</code> objects. The values are the ones
2011      *       constructed by {@link java.math.BigDecimal#BigDecimal(String)}
2012      *       for corresponding strings in locale-independent format. The
2013      *       special cases negative and positive infinity and NaN are returned
2014      *       as <code>Double</code> instances holding the values of the
2015      *       corresponding <code>Double</code> constants.
2016      * </ul>
2017      * <p>
2018      * <code>DecimalFormat</code> parses all Unicode characters that represent
2019      * decimal digits, as defined by <code>Character.digit()</code>. In
2020      * addition, <code>DecimalFormat</code> also recognizes as digits the ten
2021      * consecutive characters starting with the localized zero digit defined in
2022      * the <code>DecimalFormatSymbols</code> object.
2023      *
2024      * @param text the string to be parsed
2025      * @param pos  A <code>ParsePosition</code> object with index and error
2026      *             index information as described above.
2027      * @return     the parsed value, or <code>null</code> if the parse fails
2028      * @exception  NullPointerException if <code>text</code> or
2029      *             <code>pos</code> is null.
2030      */
2031     @Override
2032     public Number parse(String text, ParsePosition pos) {
2033         // special case NaN
2034         if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) {
2035             pos.index = pos.index + symbols.getNaN().length();
2036             return Double.valueOf(Double.NaN);
2037         }
2038 
2039         boolean[] status = new boolean[STATUS_LENGTH];
2040         if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) {
2041             return null;
2042         }
2043 
2044         // special case INFINITY
2045         if (status[STATUS_INFINITE]) {
2046             if (status[STATUS_POSITIVE] == (multiplier >= 0)) {
2047                 return Double.valueOf(Double.POSITIVE_INFINITY);
2048             } else {
2049                 return Double.valueOf(Double.NEGATIVE_INFINITY);
2050             }
2051         }
2052 
2053         if (multiplier == 0) {
2054             if (digitList.isZero()) {
2055                 return Double.valueOf(Double.NaN);
2056             } else if (status[STATUS_POSITIVE]) {
2057                 return Double.valueOf(Double.POSITIVE_INFINITY);
2058             } else {
2059                 return Double.valueOf(Double.NEGATIVE_INFINITY);
2060             }
2061         }
2062 
2063         if (isParseBigDecimal()) {
2064             BigDecimal bigDecimalResult = digitList.getBigDecimal();
2065 
2066             if (multiplier != 1) {
2067                 try {
2068                     bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier());
2069                 }
2070                 catch (ArithmeticException e) {  // non-terminating decimal expansion
2071                     bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier(), roundingMode);
2072                 }
2073             }
2074 
2075             if (!status[STATUS_POSITIVE]) {
2076                 bigDecimalResult = bigDecimalResult.negate();
2077             }
2078             return bigDecimalResult;
2079         } else {
2080             boolean gotDouble = true;
2081             boolean gotLongMinimum = false;
2082             double  doubleResult = 0.0;
2083             long    longResult = 0;
2084 
2085             // Finally, have DigitList parse the digits into a value.
2086             if (digitList.fitsIntoLong(status[STATUS_POSITIVE], isParseIntegerOnly())) {
2087                 gotDouble = false;
2088                 longResult = digitList.getLong();
2089                 if (longResult < 0) {  // got Long.MIN_VALUE
2090                     gotLongMinimum = true;
2091                 }
2092             } else {
2093                 doubleResult = digitList.getDouble();
2094             }
2095 
2096             // Divide by multiplier. We have to be careful here not to do
2097             // unneeded conversions between double and long.
2098             if (multiplier != 1) {
2099                 if (gotDouble) {
2100                     doubleResult /= multiplier;
2101                 } else {
2102                     // Avoid converting to double if we can
2103                     if (longResult % multiplier == 0) {
2104                         longResult /= multiplier;
2105                     } else {
2106                         doubleResult = ((double)longResult) / multiplier;
2107                         gotDouble = true;
2108                     }
2109                 }
2110             }
2111 
2112             if (!status[STATUS_POSITIVE] && !gotLongMinimum) {
2113                 doubleResult = -doubleResult;
2114                 longResult = -longResult;
2115             }
2116 
2117             // At this point, if we divided the result by the multiplier, the
2118             // result may fit into a long.  We check for this case and return
2119             // a long if possible.
2120             // We must do this AFTER applying the negative (if appropriate)
2121             // in order to handle the case of LONG_MIN; otherwise, if we do
2122             // this with a positive value -LONG_MIN, the double is > 0, but
2123             // the long is < 0. We also must retain a double in the case of
2124             // -0.0, which will compare as == to a long 0 cast to a double
2125             // (bug 4162852).
2126             if (multiplier != 1 && gotDouble) {
2127                 longResult = (long)doubleResult;
2128                 gotDouble = ((doubleResult != (double)longResult) ||
2129                             (doubleResult == 0.0 && 1/doubleResult < 0.0)) &&
2130                             !isParseIntegerOnly();
2131             }
2132 
2133             // cast inside of ?: because of binary numeric promotion, JLS 15.25
2134             return gotDouble ? (Number)doubleResult : (Number)longResult;
2135         }
2136     }
2137 
2138     /**
2139      * Return a BigInteger multiplier.
2140      */
2141     private BigInteger getBigIntegerMultiplier() {
2142         if (bigIntegerMultiplier == null) {
2143             bigIntegerMultiplier = BigInteger.valueOf(multiplier);
2144         }
2145         return bigIntegerMultiplier;
2146     }
2147     private transient BigInteger bigIntegerMultiplier;
2148 
2149     /**
2150      * Return a BigDecimal multiplier.
2151      */
2152     private BigDecimal getBigDecimalMultiplier() {
2153         if (bigDecimalMultiplier == null) {
2154             bigDecimalMultiplier = new BigDecimal(multiplier);
2155         }
2156         return bigDecimalMultiplier;
2157     }
2158     private transient BigDecimal bigDecimalMultiplier;
2159 
2160     private static final int STATUS_INFINITE = 0;
2161     private static final int STATUS_POSITIVE = 1;
2162     private static final int STATUS_LENGTH   = 2;
2163 
2164     /**
2165      * Parse the given text into a number.  The text is parsed beginning at
2166      * parsePosition, until an unparseable character is seen.
2167      * @param text The string to parse.
2168      * @param parsePosition The position at which to being parsing.  Upon
2169      * return, the first unparseable character.
2170      * @param digits The DigitList to set to the parsed value.
2171      * @param isExponent If true, parse an exponent.  This means no
2172      * infinite values and integer only.
2173      * @param status Upon return contains boolean status flags indicating
2174      * whether the value was infinite and whether it was positive.
2175      */
2176     private final boolean subparse(String text, ParsePosition parsePosition,
2177                    String positivePrefix, String negativePrefix,
2178                    DigitList digits, boolean isExponent,
2179                    boolean status[]) {
2180         int position = parsePosition.index;
2181         int oldStart = parsePosition.index;
2182         int backup;
2183         boolean gotPositive, gotNegative;
2184 
2185         // check for positivePrefix; take longest
2186         gotPositive = text.regionMatches(position, positivePrefix, 0,
2187                                          positivePrefix.length());
2188         gotNegative = text.regionMatches(position, negativePrefix, 0,
2189                                          negativePrefix.length());
2190 
2191         if (gotPositive && gotNegative) {
2192             if (positivePrefix.length() > negativePrefix.length()) {
2193                 gotNegative = false;
2194             } else if (positivePrefix.length() < negativePrefix.length()) {
2195                 gotPositive = false;
2196             }
2197         }
2198 
2199         if (gotPositive) {
2200             position += positivePrefix.length();
2201         } else if (gotNegative) {
2202             position += negativePrefix.length();
2203         } else {
2204             parsePosition.errorIndex = position;
2205             return false;
2206         }
2207 
2208         // process digits or Inf, find decimal position
2209         status[STATUS_INFINITE] = false;
2210         if (!isExponent && text.regionMatches(position,symbols.getInfinity(),0,
2211                           symbols.getInfinity().length())) {
2212             position += symbols.getInfinity().length();
2213             status[STATUS_INFINITE] = true;
2214         } else {
2215             // We now have a string of digits, possibly with grouping symbols,
2216             // and decimal points.  We want to process these into a DigitList.
2217             // We don't want to put a bunch of leading zeros into the DigitList
2218             // though, so we keep track of the location of the decimal point,
2219             // put only significant digits into the DigitList, and adjust the
2220             // exponent as needed.
2221 
2222             digits.decimalAt = digits.count = 0;
2223             char zero = symbols.getZeroDigit();
2224             char decimal = isCurrencyFormat ?
2225                 symbols.getMonetaryDecimalSeparator() :
2226                 symbols.getDecimalSeparator();
2227             char grouping = symbols.getGroupingSeparator();
2228             String exponentString = symbols.getExponentSeparator();
2229             boolean sawDecimal = false;
2230             boolean sawExponent = false;
2231             boolean sawDigit = false;
2232             int exponent = 0; // Set to the exponent value, if any
2233 
2234             // We have to track digitCount ourselves, because digits.count will
2235             // pin when the maximum allowable digits is reached.
2236             int digitCount = 0;
2237 
2238             backup = -1;
2239             for (; position < text.length(); ++position) {
2240                 char ch = text.charAt(position);
2241 
2242                 /* We recognize all digit ranges, not only the Latin digit range
2243                  * '0'..'9'.  We do so by using the Character.digit() method,
2244                  * which converts a valid Unicode digit to the range 0..9.
2245                  *
2246                  * The character 'ch' may be a digit.  If so, place its value
2247                  * from 0 to 9 in 'digit'.  First try using the locale digit,
2248                  * which may or MAY NOT be a standard Unicode digit range.  If
2249                  * this fails, try using the standard Unicode digit ranges by
2250                  * calling Character.digit().  If this also fails, digit will
2251                  * have a value outside the range 0..9.
2252                  */
2253                 int digit = ch - zero;
2254                 if (digit < 0 || digit > 9) {
2255                     digit = Character.digit(ch, 10);
2256                 }
2257 
2258                 if (digit == 0) {
2259                     // Cancel out backup setting (see grouping handler below)
2260                     backup = -1; // Do this BEFORE continue statement below!!!
2261                     sawDigit = true;
2262 
2263                     // Handle leading zeros
2264                     if (digits.count == 0) {
2265                         // Ignore leading zeros in integer part of number.
2266                         if (!sawDecimal) {
2267                             continue;
2268                         }
2269 
2270                         // If we have seen the decimal, but no significant
2271                         // digits yet, then we account for leading zeros by
2272                         // decrementing the digits.decimalAt into negative
2273                         // values.
2274                         --digits.decimalAt;
2275                     } else {
2276                         ++digitCount;
2277                         digits.append((char)(digit + '0'));
2278                     }
2279                 } else if (digit > 0 && digit <= 9) { // [sic] digit==0 handled above
2280                     sawDigit = true;
2281                     ++digitCount;
2282                     digits.append((char)(digit + '0'));
2283 
2284                     // Cancel out backup setting (see grouping handler below)
2285                     backup = -1;
2286                 } else if (!isExponent && ch == decimal) {
2287                     // If we're only parsing integers, or if we ALREADY saw the
2288                     // decimal, then don't parse this one.
2289                     if (isParseIntegerOnly() || sawDecimal) {
2290                         break;
2291                     }
2292                     digits.decimalAt = digitCount; // Not digits.count!
2293                     sawDecimal = true;
2294                 } else if (!isExponent && ch == grouping && isGroupingUsed()) {
2295                     if (sawDecimal) {
2296                         break;
2297                     }
2298                     // Ignore grouping characters, if we are using them, but
2299                     // require that they be followed by a digit.  Otherwise
2300                     // we backup and reprocess them.
2301                     backup = position;
2302                 } else if (!isExponent && text.regionMatches(position, exponentString, 0, exponentString.length())
2303                              && !sawExponent) {
2304                     // Process the exponent by recursively calling this method.
2305                      ParsePosition pos = new ParsePosition(position + exponentString.length());
2306                     boolean[] stat = new boolean[STATUS_LENGTH];
2307                     DigitList exponentDigits = new DigitList();
2308 
2309                     if (subparse(text, pos, "", Character.toString(symbols.getMinusSign()), exponentDigits, true, stat) &&
2310                         exponentDigits.fitsIntoLong(stat[STATUS_POSITIVE], true)) {
2311                         position = pos.index; // Advance past the exponent
2312                         exponent = (int)exponentDigits.getLong();
2313                         if (!stat[STATUS_POSITIVE]) {
2314                             exponent = -exponent;
2315                         }
2316                         sawExponent = true;
2317                     }
2318                     break; // Whether we fail or succeed, we exit this loop
2319                 } else {
2320                     break;
2321                 }
2322             }
2323 
2324             if (backup != -1) {
2325                 position = backup;
2326             }
2327 
2328             // If there was no decimal point we have an integer
2329             if (!sawDecimal) {
2330                 digits.decimalAt = digitCount; // Not digits.count!
2331             }
2332 
2333             // Adjust for exponent, if any
2334             digits.decimalAt += exponent;
2335 
2336             // If none of the text string was recognized.  For example, parse
2337             // "x" with pattern "#0.00" (return index and error index both 0)
2338             // parse "$" with pattern "$#0.00". (return index 0 and error
2339             // index 1).
2340             if (!sawDigit && digitCount == 0) {
2341                 parsePosition.index = oldStart;
2342                 parsePosition.errorIndex = oldStart;
2343                 return false;
2344             }
2345         }
2346 
2347         // check for suffix
2348         if (!isExponent) {
2349             if (gotPositive) {
2350                 gotPositive = text.regionMatches(position,positiveSuffix,0,
2351                                                  positiveSuffix.length());
2352             }
2353             if (gotNegative) {
2354                 gotNegative = text.regionMatches(position,negativeSuffix,0,
2355                                                  negativeSuffix.length());
2356             }
2357 
2358         // if both match, take longest
2359         if (gotPositive && gotNegative) {
2360             if (positiveSuffix.length() > negativeSuffix.length()) {
2361                 gotNegative = false;
2362             } else if (positiveSuffix.length() < negativeSuffix.length()) {
2363                 gotPositive = false;
2364             }
2365         }
2366 
2367         // fail if neither or both
2368         if (gotPositive == gotNegative) {
2369             parsePosition.errorIndex = position;
2370             return false;
2371         }
2372 
2373         parsePosition.index = position +
2374             (gotPositive ? positiveSuffix.length() : negativeSuffix.length()); // mark success!
2375         } else {
2376             parsePosition.index = position;
2377         }
2378 
2379         status[STATUS_POSITIVE] = gotPositive;
2380         if (parsePosition.index == oldStart) {
2381             parsePosition.errorIndex = position;
2382             return false;
2383         }
2384         return true;
2385     }
2386 
2387     /**
2388      * Returns a copy of the decimal format symbols, which is generally not
2389      * changed by the programmer or user.
2390      * @return a copy of the desired DecimalFormatSymbols
2391      * @see java.text.DecimalFormatSymbols
2392      */
2393     public DecimalFormatSymbols getDecimalFormatSymbols() {
2394         try {
2395             // don't allow multiple references
2396             return (DecimalFormatSymbols) symbols.clone();
2397         } catch (Exception foo) {
2398             return null; // should never happen
2399         }
2400     }
2401 
2402 
2403     /**
2404      * Sets the decimal format symbols, which is generally not changed
2405      * by the programmer or user.
2406      * @param newSymbols desired DecimalFormatSymbols
2407      * @see java.text.DecimalFormatSymbols
2408      */
2409     public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) {
2410         try {
2411             // don't allow multiple references
2412             symbols = (DecimalFormatSymbols) newSymbols.clone();
2413             expandAffixes();
2414             fastPathCheckNeeded = true;
2415         } catch (Exception foo) {
2416             // should never happen
2417         }
2418     }
2419 
2420     /**
2421      * Get the positive prefix.
2422      * <P>Examples: +123, $123, sFr123
2423      *
2424      * @return the positive prefix
2425      */
2426     public String getPositivePrefix () {
2427         return positivePrefix;
2428     }
2429 
2430     /**
2431      * Set the positive prefix.
2432      * <P>Examples: +123, $123, sFr123
2433      *
2434      * @param newValue the new positive prefix
2435      */
2436     public void setPositivePrefix (String newValue) {
2437         positivePrefix = newValue;
2438         posPrefixPattern = null;
2439         positivePrefixFieldPositions = null;
2440         fastPathCheckNeeded = true;
2441     }
2442 
2443     /**
2444      * Returns the FieldPositions of the fields in the prefix used for
2445      * positive numbers. This is not used if the user has explicitly set
2446      * a positive prefix via <code>setPositivePrefix</code>. This is
2447      * lazily created.
2448      *
2449      * @return FieldPositions in positive prefix
2450      */
2451     private FieldPosition[] getPositivePrefixFieldPositions() {
2452         if (positivePrefixFieldPositions == null) {
2453             if (posPrefixPattern != null) {
2454                 positivePrefixFieldPositions = expandAffix(posPrefixPattern);
2455             } else {
2456                 positivePrefixFieldPositions = EmptyFieldPositionArray;
2457             }
2458         }
2459         return positivePrefixFieldPositions;
2460     }
2461 
2462     /**
2463      * Get the negative prefix.
2464      * <P>Examples: -123, ($123) (with negative suffix), sFr-123
2465      *
2466      * @return the negative prefix
2467      */
2468     public String getNegativePrefix () {
2469         return negativePrefix;
2470     }
2471 
2472     /**
2473      * Set the negative prefix.
2474      * <P>Examples: -123, ($123) (with negative suffix), sFr-123
2475      *
2476      * @param newValue the new negative prefix
2477      */
2478     public void setNegativePrefix (String newValue) {
2479         negativePrefix = newValue;
2480         negPrefixPattern = null;
2481         fastPathCheckNeeded = true;
2482     }
2483 
2484     /**
2485      * Returns the FieldPositions of the fields in the prefix used for
2486      * negative numbers. This is not used if the user has explicitly set
2487      * a negative prefix via <code>setNegativePrefix</code>. This is
2488      * lazily created.
2489      *
2490      * @return FieldPositions in positive prefix
2491      */
2492     private FieldPosition[] getNegativePrefixFieldPositions() {
2493         if (negativePrefixFieldPositions == null) {
2494             if (negPrefixPattern != null) {
2495                 negativePrefixFieldPositions = expandAffix(negPrefixPattern);
2496             } else {
2497                 negativePrefixFieldPositions = EmptyFieldPositionArray;
2498             }
2499         }
2500         return negativePrefixFieldPositions;
2501     }
2502 
2503     /**
2504      * Get the positive suffix.
2505      * <P>Example: 123%
2506      *
2507      * @return the positive suffix
2508      */
2509     public String getPositiveSuffix () {
2510         return positiveSuffix;
2511     }
2512 
2513     /**
2514      * Set the positive suffix.
2515      * <P>Example: 123%
2516      *
2517      * @param newValue the new positive suffix
2518      */
2519     public void setPositiveSuffix (String newValue) {
2520         positiveSuffix = newValue;
2521         posSuffixPattern = null;
2522         fastPathCheckNeeded = true;
2523     }
2524 
2525     /**
2526      * Returns the FieldPositions of the fields in the suffix used for
2527      * positive numbers. This is not used if the user has explicitly set
2528      * a positive suffix via <code>setPositiveSuffix</code>. This is
2529      * lazily created.
2530      *
2531      * @return FieldPositions in positive prefix
2532      */
2533     private FieldPosition[] getPositiveSuffixFieldPositions() {
2534         if (positiveSuffixFieldPositions == null) {
2535             if (posSuffixPattern != null) {
2536                 positiveSuffixFieldPositions = expandAffix(posSuffixPattern);
2537             } else {
2538                 positiveSuffixFieldPositions = EmptyFieldPositionArray;
2539             }
2540         }
2541         return positiveSuffixFieldPositions;
2542     }
2543 
2544     /**
2545      * Get the negative suffix.
2546      * <P>Examples: -123%, ($123) (with positive suffixes)
2547      *
2548      * @return the negative suffix
2549      */
2550     public String getNegativeSuffix () {
2551         return negativeSuffix;
2552     }
2553 
2554     /**
2555      * Set the negative suffix.
2556      * <P>Examples: 123%
2557      *
2558      * @param newValue the new negative suffix
2559      */
2560     public void setNegativeSuffix (String newValue) {
2561         negativeSuffix = newValue;
2562         negSuffixPattern = null;
2563         fastPathCheckNeeded = true;
2564     }
2565 
2566     /**
2567      * Returns the FieldPositions of the fields in the suffix used for
2568      * negative numbers. This is not used if the user has explicitly set
2569      * a negative suffix via <code>setNegativeSuffix</code>. This is
2570      * lazily created.
2571      *
2572      * @return FieldPositions in positive prefix
2573      */
2574     private FieldPosition[] getNegativeSuffixFieldPositions() {
2575         if (negativeSuffixFieldPositions == null) {
2576             if (negSuffixPattern != null) {
2577                 negativeSuffixFieldPositions = expandAffix(negSuffixPattern);
2578             } else {
2579                 negativeSuffixFieldPositions = EmptyFieldPositionArray;
2580             }
2581         }
2582         return negativeSuffixFieldPositions;
2583     }
2584 
2585     /**
2586      * Gets the multiplier for use in percent, per mille, and similar
2587      * formats.
2588      *
2589      * @return the multiplier
2590      * @see #setMultiplier(int)
2591      */
2592     public int getMultiplier () {
2593         return multiplier;
2594     }
2595 
2596     /**
2597      * Sets the multiplier for use in percent, per mille, and similar
2598      * formats.
2599      * For a percent format, set the multiplier to 100 and the suffixes to
2600      * have '%' (for Arabic, use the Arabic percent sign).
2601      * For a per mille format, set the multiplier to 1000 and the suffixes to
2602      * have '\u2030'.
2603      *
2604      * <P>Example: with multiplier 100, 1.23 is formatted as "123", and
2605      * "123" is parsed into 1.23.
2606      *
2607      * @param newValue the new multiplier
2608      * @see #getMultiplier
2609      */
2610     public void setMultiplier (int newValue) {
2611         multiplier = newValue;
2612         bigDecimalMultiplier = null;
2613         bigIntegerMultiplier = null;
2614         fastPathCheckNeeded = true;
2615     }
2616 
2617     /**
2618      * {@inheritDoc}
2619      */
2620     @Override
2621     public void setGroupingUsed(boolean newValue) {
2622         super.setGroupingUsed(newValue);
2623         fastPathCheckNeeded = true;
2624     }
2625 
2626     /**
2627      * Return the grouping size. Grouping size is the number of digits between
2628      * grouping separators in the integer portion of a number.  For example,
2629      * in the number "123,456.78", the grouping size is 3.
2630      *
2631      * @return the grouping size
2632      * @see #setGroupingSize
2633      * @see java.text.NumberFormat#isGroupingUsed
2634      * @see java.text.DecimalFormatSymbols#getGroupingSeparator
2635      */
2636     public int getGroupingSize () {
2637         return groupingSize;
2638     }
2639 
2640     /**
2641      * Set the grouping size. Grouping size is the number of digits between
2642      * grouping separators in the integer portion of a number.  For example,
2643      * in the number "123,456.78", the grouping size is 3.
2644      * <br>
2645      * The value passed in is converted to a byte, which may lose information.
2646      *
2647      * @param newValue the new grouping size
2648      * @see #getGroupingSize
2649      * @see java.text.NumberFormat#setGroupingUsed
2650      * @see java.text.DecimalFormatSymbols#setGroupingSeparator
2651      */
2652     public void setGroupingSize (int newValue) {
2653         groupingSize = (byte)newValue;
2654         fastPathCheckNeeded = true;
2655     }
2656 
2657     /**
2658      * Allows you to get the behavior of the decimal separator with integers.
2659      * (The decimal separator will always appear with decimals.)
2660      * <P>Example: Decimal ON: 12345 &rarr; 12345.; OFF: 12345 &rarr; 12345
2661      *
2662      * @return {@code true} if the decimal separator is always shown;
2663      *         {@code false} otherwise
2664      */
2665     public boolean isDecimalSeparatorAlwaysShown() {
2666         return decimalSeparatorAlwaysShown;
2667     }
2668 
2669     /**
2670      * Allows you to set the behavior of the decimal separator with integers.
2671      * (The decimal separator will always appear with decimals.)
2672      * <P>Example: Decimal ON: 12345 &rarr; 12345.; OFF: 12345 &rarr; 12345
2673      *
2674      * @param newValue {@code true} if the decimal separator is always shown;
2675      *                 {@code false} otherwise
2676      */
2677     public void setDecimalSeparatorAlwaysShown(boolean newValue) {
2678         decimalSeparatorAlwaysShown = newValue;
2679         fastPathCheckNeeded = true;
2680     }
2681 
2682     /**
2683      * Returns whether the {@link #parse(java.lang.String, java.text.ParsePosition)}
2684      * method returns <code>BigDecimal</code>. The default value is false.
2685      *
2686      * @return {@code true} if the parse method returns BigDecimal;
2687      *         {@code false} otherwise
2688      * @see #setParseBigDecimal
2689      * @since 1.5
2690      */
2691     public boolean isParseBigDecimal() {
2692         return parseBigDecimal;
2693     }
2694 
2695     /**
2696      * Sets whether the {@link #parse(java.lang.String, java.text.ParsePosition)}
2697      * method returns <code>BigDecimal</code>.
2698      *
2699      * @param newValue {@code true} if the parse method returns BigDecimal;
2700      *                 {@code false} otherwise
2701      * @see #isParseBigDecimal
2702      * @since 1.5
2703      */
2704     public void setParseBigDecimal(boolean newValue) {
2705         parseBigDecimal = newValue;
2706     }
2707 
2708     /**
2709      * Standard override; no change in semantics.
2710      */
2711     @Override
2712     public Object clone() {
2713         DecimalFormat other = (DecimalFormat) super.clone();
2714         other.symbols = (DecimalFormatSymbols) symbols.clone();
2715         other.digitList = (DigitList) digitList.clone();
2716 
2717         // Fast-path is almost stateless algorithm. The only logical state is the
2718         // isFastPath flag. In addition fastPathCheckNeeded is a sentinel flag
2719         // that forces recalculation of all fast-path fields when set to true.
2720         //
2721         // There is thus no need to clone all the fast-path fields.
2722         // We just only need to set fastPathCheckNeeded to true when cloning,
2723         // and init fastPathData to null as if it were a truly new instance.
2724         // Every fast-path field will be recalculated (only once) at next usage of
2725         // fast-path algorithm.
2726         other.fastPathCheckNeeded = true;
2727         other.isFastPath = false;
2728         other.fastPathData = null;
2729 
2730         return other;
2731     }
2732 
2733     /**
2734      * Overrides equals
2735      */
2736     @Override
2737     public boolean equals(Object obj)
2738     {
2739         if (obj == null)
2740             return false;
2741         if (!super.equals(obj))
2742             return false; // super does class check
2743         DecimalFormat other = (DecimalFormat) obj;
2744         return ((posPrefixPattern == other.posPrefixPattern &&
2745                  positivePrefix.equals(other.positivePrefix))
2746                 || (posPrefixPattern != null &&
2747                     posPrefixPattern.equals(other.posPrefixPattern)))
2748             && ((posSuffixPattern == other.posSuffixPattern &&
2749                  positiveSuffix.equals(other.positiveSuffix))
2750                 || (posSuffixPattern != null &&
2751                     posSuffixPattern.equals(other.posSuffixPattern)))
2752             && ((negPrefixPattern == other.negPrefixPattern &&
2753                  negativePrefix.equals(other.negativePrefix))
2754                 || (negPrefixPattern != null &&
2755                     negPrefixPattern.equals(other.negPrefixPattern)))
2756             && ((negSuffixPattern == other.negSuffixPattern &&
2757                  negativeSuffix.equals(other.negativeSuffix))
2758                 || (negSuffixPattern != null &&
2759                     negSuffixPattern.equals(other.negSuffixPattern)))
2760             && multiplier == other.multiplier
2761             && groupingSize == other.groupingSize
2762             && decimalSeparatorAlwaysShown == other.decimalSeparatorAlwaysShown
2763             && parseBigDecimal == other.parseBigDecimal
2764             && useExponentialNotation == other.useExponentialNotation
2765             && (!useExponentialNotation ||
2766                 minExponentDigits == other.minExponentDigits)
2767             && maximumIntegerDigits == other.maximumIntegerDigits
2768             && minimumIntegerDigits == other.minimumIntegerDigits
2769             && maximumFractionDigits == other.maximumFractionDigits
2770             && minimumFractionDigits == other.minimumFractionDigits
2771             && roundingMode == other.roundingMode
2772             && symbols.equals(other.symbols);
2773     }
2774 
2775     /**
2776      * Overrides hashCode
2777      */
2778     @Override
2779     public int hashCode() {
2780         return super.hashCode() * 37 + positivePrefix.hashCode();
2781         // just enough fields for a reasonable distribution
2782     }
2783 
2784     /**
2785      * Synthesizes a pattern string that represents the current state
2786      * of this Format object.
2787      *
2788      * @return a pattern string
2789      * @see #applyPattern
2790      */
2791     public String toPattern() {
2792         return toPattern( false );
2793     }
2794 
2795     /**
2796      * Synthesizes a localized pattern string that represents the current
2797      * state of this Format object.
2798      *
2799      * @return a localized pattern string
2800      * @see #applyPattern
2801      */
2802     public String toLocalizedPattern() {
2803         return toPattern( true );
2804     }
2805 
2806     /**
2807      * Expand the affix pattern strings into the expanded affix strings.  If any
2808      * affix pattern string is null, do not expand it.  This method should be
2809      * called any time the symbols or the affix patterns change in order to keep
2810      * the expanded affix strings up to date.
2811      */
2812     private void expandAffixes() {
2813         // Reuse one StringBuffer for better performance
2814         StringBuffer buffer = new StringBuffer();
2815         if (posPrefixPattern != null) {
2816             positivePrefix = expandAffix(posPrefixPattern, buffer);
2817             positivePrefixFieldPositions = null;
2818         }
2819         if (posSuffixPattern != null) {
2820             positiveSuffix = expandAffix(posSuffixPattern, buffer);
2821             positiveSuffixFieldPositions = null;
2822         }
2823         if (negPrefixPattern != null) {
2824             negativePrefix = expandAffix(negPrefixPattern, buffer);
2825             negativePrefixFieldPositions = null;
2826         }
2827         if (negSuffixPattern != null) {
2828             negativeSuffix = expandAffix(negSuffixPattern, buffer);
2829             negativeSuffixFieldPositions = null;
2830         }
2831     }
2832 
2833     /**
2834      * Expand an affix pattern into an affix string.  All characters in the
2835      * pattern are literal unless prefixed by QUOTE.  The following characters
2836      * after QUOTE are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
2837      * PATTERN_MINUS, and CURRENCY_SIGN.  If CURRENCY_SIGN is doubled (QUOTE +
2838      * CURRENCY_SIGN + CURRENCY_SIGN), it is interpreted as an ISO 4217
2839      * currency code.  Any other character after a QUOTE represents itself.
2840      * QUOTE must be followed by another character; QUOTE may not occur by
2841      * itself at the end of the pattern.
2842      *
2843      * @param pattern the non-null, possibly empty pattern
2844      * @param buffer a scratch StringBuffer; its contents will be lost
2845      * @return the expanded equivalent of pattern
2846      */
2847     private String expandAffix(String pattern, StringBuffer buffer) {
2848         buffer.setLength(0);
2849         for (int i=0; i<pattern.length(); ) {
2850             char c = pattern.charAt(i++);
2851             if (c == QUOTE) {
2852                 c = pattern.charAt(i++);
2853                 switch (c) {
2854                 case CURRENCY_SIGN:
2855                     if (i<pattern.length() &&
2856                         pattern.charAt(i) == CURRENCY_SIGN) {
2857                         ++i;
2858                         buffer.append(symbols.getInternationalCurrencySymbol());
2859                     } else {
2860                         buffer.append(symbols.getCurrencySymbol());
2861                     }
2862                     continue;
2863                 case PATTERN_PERCENT:
2864                     c = symbols.getPercent();
2865                     break;
2866                 case PATTERN_PER_MILLE:
2867                     c = symbols.getPerMill();
2868                     break;
2869                 case PATTERN_MINUS:
2870                     c = symbols.getMinusSign();
2871                     break;
2872                 }
2873             }
2874             buffer.append(c);
2875         }
2876         return buffer.toString();
2877     }
2878 
2879     /**
2880      * Expand an affix pattern into an array of FieldPositions describing
2881      * how the pattern would be expanded.
2882      * All characters in the
2883      * pattern are literal unless prefixed by QUOTE.  The following characters
2884      * after QUOTE are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
2885      * PATTERN_MINUS, and CURRENCY_SIGN.  If CURRENCY_SIGN is doubled (QUOTE +
2886      * CURRENCY_SIGN + CURRENCY_SIGN), it is interpreted as an ISO 4217
2887      * currency code.  Any other character after a QUOTE represents itself.
2888      * QUOTE must be followed by another character; QUOTE may not occur by
2889      * itself at the end of the pattern.
2890      *
2891      * @param pattern the non-null, possibly empty pattern
2892      * @return FieldPosition array of the resulting fields.
2893      */
2894     private FieldPosition[] expandAffix(String pattern) {
2895         ArrayList<FieldPosition> positions = null;
2896         int stringIndex = 0;
2897         for (int i=0; i<pattern.length(); ) {
2898             char c = pattern.charAt(i++);
2899             if (c == QUOTE) {
2900                 int field = -1;
2901                 Format.Field fieldID = null;
2902                 c = pattern.charAt(i++);
2903                 switch (c) {
2904                 case CURRENCY_SIGN:
2905                     String string;
2906                     if (i<pattern.length() &&
2907                         pattern.charAt(i) == CURRENCY_SIGN) {
2908                         ++i;
2909                         string = symbols.getInternationalCurrencySymbol();
2910                     } else {
2911                         string = symbols.getCurrencySymbol();
2912                     }
2913                     if (string.length() > 0) {
2914                         if (positions == null) {
2915                             positions = new ArrayList<>(2);
2916                         }
2917                         FieldPosition fp = new FieldPosition(Field.CURRENCY);
2918                         fp.setBeginIndex(stringIndex);
2919                         fp.setEndIndex(stringIndex + string.length());
2920                         positions.add(fp);
2921                         stringIndex += string.length();
2922                     }
2923                     continue;
2924                 case PATTERN_PERCENT:
2925                     c = symbols.getPercent();
2926                     field = -1;
2927                     fieldID = Field.PERCENT;
2928                     break;
2929                 case PATTERN_PER_MILLE:
2930                     c = symbols.getPerMill();
2931                     field = -1;
2932                     fieldID = Field.PERMILLE;
2933                     break;
2934                 case PATTERN_MINUS:
2935                     c = symbols.getMinusSign();
2936                     field = -1;
2937                     fieldID = Field.SIGN;
2938                     break;
2939                 }
2940                 if (fieldID != null) {
2941                     if (positions == null) {
2942                         positions = new ArrayList<>(2);
2943                     }
2944                     FieldPosition fp = new FieldPosition(fieldID, field);
2945                     fp.setBeginIndex(stringIndex);
2946                     fp.setEndIndex(stringIndex + 1);
2947                     positions.add(fp);
2948                 }
2949             }
2950             stringIndex++;
2951         }
2952         if (positions != null) {
2953             return positions.toArray(EmptyFieldPositionArray);
2954         }
2955         return EmptyFieldPositionArray;
2956     }
2957 
2958     /**
2959      * Appends an affix pattern to the given StringBuffer, quoting special
2960      * characters as needed.  Uses the internal affix pattern, if that exists,
2961      * or the literal affix, if the internal affix pattern is null.  The
2962      * appended string will generate the same affix pattern (or literal affix)
2963      * when passed to toPattern().
2964      *
2965      * @param buffer the affix string is appended to this
2966      * @param affixPattern a pattern such as posPrefixPattern; may be null
2967      * @param expAffix a corresponding expanded affix, such as positivePrefix.
2968      * Ignored unless affixPattern is null.  If affixPattern is null, then
2969      * expAffix is appended as a literal affix.
2970      * @param localized true if the appended pattern should contain localized
2971      * pattern characters; otherwise, non-localized pattern chars are appended
2972      */
2973     private void appendAffix(StringBuffer buffer, String affixPattern,
2974                              String expAffix, boolean localized) {
2975         if (affixPattern == null) {
2976             appendAffix(buffer, expAffix, localized);
2977         } else {
2978             int i;
2979             for (int pos=0; pos<affixPattern.length(); pos=i) {
2980                 i = affixPattern.indexOf(QUOTE, pos);
2981                 if (i < 0) {
2982                     appendAffix(buffer, affixPattern.substring(pos), localized);
2983                     break;
2984                 }
2985                 if (i > pos) {
2986                     appendAffix(buffer, affixPattern.substring(pos, i), localized);
2987                 }
2988                 char c = affixPattern.charAt(++i);
2989                 ++i;
2990                 if (c == QUOTE) {
2991                     buffer.append(c);
2992                     // Fall through and append another QUOTE below
2993                 } else if (c == CURRENCY_SIGN &&
2994                            i<affixPattern.length() &&
2995                            affixPattern.charAt(i) == CURRENCY_SIGN) {
2996                     ++i;
2997                     buffer.append(c);
2998                     // Fall through and append another CURRENCY_SIGN below
2999                 } else if (localized) {
3000                     switch (c) {
3001                     case PATTERN_PERCENT:
3002                         c = symbols.getPercent();
3003                         break;
3004                     case PATTERN_PER_MILLE:
3005                         c = symbols.getPerMill();
3006                         break;
3007                     case PATTERN_MINUS:
3008                         c = symbols.getMinusSign();
3009                         break;
3010                     }
3011                 }
3012                 buffer.append(c);
3013             }
3014         }
3015     }
3016 
3017     /**
3018      * Append an affix to the given StringBuffer, using quotes if
3019      * there are special characters.  Single quotes themselves must be
3020      * escaped in either case.
3021      */
3022     private void appendAffix(StringBuffer buffer, String affix, boolean localized) {
3023         boolean needQuote;
3024         if (localized) {
3025             needQuote = affix.indexOf(symbols.getZeroDigit()) >= 0
3026                 || affix.indexOf(symbols.getGroupingSeparator()) >= 0
3027                 || affix.indexOf(symbols.getDecimalSeparator()) >= 0
3028                 || affix.indexOf(symbols.getPercent()) >= 0
3029                 || affix.indexOf(symbols.getPerMill()) >= 0
3030                 || affix.indexOf(symbols.getDigit()) >= 0
3031                 || affix.indexOf(symbols.getPatternSeparator()) >= 0
3032                 || affix.indexOf(symbols.getMinusSign()) >= 0
3033                 || affix.indexOf(CURRENCY_SIGN) >= 0;
3034         } else {
3035             needQuote = affix.indexOf(PATTERN_ZERO_DIGIT) >= 0
3036                 || affix.indexOf(PATTERN_GROUPING_SEPARATOR) >= 0
3037                 || affix.indexOf(PATTERN_DECIMAL_SEPARATOR) >= 0
3038                 || affix.indexOf(PATTERN_PERCENT) >= 0
3039                 || affix.indexOf(PATTERN_PER_MILLE) >= 0
3040                 || affix.indexOf(PATTERN_DIGIT) >= 0
3041                 || affix.indexOf(PATTERN_SEPARATOR) >= 0
3042                 || affix.indexOf(PATTERN_MINUS) >= 0
3043                 || affix.indexOf(CURRENCY_SIGN) >= 0;
3044         }
3045         if (needQuote) buffer.append('\'');
3046         if (affix.indexOf('\'') < 0) buffer.append(affix);
3047         else {
3048             for (int j=0; j<affix.length(); ++j) {
3049                 char c = affix.charAt(j);
3050                 buffer.append(c);
3051                 if (c == '\'') buffer.append(c);
3052             }
3053         }
3054         if (needQuote) buffer.append('\'');
3055     }
3056 
3057     /**
3058      * Does the real work of generating a pattern.  */
3059     private String toPattern(boolean localized) {
3060         StringBuffer result = new StringBuffer();
3061         for (int j = 1; j >= 0; --j) {
3062             if (j == 1)
3063                 appendAffix(result, posPrefixPattern, positivePrefix, localized);
3064             else appendAffix(result, negPrefixPattern, negativePrefix, localized);
3065             int i;
3066             int digitCount = useExponentialNotation
3067                         ? getMaximumIntegerDigits()
3068                         : Math.max(groupingSize, getMinimumIntegerDigits())+1;
3069             for (i = digitCount; i > 0; --i) {
3070                 if (i != digitCount && isGroupingUsed() && groupingSize != 0 &&
3071                     i % groupingSize == 0) {
3072                     result.append(localized ? symbols.getGroupingSeparator() :
3073                                   PATTERN_GROUPING_SEPARATOR);
3074                 }
3075                 result.append(i <= getMinimumIntegerDigits()
3076                     ? (localized ? symbols.getZeroDigit() : PATTERN_ZERO_DIGIT)
3077                     : (localized ? symbols.getDigit() : PATTERN_DIGIT));
3078             }
3079             if (getMaximumFractionDigits() > 0 || decimalSeparatorAlwaysShown)
3080                 result.append(localized ? symbols.getDecimalSeparator() :
3081                               PATTERN_DECIMAL_SEPARATOR);
3082             for (i = 0; i < getMaximumFractionDigits(); ++i) {
3083                 if (i < getMinimumFractionDigits()) {
3084                     result.append(localized ? symbols.getZeroDigit() :
3085                                   PATTERN_ZERO_DIGIT);
3086                 } else {
3087                     result.append(localized ? symbols.getDigit() :
3088                                   PATTERN_DIGIT);
3089                 }
3090             }
3091         if (useExponentialNotation)
3092         {
3093             result.append(localized ? symbols.getExponentSeparator() :
3094                   PATTERN_EXPONENT);
3095         for (i=0; i<minExponentDigits; ++i)
3096                     result.append(localized ? symbols.getZeroDigit() :
3097                                   PATTERN_ZERO_DIGIT);
3098         }
3099             if (j == 1) {
3100                 appendAffix(result, posSuffixPattern, positiveSuffix, localized);
3101                 if ((negSuffixPattern == posSuffixPattern && // n == p == null
3102                      negativeSuffix.equals(positiveSuffix))
3103                     || (negSuffixPattern != null &&
3104                         negSuffixPattern.equals(posSuffixPattern))) {
3105                     if ((negPrefixPattern != null && posPrefixPattern != null &&
3106                          negPrefixPattern.equals("'-" + posPrefixPattern)) ||
3107                         (negPrefixPattern == posPrefixPattern && // n == p == null
3108                          negativePrefix.equals(symbols.getMinusSign() + positivePrefix)))
3109                         break;
3110                 }
3111                 result.append(localized ? symbols.getPatternSeparator() :
3112                               PATTERN_SEPARATOR);
3113             } else appendAffix(result, negSuffixPattern, negativeSuffix, localized);
3114         }
3115         return result.toString();
3116     }
3117 
3118     /**
3119      * Apply the given pattern to this Format object.  A pattern is a
3120      * short-hand specification for the various formatting properties.
3121      * These properties can also be changed individually through the
3122      * various setter methods.
3123      * <p>
3124      * There is no limit to integer digits set
3125      * by this routine, since that is the typical end-user desire;
3126      * use setMaximumInteger if you want to set a real value.
3127      * For negative numbers, use a second pattern, separated by a semicolon
3128      * <P>Example <code>"#,#00.0#"</code> &rarr; 1,234.56
3129      * <P>This means a minimum of 2 integer digits, 1 fraction digit, and
3130      * a maximum of 2 fraction digits.
3131      * <p>Example: <code>"#,#00.0#;(#,#00.0#)"</code> for negatives in
3132      * parentheses.
3133      * <p>In negative patterns, the minimum and maximum counts are ignored;
3134      * these are presumed to be set in the positive pattern.
3135      *
3136      * @param pattern a new pattern
3137      * @exception NullPointerException if <code>pattern</code> is null
3138      * @exception IllegalArgumentException if the given pattern is invalid.
3139      */
3140     public void applyPattern(String pattern) {
3141         applyPattern(pattern, false);
3142     }
3143 
3144     /**
3145      * Apply the given pattern to this Format object.  The pattern
3146      * is assumed to be in a localized notation. A pattern is a
3147      * short-hand specification for the various formatting properties.
3148      * These properties can also be changed individually through the
3149      * various setter methods.
3150      * <p>
3151      * There is no limit to integer digits set
3152      * by this routine, since that is the typical end-user desire;
3153      * use setMaximumInteger if you want to set a real value.
3154      * For negative numbers, use a second pattern, separated by a semicolon
3155      * <P>Example <code>"#,#00.0#"</code> &rarr; 1,234.56
3156      * <P>This means a minimum of 2 integer digits, 1 fraction digit, and
3157      * a maximum of 2 fraction digits.
3158      * <p>Example: <code>"#,#00.0#;(#,#00.0#)"</code> for negatives in
3159      * parentheses.
3160      * <p>In negative patterns, the minimum and maximum counts are ignored;
3161      * these are presumed to be set in the positive pattern.
3162      *
3163      * @param pattern a new pattern
3164      * @exception NullPointerException if <code>pattern</code> is null
3165      * @exception IllegalArgumentException if the given pattern is invalid.
3166      */
3167     public void applyLocalizedPattern(String pattern) {
3168         applyPattern(pattern, true);
3169     }
3170 
3171     /**
3172      * Does the real work of applying a pattern.
3173      */
3174     private void applyPattern(String pattern, boolean localized) {
3175         char zeroDigit         = PATTERN_ZERO_DIGIT;
3176         char groupingSeparator = PATTERN_GROUPING_SEPARATOR;
3177         char decimalSeparator  = PATTERN_DECIMAL_SEPARATOR;
3178         char percent           = PATTERN_PERCENT;
3179         char perMill           = PATTERN_PER_MILLE;
3180         char digit             = PATTERN_DIGIT;
3181         char separator         = PATTERN_SEPARATOR;
3182         String exponent          = PATTERN_EXPONENT;
3183         char minus             = PATTERN_MINUS;
3184         if (localized) {
3185             zeroDigit         = symbols.getZeroDigit();
3186             groupingSeparator = symbols.getGroupingSeparator();
3187             decimalSeparator  = symbols.getDecimalSeparator();
3188             percent           = symbols.getPercent();
3189             perMill           = symbols.getPerMill();
3190             digit             = symbols.getDigit();
3191             separator         = symbols.getPatternSeparator();
3192             exponent          = symbols.getExponentSeparator();
3193             minus             = symbols.getMinusSign();
3194         }
3195         boolean gotNegative = false;
3196         decimalSeparatorAlwaysShown = false;
3197         isCurrencyFormat = false;
3198         useExponentialNotation = false;
3199 
3200         // Two variables are used to record the subrange of the pattern
3201         // occupied by phase 1.  This is used during the processing of the
3202         // second pattern (the one representing negative numbers) to ensure
3203         // that no deviation exists in phase 1 between the two patterns.
3204         int phaseOneStart = 0;
3205         int phaseOneLength = 0;
3206 
3207         int start = 0;
3208         for (int j = 1; j >= 0 && start < pattern.length(); --j) {
3209             boolean inQuote = false;
3210             StringBuffer prefix = new StringBuffer();
3211             StringBuffer suffix = new StringBuffer();
3212             int decimalPos = -1;
3213             int multiplier = 1;
3214             int digitLeftCount = 0, zeroDigitCount = 0, digitRightCount = 0;
3215             byte groupingCount = -1;
3216 
3217             // The phase ranges from 0 to 2.  Phase 0 is the prefix.  Phase 1 is
3218             // the section of the pattern with digits, decimal separator,
3219             // grouping characters.  Phase 2 is the suffix.  In phases 0 and 2,
3220             // percent, per mille, and currency symbols are recognized and
3221             // translated.  The separation of the characters into phases is
3222             // strictly enforced; if phase 1 characters are to appear in the
3223             // suffix, for example, they must be quoted.
3224             int phase = 0;
3225 
3226             // The affix is either the prefix or the suffix.
3227             StringBuffer affix = prefix;
3228 
3229             for (int pos = start; pos < pattern.length(); ++pos) {
3230                 char ch = pattern.charAt(pos);
3231                 switch (phase) {
3232                 case 0:
3233                 case 2:
3234                     // Process the prefix / suffix characters
3235                     if (inQuote) {
3236                         // A quote within quotes indicates either the closing
3237                         // quote or two quotes, which is a quote literal. That
3238                         // is, we have the second quote in 'do' or 'don''t'.
3239                         if (ch == QUOTE) {
3240                             if ((pos+1) < pattern.length() &&
3241                                 pattern.charAt(pos+1) == QUOTE) {
3242                                 ++pos;
3243                                 affix.append("''"); // 'don''t'
3244                             } else {
3245                                 inQuote = false; // 'do'
3246                             }
3247                             continue;
3248                         }
3249                     } else {
3250                         // Process unquoted characters seen in prefix or suffix
3251                         // phase.
3252                         if (ch == digit ||
3253                             ch == zeroDigit ||
3254                             ch == groupingSeparator ||
3255                             ch == decimalSeparator) {
3256                             phase = 1;
3257                             if (j == 1) {
3258                                 phaseOneStart = pos;
3259                             }
3260                             --pos; // Reprocess this character
3261                             continue;
3262                         } else if (ch == CURRENCY_SIGN) {
3263                             // Use lookahead to determine if the currency sign
3264                             // is doubled or not.
3265                             boolean doubled = (pos + 1) < pattern.length() &&
3266                                 pattern.charAt(pos + 1) == CURRENCY_SIGN;
3267                             if (doubled) { // Skip over the doubled character
3268                              ++pos;
3269                             }
3270                             isCurrencyFormat = true;
3271                             affix.append(doubled ? "'\u00A4\u00A4" : "'\u00A4");
3272                             continue;
3273                         } else if (ch == QUOTE) {
3274                             // A quote outside quotes indicates either the
3275                             // opening quote or two quotes, which is a quote
3276                             // literal. That is, we have the first quote in 'do'
3277                             // or o''clock.
3278                             if (ch == QUOTE) {
3279                                 if ((pos+1) < pattern.length() &&
3280                                     pattern.charAt(pos+1) == QUOTE) {
3281                                     ++pos;
3282                                     affix.append("''"); // o''clock
3283                                 } else {
3284                                     inQuote = true; // 'do'
3285                                 }
3286                                 continue;
3287                             }
3288                         } else if (ch == separator) {
3289                             // Don't allow separators before we see digit
3290                             // characters of phase 1, and don't allow separators
3291                             // in the second pattern (j == 0).
3292                             if (phase == 0 || j == 0) {
3293                                 throw new IllegalArgumentException("Unquoted special character '" +
3294                                     ch + "' in pattern \"" + pattern + '"');
3295                             }
3296                             start = pos + 1;
3297                             pos = pattern.length();
3298                             continue;
3299                         }
3300 
3301                         // Next handle characters which are appended directly.
3302                         else if (ch == percent) {
3303                             if (multiplier != 1) {
3304                                 throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" +
3305                                     pattern + '"');
3306                             }
3307                             multiplier = 100;
3308                             affix.append("'%");
3309                             continue;
3310                         } else if (ch == perMill) {
3311                             if (multiplier != 1) {
3312                                 throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" +
3313                                     pattern + '"');
3314                             }
3315                             multiplier = 1000;
3316                             affix.append("'\u2030");
3317                             continue;
3318                         } else if (ch == minus) {
3319                             affix.append("'-");
3320                             continue;
3321                         }
3322                     }
3323                     // Note that if we are within quotes, or if this is an
3324                     // unquoted, non-special character, then we usually fall
3325                     // through to here.
3326                     affix.append(ch);
3327                     break;
3328 
3329                 case 1:
3330                     // Phase one must be identical in the two sub-patterns. We
3331                     // enforce this by doing a direct comparison. While
3332                     // processing the first sub-pattern, we just record its
3333                     // length. While processing the second, we compare
3334                     // characters.
3335                     if (j == 1) {
3336                         ++phaseOneLength;
3337                     } else {
3338                         if (--phaseOneLength == 0) {
3339                             phase = 2;
3340                             affix = suffix;
3341                         }
3342                         continue;
3343                     }
3344 
3345                     // Process the digits, decimal, and grouping characters. We
3346                     // record five pieces of information. We expect the digits
3347                     // to occur in the pattern ####0000.####, and we record the
3348                     // number of left digits, zero (central) digits, and right
3349                     // digits. The position of the last grouping character is
3350                     // recorded (should be somewhere within the first two blocks
3351                     // of characters), as is the position of the decimal point,
3352                     // if any (should be in the zero digits). If there is no
3353                     // decimal point, then there should be no right digits.
3354                     if (ch == digit) {
3355                         if (zeroDigitCount > 0) {
3356                             ++digitRightCount;
3357                         } else {
3358                             ++digitLeftCount;
3359                         }
3360                         if (groupingCount >= 0 && decimalPos < 0) {
3361                             ++groupingCount;
3362                         }
3363                     } else if (ch == zeroDigit) {
3364                         if (digitRightCount > 0) {
3365                             throw new IllegalArgumentException("Unexpected '0' in pattern \"" +
3366                                 pattern + '"');
3367                         }
3368                         ++zeroDigitCount;
3369                         if (groupingCount >= 0 && decimalPos < 0) {
3370                             ++groupingCount;
3371                         }
3372                     } else if (ch == groupingSeparator) {
3373                         groupingCount = 0;
3374                     } else if (ch == decimalSeparator) {
3375                         if (decimalPos >= 0) {
3376                             throw new IllegalArgumentException("Multiple decimal separators in pattern \"" +
3377                                 pattern + '"');
3378                         }
3379                         decimalPos = digitLeftCount + zeroDigitCount + digitRightCount;
3380                     } else if (pattern.regionMatches(pos, exponent, 0, exponent.length())){
3381                         if (useExponentialNotation) {
3382                             throw new IllegalArgumentException("Multiple exponential " +
3383                                 "symbols in pattern \"" + pattern + '"');
3384                         }
3385                         useExponentialNotation = true;
3386                         minExponentDigits = 0;
3387 
3388                         // Use lookahead to parse out the exponential part
3389                         // of the pattern, then jump into phase 2.
3390                         pos = pos+exponent.length();
3391                          while (pos < pattern.length() &&
3392                                pattern.charAt(pos) == zeroDigit) {
3393                             ++minExponentDigits;
3394                             ++phaseOneLength;
3395                             ++pos;
3396                         }
3397 
3398                         if ((digitLeftCount + zeroDigitCount) < 1 ||
3399                             minExponentDigits < 1) {
3400                             throw new IllegalArgumentException("Malformed exponential " +
3401                                 "pattern \"" + pattern + '"');
3402                         }
3403 
3404                         // Transition to phase 2
3405                         phase = 2;
3406                         affix = suffix;
3407                         --pos;
3408                         continue;
3409                     } else {
3410                         phase = 2;
3411                         affix = suffix;
3412                         --pos;
3413                         --phaseOneLength;
3414                         continue;
3415                     }
3416                     break;
3417                 }
3418             }
3419 
3420             // Handle patterns with no '0' pattern character. These patterns
3421             // are legal, but must be interpreted.  "##.###" -> "#0.###".
3422             // ".###" -> ".0##".
3423             /* We allow patterns of the form "####" to produce a zeroDigitCount
3424              * of zero (got that?); although this seems like it might make it
3425              * possible for format() to produce empty strings, format() checks
3426              * for this condition and outputs a zero digit in this situation.
3427              * Having a zeroDigitCount of zero yields a minimum integer digits
3428              * of zero, which allows proper round-trip patterns.  That is, we
3429              * don't want "#" to become "#0" when toPattern() is called (even
3430              * though that's what it really is, semantically).
3431              */
3432             if (zeroDigitCount == 0 && digitLeftCount > 0 && decimalPos >= 0) {
3433                 // Handle "###.###" and "###." and ".###"
3434                 int n = decimalPos;
3435                 if (n == 0) { // Handle ".###"
3436                     ++n;
3437                 }
3438                 digitRightCount = digitLeftCount - n;
3439                 digitLeftCount = n - 1;
3440                 zeroDigitCount = 1;
3441             }
3442 
3443             // Do syntax checking on the digits.
3444             if ((decimalPos < 0 && digitRightCount > 0) ||
3445                 (decimalPos >= 0 && (decimalPos < digitLeftCount ||
3446                  decimalPos > (digitLeftCount + zeroDigitCount))) ||
3447                  groupingCount == 0 || inQuote) {
3448                 throw new IllegalArgumentException("Malformed pattern \"" +
3449                     pattern + '"');
3450             }
3451 
3452             if (j == 1) {
3453                 posPrefixPattern = prefix.toString();
3454                 posSuffixPattern = suffix.toString();
3455                 negPrefixPattern = posPrefixPattern;   // assume these for now
3456                 negSuffixPattern = posSuffixPattern;
3457                 int digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount;
3458                 /* The effectiveDecimalPos is the position the decimal is at or
3459                  * would be at if there is no decimal. Note that if decimalPos<0,
3460                  * then digitTotalCount == digitLeftCount + zeroDigitCount.
3461                  */
3462                 int effectiveDecimalPos = decimalPos >= 0 ?
3463                     decimalPos : digitTotalCount;
3464                 setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
3465                 setMaximumIntegerDigits(useExponentialNotation ?
3466                     digitLeftCount + getMinimumIntegerDigits() :
3467                     MAXIMUM_INTEGER_DIGITS);
3468                 setMaximumFractionDigits(decimalPos >= 0 ?
3469                     (digitTotalCount - decimalPos) : 0);
3470                 setMinimumFractionDigits(decimalPos >= 0 ?
3471                     (digitLeftCount + zeroDigitCount - decimalPos) : 0);
3472                 setGroupingUsed(groupingCount > 0);
3473                 this.groupingSize = (groupingCount > 0) ? groupingCount : 0;
3474                 this.multiplier = multiplier;
3475                 setDecimalSeparatorAlwaysShown(decimalPos == 0 ||
3476                     decimalPos == digitTotalCount);
3477             } else {
3478                 negPrefixPattern = prefix.toString();
3479                 negSuffixPattern = suffix.toString();
3480                 gotNegative = true;
3481             }
3482         }
3483 
3484         if (pattern.length() == 0) {
3485             posPrefixPattern = posSuffixPattern = "";
3486             setMinimumIntegerDigits(0);
3487             setMaximumIntegerDigits(MAXIMUM_INTEGER_DIGITS);
3488             setMinimumFractionDigits(0);
3489             setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS);
3490         }
3491 
3492         // If there was no negative pattern, or if the negative pattern is
3493         // identical to the positive pattern, then prepend the minus sign to
3494         // the positive pattern to form the negative pattern.
3495         if (!gotNegative ||
3496             (negPrefixPattern.equals(posPrefixPattern)
3497              && negSuffixPattern.equals(posSuffixPattern))) {
3498             negSuffixPattern = posSuffixPattern;
3499             negPrefixPattern = "'-" + posPrefixPattern;
3500         }
3501 
3502         expandAffixes();
3503     }
3504 
3505     /**
3506      * Sets the maximum number of digits allowed in the integer portion of a
3507      * number.
3508      * For formatting numbers other than <code>BigInteger</code> and
3509      * <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
3510      * 309 is used. Negative input values are replaced with 0.
3511      * @see NumberFormat#setMaximumIntegerDigits
3512      */
3513     @Override
3514     public void setMaximumIntegerDigits(int newValue) {
3515         maximumIntegerDigits = Math.min(Math.max(0, newValue), MAXIMUM_INTEGER_DIGITS);
3516         super.setMaximumIntegerDigits((maximumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
3517             DOUBLE_INTEGER_DIGITS : maximumIntegerDigits);
3518         if (minimumIntegerDigits > maximumIntegerDigits) {
3519             minimumIntegerDigits = maximumIntegerDigits;
3520             super.setMinimumIntegerDigits((minimumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
3521                 DOUBLE_INTEGER_DIGITS : minimumIntegerDigits);
3522         }
3523         fastPathCheckNeeded = true;
3524     }
3525 
3526     /**
3527      * Sets the minimum number of digits allowed in the integer portion of a
3528      * number.
3529      * For formatting numbers other than <code>BigInteger</code> and
3530      * <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
3531      * 309 is used. Negative input values are replaced with 0.
3532      * @see NumberFormat#setMinimumIntegerDigits
3533      */
3534     @Override
3535     public void setMinimumIntegerDigits(int newValue) {
3536         minimumIntegerDigits = Math.min(Math.max(0, newValue), MAXIMUM_INTEGER_DIGITS);
3537         super.setMinimumIntegerDigits((minimumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
3538             DOUBLE_INTEGER_DIGITS : minimumIntegerDigits);
3539         if (minimumIntegerDigits > maximumIntegerDigits) {
3540             maximumIntegerDigits = minimumIntegerDigits;
3541             super.setMaximumIntegerDigits((maximumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
3542                 DOUBLE_INTEGER_DIGITS : maximumIntegerDigits);
3543         }
3544         fastPathCheckNeeded = true;
3545     }
3546 
3547     /**
3548      * Sets the maximum number of digits allowed in the fraction portion of a
3549      * number.
3550      * For formatting numbers other than <code>BigInteger</code> and
3551      * <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
3552      * 340 is used. Negative input values are replaced with 0.
3553      * @see NumberFormat#setMaximumFractionDigits
3554      */
3555     @Override
3556     public void setMaximumFractionDigits(int newValue) {
3557         maximumFractionDigits = Math.min(Math.max(0, newValue), MAXIMUM_FRACTION_DIGITS);
3558         super.setMaximumFractionDigits((maximumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
3559             DOUBLE_FRACTION_DIGITS : maximumFractionDigits);
3560         if (minimumFractionDigits > maximumFractionDigits) {
3561             minimumFractionDigits = maximumFractionDigits;
3562             super.setMinimumFractionDigits((minimumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
3563                 DOUBLE_FRACTION_DIGITS : minimumFractionDigits);
3564         }
3565         fastPathCheckNeeded = true;
3566     }
3567 
3568     /**
3569      * Sets the minimum number of digits allowed in the fraction portion of a
3570      * number.
3571      * For formatting numbers other than <code>BigInteger</code> and
3572      * <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
3573      * 340 is used. Negative input values are replaced with 0.
3574      * @see NumberFormat#setMinimumFractionDigits
3575      */
3576     @Override
3577     public void setMinimumFractionDigits(int newValue) {
3578         minimumFractionDigits = Math.min(Math.max(0, newValue), MAXIMUM_FRACTION_DIGITS);
3579         super.setMinimumFractionDigits((minimumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
3580             DOUBLE_FRACTION_DIGITS : minimumFractionDigits);
3581         if (minimumFractionDigits > maximumFractionDigits) {
3582             maximumFractionDigits = minimumFractionDigits;
3583             super.setMaximumFractionDigits((maximumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
3584                 DOUBLE_FRACTION_DIGITS : maximumFractionDigits);
3585         }
3586         fastPathCheckNeeded = true;
3587     }
3588 
3589     /**
3590      * Gets the maximum number of digits allowed in the integer portion of a
3591      * number.
3592      * For formatting numbers other than <code>BigInteger</code> and
3593      * <code>BigDecimal</code> objects, the lower of the return value and
3594      * 309 is used.
3595      * @see #setMaximumIntegerDigits
3596      */
3597     @Override
3598     public int getMaximumIntegerDigits() {
3599         return maximumIntegerDigits;
3600     }
3601 
3602     /**
3603      * Gets the minimum number of digits allowed in the integer portion of a
3604      * number.
3605      * For formatting numbers other than <code>BigInteger</code> and
3606      * <code>BigDecimal</code> objects, the lower of the return value and
3607      * 309 is used.
3608      * @see #setMinimumIntegerDigits
3609      */
3610     @Override
3611     public int getMinimumIntegerDigits() {
3612         return minimumIntegerDigits;
3613     }
3614 
3615     /**
3616      * Gets the maximum number of digits allowed in the fraction portion of a
3617      * number.
3618      * For formatting numbers other than <code>BigInteger</code> and
3619      * <code>BigDecimal</code> objects, the lower of the return value and
3620      * 340 is used.
3621      * @see #setMaximumFractionDigits
3622      */
3623     @Override
3624     public int getMaximumFractionDigits() {
3625         return maximumFractionDigits;
3626     }
3627 
3628     /**
3629      * Gets the minimum number of digits allowed in the fraction portion of a
3630      * number.
3631      * For formatting numbers other than <code>BigInteger</code> and
3632      * <code>BigDecimal</code> objects, the lower of the return value and
3633      * 340 is used.
3634      * @see #setMinimumFractionDigits
3635      */
3636     @Override
3637     public int getMinimumFractionDigits() {
3638         return minimumFractionDigits;
3639     }
3640 
3641     /**
3642      * Gets the currency used by this decimal format when formatting
3643      * currency values.
3644      * The currency is obtained by calling
3645      * {@link DecimalFormatSymbols#getCurrency DecimalFormatSymbols.getCurrency}
3646      * on this number format's symbols.
3647      *
3648      * @return the currency used by this decimal format, or <code>null</code>
3649      * @since 1.4
3650      */
3651     @Override
3652     public Currency getCurrency() {
3653         return symbols.getCurrency();
3654     }
3655 
3656     /**
3657      * Sets the currency used by this number format when formatting
3658      * currency values. This does not update the minimum or maximum
3659      * number of fraction digits used by the number format.
3660      * The currency is set by calling
3661      * {@link DecimalFormatSymbols#setCurrency DecimalFormatSymbols.setCurrency}
3662      * on this number format's symbols.
3663      *
3664      * @param currency the new currency to be used by this decimal format
3665      * @exception NullPointerException if <code>currency</code> is null
3666      * @since 1.4
3667      */
3668     @Override
3669     public void setCurrency(Currency currency) {
3670         if (currency != symbols.getCurrency()) {
3671             symbols.setCurrency(currency);
3672             if (isCurrencyFormat) {
3673                 expandAffixes();
3674             }
3675         }
3676         fastPathCheckNeeded = true;
3677     }
3678 
3679     /**
3680      * Gets the {@link java.math.RoundingMode} used in this DecimalFormat.
3681      *
3682      * @return The <code>RoundingMode</code> used for this DecimalFormat.
3683      * @see #setRoundingMode(RoundingMode)
3684      * @since 1.6
3685      */
3686     @Override
3687     public RoundingMode getRoundingMode() {
3688         return roundingMode;
3689     }
3690 
3691     /**
3692      * Sets the {@link java.math.RoundingMode} used in this DecimalFormat.
3693      *
3694      * @param roundingMode The <code>RoundingMode</code> to be used
3695      * @see #getRoundingMode()
3696      * @exception NullPointerException if <code>roundingMode</code> is null.
3697      * @since 1.6
3698      */
3699     @Override
3700     public void setRoundingMode(RoundingMode roundingMode) {
3701         if (roundingMode == null) {
3702             throw new NullPointerException();
3703         }
3704 
3705         this.roundingMode = roundingMode;
3706         digitList.setRoundingMode(roundingMode);
3707         fastPathCheckNeeded = true;
3708     }
3709 
3710     /**
3711      * Reads the default serializable fields from the stream and performs
3712      * validations and adjustments for older serialized versions. The
3713      * validations and adjustments are:
3714      * <ol>
3715      * <li>
3716      * Verify that the superclass's digit count fields correctly reflect
3717      * the limits imposed on formatting numbers other than
3718      * <code>BigInteger</code> and <code>BigDecimal</code> objects. These
3719      * limits are stored in the superclass for serialization compatibility
3720      * with older versions, while the limits for <code>BigInteger</code> and
3721      * <code>BigDecimal</code> objects are kept in this class.
3722      * If, in the superclass, the minimum or maximum integer digit count is
3723      * larger than <code>DOUBLE_INTEGER_DIGITS</code> or if the minimum or
3724      * maximum fraction digit count is larger than
3725      * <code>DOUBLE_FRACTION_DIGITS</code>, then the stream data is invalid
3726      * and this method throws an <code>InvalidObjectException</code>.
3727      * <li>
3728      * If <code>serialVersionOnStream</code> is less than 4, initialize
3729      * <code>roundingMode</code> to {@link java.math.RoundingMode#HALF_EVEN
3730      * RoundingMode.HALF_EVEN}.  This field is new with version 4.
3731      * <li>
3732      * If <code>serialVersionOnStream</code> is less than 3, then call
3733      * the setters for the minimum and maximum integer and fraction digits with
3734      * the values of the corresponding superclass getters to initialize the
3735      * fields in this class. The fields in this class are new with version 3.
3736      * <li>
3737      * If <code>serialVersionOnStream</code> is less than 1, indicating that
3738      * the stream was written by JDK 1.1, initialize
3739      * <code>useExponentialNotation</code>
3740      * to false, since it was not present in JDK 1.1.
3741      * <li>
3742      * Set <code>serialVersionOnStream</code> to the maximum allowed value so
3743      * that default serialization will work properly if this object is streamed
3744      * out again.
3745      * </ol>
3746      *
3747      * <p>Stream versions older than 2 will not have the affix pattern variables
3748      * <code>posPrefixPattern</code> etc.  As a result, they will be initialized
3749      * to <code>null</code>, which means the affix strings will be taken as
3750      * literal values.  This is exactly what we want, since that corresponds to
3751      * the pre-version-2 behavior.
3752      */
3753     private void readObject(ObjectInputStream stream)
3754          throws IOException, ClassNotFoundException
3755     {
3756         stream.defaultReadObject();
3757         digitList = new DigitList();
3758 
3759         // We force complete fast-path reinitialization when the instance is
3760         // deserialized. See clone() comment on fastPathCheckNeeded.
3761         fastPathCheckNeeded = true;
3762         isFastPath = false;
3763         fastPathData = null;
3764 
3765         if (serialVersionOnStream < 4) {
3766             setRoundingMode(RoundingMode.HALF_EVEN);
3767         } else {
3768             setRoundingMode(getRoundingMode());
3769         }
3770 
3771         // We only need to check the maximum counts because NumberFormat
3772         // .readObject has already ensured that the maximum is greater than the
3773         // minimum count.
3774         if (super.getMaximumIntegerDigits() > DOUBLE_INTEGER_DIGITS ||
3775             super.getMaximumFractionDigits() > DOUBLE_FRACTION_DIGITS) {
3776             throw new InvalidObjectException("Digit count out of range");
3777         }
3778         if (serialVersionOnStream < 3) {
3779             setMaximumIntegerDigits(super.getMaximumIntegerDigits());
3780             setMinimumIntegerDigits(super.getMinimumIntegerDigits());
3781             setMaximumFractionDigits(super.getMaximumFractionDigits());
3782             setMinimumFractionDigits(super.getMinimumFractionDigits());
3783         }
3784         if (serialVersionOnStream < 1) {
3785             // Didn't have exponential fields
3786             useExponentialNotation = false;
3787         }
3788         serialVersionOnStream = currentSerialVersion;
3789     }
3790 
3791     //----------------------------------------------------------------------
3792     // INSTANCE VARIABLES
3793     //----------------------------------------------------------------------
3794 
3795     private transient DigitList digitList = new DigitList();
3796 
3797     /**
3798      * The symbol used as a prefix when formatting positive numbers, e.g. "+".
3799      *
3800      * @serial
3801      * @see #getPositivePrefix
3802      */
3803     private String  positivePrefix = "";
3804 
3805     /**
3806      * The symbol used as a suffix when formatting positive numbers.
3807      * This is often an empty string.
3808      *
3809      * @serial
3810      * @see #getPositiveSuffix
3811      */
3812     private String  positiveSuffix = "";
3813 
3814     /**
3815      * The symbol used as a prefix when formatting negative numbers, e.g. "-".
3816      *
3817      * @serial
3818      * @see #getNegativePrefix
3819      */
3820     private String  negativePrefix = "-";
3821 
3822     /**
3823      * The symbol used as a suffix when formatting negative numbers.
3824      * This is often an empty string.
3825      *
3826      * @serial
3827      * @see #getNegativeSuffix
3828      */
3829     private String  negativeSuffix = "";
3830 
3831     /**
3832      * The prefix pattern for non-negative numbers.  This variable corresponds
3833      * to <code>positivePrefix</code>.
3834      *
3835      * <p>This pattern is expanded by the method <code>expandAffix()</code> to
3836      * <code>positivePrefix</code> to update the latter to reflect changes in
3837      * <code>symbols</code>.  If this variable is <code>null</code> then
3838      * <code>positivePrefix</code> is taken as a literal value that does not
3839      * change when <code>symbols</code> changes.  This variable is always
3840      * <code>null</code> for <code>DecimalFormat</code> objects older than
3841      * stream version 2 restored from stream.
3842      *
3843      * @serial
3844      * @since 1.3
3845      */
3846     private String posPrefixPattern;
3847 
3848     /**
3849      * The suffix pattern for non-negative numbers.  This variable corresponds
3850      * to <code>positiveSuffix</code>.  This variable is analogous to
3851      * <code>posPrefixPattern</code>; see that variable for further
3852      * documentation.
3853      *
3854      * @serial
3855      * @since 1.3
3856      */
3857     private String posSuffixPattern;
3858 
3859     /**
3860      * The prefix pattern for negative numbers.  This variable corresponds
3861      * to <code>negativePrefix</code>.  This variable is analogous to
3862      * <code>posPrefixPattern</code>; see that variable for further
3863      * documentation.
3864      *
3865      * @serial
3866      * @since 1.3
3867      */
3868     private String negPrefixPattern;
3869 
3870     /**
3871      * The suffix pattern for negative numbers.  This variable corresponds
3872      * to <code>negativeSuffix</code>.  This variable is analogous to
3873      * <code>posPrefixPattern</code>; see that variable for further
3874      * documentation.
3875      *
3876      * @serial
3877      * @since 1.3
3878      */
3879     private String negSuffixPattern;
3880 
3881     /**
3882      * The multiplier for use in percent, per mille, etc.
3883      *
3884      * @serial
3885      * @see #getMultiplier
3886      */
3887     private int     multiplier = 1;
3888 
3889     /**
3890      * The number of digits between grouping separators in the integer
3891      * portion of a number.  Must be greater than 0 if
3892      * <code>NumberFormat.groupingUsed</code> is true.
3893      *
3894      * @serial
3895      * @see #getGroupingSize
3896      * @see java.text.NumberFormat#isGroupingUsed
3897      */
3898     private byte    groupingSize = 3;  // invariant, > 0 if useThousands
3899 
3900     /**
3901      * If true, forces the decimal separator to always appear in a formatted
3902      * number, even if the fractional part of the number is zero.
3903      *
3904      * @serial
3905      * @see #isDecimalSeparatorAlwaysShown
3906      */
3907     private boolean decimalSeparatorAlwaysShown = false;
3908 
3909     /**
3910      * If true, parse returns BigDecimal wherever possible.
3911      *
3912      * @serial
3913      * @see #isParseBigDecimal
3914      * @since 1.5
3915      */
3916     private boolean parseBigDecimal = false;
3917 
3918 
3919     /**
3920      * True if this object represents a currency format.  This determines
3921      * whether the monetary decimal separator is used instead of the normal one.
3922      */
3923     private transient boolean isCurrencyFormat = false;
3924 
3925     /**
3926      * The <code>DecimalFormatSymbols</code> object used by this format.
3927      * It contains the symbols used to format numbers, e.g. the grouping separator,
3928      * decimal separator, and so on.
3929      *
3930      * @serial
3931      * @see #setDecimalFormatSymbols
3932      * @see java.text.DecimalFormatSymbols
3933      */
3934     private DecimalFormatSymbols symbols = null; // LIU new DecimalFormatSymbols();
3935 
3936     /**
3937      * True to force the use of exponential (i.e. scientific) notation when formatting
3938      * numbers.
3939      *
3940      * @serial
3941      * @since 1.2
3942      */
3943     private boolean useExponentialNotation;  // Newly persistent in the Java 2 platform v.1.2
3944 
3945     /**
3946      * FieldPositions describing the positive prefix String. This is
3947      * lazily created. Use <code>getPositivePrefixFieldPositions</code>
3948      * when needed.
3949      */
3950     private transient FieldPosition[] positivePrefixFieldPositions;
3951 
3952     /**
3953      * FieldPositions describing the positive suffix String. This is
3954      * lazily created. Use <code>getPositiveSuffixFieldPositions</code>
3955      * when needed.
3956      */
3957     private transient FieldPosition[] positiveSuffixFieldPositions;
3958 
3959     /**
3960      * FieldPositions describing the negative prefix String. This is
3961      * lazily created. Use <code>getNegativePrefixFieldPositions</code>
3962      * when needed.
3963      */
3964     private transient FieldPosition[] negativePrefixFieldPositions;
3965 
3966     /**
3967      * FieldPositions describing the negative suffix String. This is
3968      * lazily created. Use <code>getNegativeSuffixFieldPositions</code>
3969      * when needed.
3970      */
3971     private transient FieldPosition[] negativeSuffixFieldPositions;
3972 
3973     /**
3974      * The minimum number of digits used to display the exponent when a number is
3975      * formatted in exponential notation.  This field is ignored if
3976      * <code>useExponentialNotation</code> is not true.
3977      *
3978      * @serial
3979      * @since 1.2
3980      */
3981     private byte    minExponentDigits;       // Newly persistent in the Java 2 platform v.1.2
3982 
3983     /**
3984      * The maximum number of digits allowed in the integer portion of a
3985      * <code>BigInteger</code> or <code>BigDecimal</code> number.
3986      * <code>maximumIntegerDigits</code> must be greater than or equal to
3987      * <code>minimumIntegerDigits</code>.
3988      *
3989      * @serial
3990      * @see #getMaximumIntegerDigits
3991      * @since 1.5
3992      */
3993     private int    maximumIntegerDigits = super.getMaximumIntegerDigits();
3994 
3995     /**
3996      * The minimum number of digits allowed in the integer portion of a
3997      * <code>BigInteger</code> or <code>BigDecimal</code> number.
3998      * <code>minimumIntegerDigits</code> must be less than or equal to
3999      * <code>maximumIntegerDigits</code>.
4000      *
4001      * @serial
4002      * @see #getMinimumIntegerDigits
4003      * @since 1.5
4004      */
4005     private int    minimumIntegerDigits = super.getMinimumIntegerDigits();
4006 
4007     /**
4008      * The maximum number of digits allowed in the fractional portion of a
4009      * <code>BigInteger</code> or <code>BigDecimal</code> number.
4010      * <code>maximumFractionDigits</code> must be greater than or equal to
4011      * <code>minimumFractionDigits</code>.
4012      *
4013      * @serial
4014      * @see #getMaximumFractionDigits
4015      * @since 1.5
4016      */
4017     private int    maximumFractionDigits = super.getMaximumFractionDigits();
4018 
4019     /**
4020      * The minimum number of digits allowed in the fractional portion of a
4021      * <code>BigInteger</code> or <code>BigDecimal</code> number.
4022      * <code>minimumFractionDigits</code> must be less than or equal to
4023      * <code>maximumFractionDigits</code>.
4024      *
4025      * @serial
4026      * @see #getMinimumFractionDigits
4027      * @since 1.5
4028      */
4029     private int    minimumFractionDigits = super.getMinimumFractionDigits();
4030 
4031     /**
4032      * The {@link java.math.RoundingMode} used in this DecimalFormat.
4033      *
4034      * @serial
4035      * @since 1.6
4036      */
4037     private RoundingMode roundingMode = RoundingMode.HALF_EVEN;
4038 
4039     // ------ DecimalFormat fields for fast-path for double algorithm  ------
4040 
4041     /**
4042      * Helper inner utility class for storing the data used in the fast-path
4043      * algorithm. Almost all fields related to fast-path are encapsulated in
4044      * this class.
4045      *
4046      * Any {@code DecimalFormat} instance has a {@code fastPathData}
4047      * reference field that is null unless both the properties of the instance
4048      * are such that the instance is in the "fast-path" state, and a format call
4049      * has been done at least once while in this state.
4050      *
4051      * Almost all fields are related to the "fast-path" state only and don't
4052      * change until one of the instance properties is changed.
4053      *
4054      * {@code firstUsedIndex} and {@code lastFreeIndex} are the only
4055      * two fields that are used and modified while inside a call to
4056      * {@code fastDoubleFormat}.
4057      *
4058      */
4059     private static class FastPathData {
4060         // --- Temporary fields used in fast-path, shared by several methods.
4061 
4062         /** The first unused index at the end of the formatted result. */
4063         int lastFreeIndex;
4064 
4065         /** The first used index at the beginning of the formatted result */
4066         int firstUsedIndex;
4067 
4068         // --- State fields related to fast-path status. Changes due to a
4069         //     property change only. Set by checkAndSetFastPathStatus() only.
4070 
4071         /** Difference between locale zero and default zero representation. */
4072         int  zeroDelta;
4073 
4074         /** Locale char for grouping separator. */
4075         char groupingChar;
4076 
4077         /**  Fixed index position of last integral digit of formatted result */
4078         int integralLastIndex;
4079 
4080         /**  Fixed index position of first fractional digit of formatted result */
4081         int fractionalFirstIndex;
4082 
4083         /** Fractional constants depending on decimal|currency state */
4084         double fractionalScaleFactor;
4085         int fractionalMaxIntBound;
4086 
4087 
4088         /** The char array buffer that will contain the formatted result */
4089         char[] fastPathContainer;
4090 
4091         /** Suffixes recorded as char array for efficiency. */
4092         char[] charsPositivePrefix;
4093         char[] charsNegativePrefix;
4094         char[] charsPositiveSuffix;
4095         char[] charsNegativeSuffix;
4096         boolean positiveAffixesRequired = true;
4097         boolean negativeAffixesRequired = true;
4098     }
4099 
4100     /** The format fast-path status of the instance. Logical state. */
4101     private transient boolean isFastPath = false;
4102 
4103     /** Flag stating need of check and reinit fast-path status on next format call. */
4104     private transient boolean fastPathCheckNeeded = true;
4105 
4106     /** DecimalFormat reference to its FastPathData */
4107     private transient FastPathData fastPathData;
4108 
4109 
4110     //----------------------------------------------------------------------
4111 
4112     static final int currentSerialVersion = 4;
4113 
4114     /**
4115      * The internal serial version which says which version was written.
4116      * Possible values are:
4117      * <ul>
4118      * <li><b>0</b> (default): versions before the Java 2 platform v1.2
4119      * <li><b>1</b>: version for 1.2, which includes the two new fields
4120      *      <code>useExponentialNotation</code> and
4121      *      <code>minExponentDigits</code>.
4122      * <li><b>2</b>: version for 1.3 and later, which adds four new fields:
4123      *      <code>posPrefixPattern</code>, <code>posSuffixPattern</code>,
4124      *      <code>negPrefixPattern</code>, and <code>negSuffixPattern</code>.
4125      * <li><b>3</b>: version for 1.5 and later, which adds five new fields:
4126      *      <code>maximumIntegerDigits</code>,
4127      *      <code>minimumIntegerDigits</code>,
4128      *      <code>maximumFractionDigits</code>,
4129      *      <code>minimumFractionDigits</code>, and
4130      *      <code>parseBigDecimal</code>.
4131      * <li><b>4</b>: version for 1.6 and later, which adds one new field:
4132      *      <code>roundingMode</code>.
4133      * </ul>
4134      * @since 1.2
4135      * @serial
4136      */
4137     private int serialVersionOnStream = currentSerialVersion;
4138 
4139     //----------------------------------------------------------------------
4140     // CONSTANTS
4141     //----------------------------------------------------------------------
4142 
4143     // ------ Fast-Path for double Constants ------
4144 
4145     /** Maximum valid integer value for applying fast-path algorithm */
4146     private static final double MAX_INT_AS_DOUBLE = (double) Integer.MAX_VALUE;
4147 
4148     /**
4149      * The digit arrays used in the fast-path methods for collecting digits.
4150      * Using 3 constants arrays of chars ensures a very fast collection of digits
4151      */
4152     private static class DigitArrays {
4153         static final char[] DigitOnes1000 = new char[1000];
4154         static final char[] DigitTens1000 = new char[1000];
4155         static final char[] DigitHundreds1000 = new char[1000];
4156 
4157         // initialize on demand holder class idiom for arrays of digits
4158         static {
4159             int tenIndex = 0;
4160             int hundredIndex = 0;
4161             char digitOne = '0';
4162             char digitTen = '0';
4163             char digitHundred = '0';
4164             for (int i = 0;  i < 1000; i++ ) {
4165 
4166                 DigitOnes1000[i] = digitOne;
4167                 if (digitOne == '9')
4168                     digitOne = '0';
4169                 else
4170                     digitOne++;
4171 
4172                 DigitTens1000[i] = digitTen;
4173                 if (i == (tenIndex + 9)) {
4174                     tenIndex += 10;
4175                     if (digitTen == '9')
4176                         digitTen = '0';
4177                     else
4178                         digitTen++;
4179                 }
4180 
4181                 DigitHundreds1000[i] = digitHundred;
4182                 if (i == (hundredIndex + 99)) {
4183                     digitHundred++;
4184                     hundredIndex += 100;
4185                 }
4186             }
4187         }
4188     }
4189     // ------ Fast-Path for double Constants end ------
4190 
4191     // Constants for characters used in programmatic (unlocalized) patterns.
4192     private static final char       PATTERN_ZERO_DIGIT         = '0';
4193     private static final char       PATTERN_GROUPING_SEPARATOR = ',';
4194     private static final char       PATTERN_DECIMAL_SEPARATOR  = '.';
4195     private static final char       PATTERN_PER_MILLE          = '\u2030';
4196     private static final char       PATTERN_PERCENT            = '%';
4197     private static final char       PATTERN_DIGIT              = '#';
4198     private static final char       PATTERN_SEPARATOR          = ';';
4199     private static final String     PATTERN_EXPONENT           = "E";
4200     private static final char       PATTERN_MINUS              = '-';
4201 
4202     /**
4203      * The CURRENCY_SIGN is the standard Unicode symbol for currency.  It
4204      * is used in patterns and substituted with either the currency symbol,
4205      * or if it is doubled, with the international currency symbol.  If the
4206      * CURRENCY_SIGN is seen in a pattern, then the decimal separator is
4207      * replaced with the monetary decimal separator.
4208      *
4209      * The CURRENCY_SIGN is not localized.
4210      */
4211     private static final char       CURRENCY_SIGN = '\u00A4';
4212 
4213     private static final char       QUOTE = '\'';
4214 
4215     private static FieldPosition[] EmptyFieldPositionArray = new FieldPosition[0];
4216 
4217     // Upper limit on integer and fraction digits for a Java double
4218     static final int DOUBLE_INTEGER_DIGITS  = 309;
4219     static final int DOUBLE_FRACTION_DIGITS = 340;
4220 
4221     // Upper limit on integer and fraction digits for BigDecimal and BigInteger
4222     static final int MAXIMUM_INTEGER_DIGITS  = Integer.MAX_VALUE;
4223     static final int MAXIMUM_FRACTION_DIGITS = Integer.MAX_VALUE;
4224 
4225     // Proclaim JDK 1.1 serial compatibility.
4226     static final long serialVersionUID = 864413376551465018L;
4227 }