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