jdk/src/share/classes/java/text/DecimalFormat.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 7003 7d8d9506b4ee
child 12848 da701d422d2c
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7003
7d8d9506b4ee 6991380: (cal) Calendar.cachedLocaleData should be transitioned from Hashtable to ConcurrentHashMap
okutsu
parents: 6489
diff changeset
     2
 * Copyright (c) 1996, 2010, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.IOException;
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Currency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.ResourceBundle;
7003
7d8d9506b4ee 6991380: (cal) Calendar.cachedLocaleData should be transitioned from Hashtable to ConcurrentHashMap
okutsu
parents: 6489
diff changeset
    51
import java.util.concurrent.ConcurrentHashMap;
7d8d9506b4ee 6991380: (cal) Calendar.cachedLocaleData should be transitioned from Hashtable to ConcurrentHashMap
okutsu
parents: 6489
diff changeset
    52
import java.util.concurrent.ConcurrentMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.concurrent.atomic.AtomicLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.util.resources.LocaleData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>DecimalFormat</code> is a concrete subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>NumberFormat</code> that formats decimal numbers. It has a variety of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * features designed to make it possible to parse and format numbers in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * locale, including support for Western, Arabic, and Indic digits.  It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * supports different kinds of numbers, including integers (123), fixed-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * numbers (123.4), scientific notation (1.23E4), percentages (12%), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * currency amounts ($123).  All of these can be localized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>To obtain a <code>NumberFormat</code> for a specific locale, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * default locale, call one of <code>NumberFormat</code>'s factory methods, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * as <code>getInstance()</code>.  In general, do not call the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <code>DecimalFormat</code> constructors directly, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <code>NumberFormat</code> factory methods may return subclasses other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <code>DecimalFormat</code>. If you need to customize the format object, do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * something like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * NumberFormat f = NumberFormat.getInstance(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * if (f instanceof DecimalFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>A <code>DecimalFormat</code> comprises a <em>pattern</em> and a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <em>symbols</em>.  The pattern may be set directly using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <code>applyPattern()</code>, or indirectly using the API methods.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * symbols are stored in a <code>DecimalFormatSymbols</code> object.  When using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * the <code>NumberFormat</code> factory methods, the pattern and symbols are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * read from localized <code>ResourceBundle</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <h4>Patterns</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <code>DecimalFormat</code> patterns have the following syntax:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <i>Pattern:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *         <i>PositivePattern</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *         <i>PositivePattern</i> ; <i>NegativePattern</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <i>PositivePattern:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *         <i>Prefix<sub>opt</sub></i> <i>Number</i> <i>Suffix<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <i>NegativePattern:</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>Prefix:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *         any Unicode characters except &#92;uFFFE, &#92;uFFFF, and special characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <i>Suffix:</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>Number:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *         <i>Integer</i> <i>Exponent<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *         <i>Integer</i> . <i>Fraction</i> <i>Exponent<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <i>Integer:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *         <i>MinimumInteger</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *         #
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *         # <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *         # , <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <i>MinimumInteger:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *         0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *         0 <i>MinimumInteger</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *         0 , <i>MinimumInteger</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <i>Fraction:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *         <i>MinimumFraction<sub>opt</sub></i> <i>OptionalFraction<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <i>MinimumFraction:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *         0 <i>MinimumFraction<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <i>OptionalFraction:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *         # <i>OptionalFraction<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <i>Exponent:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *         E <i>MinimumExponent</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <i>MinimumExponent:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *         0 <i>MinimumExponent<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p>A <code>DecimalFormat</code> pattern contains a positive and negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * subpattern, for example, <code>"#,##0.00;(#,##0.00)"</code>.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * subpattern has a prefix, numeric part, and suffix. The negative subpattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * is optional; if absent, then the positive subpattern prefixed with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * localized minus sign (<code>'-'</code> in most locales) is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * negative subpattern. That is, <code>"0.00"</code> alone is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <code>"0.00;-0.00"</code>.  If there is an explicit negative subpattern, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * serves only to specify the negative prefix and suffix; the number of digits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * minimal digits, and other characteristics are all the same as the positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * pattern. That means that <code>"#,##0.0#;(#)"</code> produces precisely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * the same behavior as <code>"#,##0.0#;(#,##0.0#)"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <p>The prefixes, suffixes, and various symbols used for infinity, digits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * thousands separators, decimal separators, etc. may be set to arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * values, and they will appear properly during formatting.  However, care must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * be taken that the symbols and strings do not conflict, or parsing will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * unreliable.  For example, either the positive and negative prefixes or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * suffixes must be distinct for <code>DecimalFormat.parse()</code> to be able
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * to distinguish positive from negative values.  (If they are identical, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <code>DecimalFormat</code> will behave as if no negative subpattern was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * specified.)  Another example is that the decimal separator and thousands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * separator should be distinct characters, or parsing will be impossible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <p>The grouping separator is commonly used for thousands, but in some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * countries it separates ten-thousands. The grouping size is a constant number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * of digits between the grouping characters, such as 3 for 100,000,000 or 4 for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * 1,0000,0000.  If you supply a pattern with multiple grouping characters, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * interval between the last one and the end of the integer is the one that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * used. So <code>"#,##,###,####"</code> == <code>"######,####"</code> ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <code>"##,####,####"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <h4>Special Pattern Characters</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <p>Many characters in a pattern are taken literally; they are matched during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * parsing and output unchanged during formatting.  Special characters, on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * other hand, stand for other characters, strings, or classes of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * They must be quoted, unless noted otherwise, if they are to appear in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * prefix or suffix as literals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <p>The characters listed here are used in non-localized patterns.  Localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * patterns use the corresponding characters taken from this formatter's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <code>DecimalFormatSymbols</code> object instead, and these characters lose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * their special status.  Two exceptions are the currency sign and quote, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * are not localized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * <table border=0 cellspacing=3 cellpadding=0 summary="Chart showing symbol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *  location, localized, and meaning.">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *     <tr bgcolor="#ccccff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *          <th align=left>Symbol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *          <th align=left>Location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *          <th align=left>Localized?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *          <th align=left>Meaning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *     <tr valign=top>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *          <td><code>0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *          <td>Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *          <td>Digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *     <tr valign=top bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *          <td><code>#</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *          <td>Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *          <td>Digit, zero shows as absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *     <tr valign=top>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *          <td><code>.</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *          <td>Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *          <td>Decimal separator or monetary decimal separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *     <tr valign=top bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *          <td><code>-</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *          <td>Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *          <td>Minus sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *     <tr valign=top>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *          <td><code>,</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *          <td>Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *          <td>Grouping separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *     <tr valign=top bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *          <td><code>E</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *          <td>Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *          <td>Separates mantissa and exponent in scientific notation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *              <em>Need not be quoted in prefix or suffix.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *     <tr valign=top>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *          <td><code>;</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *          <td>Subpattern boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *          <td>Separates positive and negative subpatterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *     <tr valign=top bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *          <td><code>%</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *          <td>Prefix or suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *          <td>Multiply by 100 and show as percentage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *     <tr valign=top>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *          <td><code>&#92;u2030</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *          <td>Prefix or suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *          <td>Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *          <td>Multiply by 1000 and show as per mille value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *     <tr valign=top bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *          <td><code>&#164;</code> (<code>&#92;u00A4</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *          <td>Prefix or suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *          <td>No
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *          <td>Currency sign, replaced by currency symbol.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *              doubled, replaced by international currency symbol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *              If present in a pattern, the monetary decimal separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *              is used instead of the decimal separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *     <tr valign=top>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *          <td><code>'</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *          <td>Prefix or suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *          <td>No
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *          <td>Used to quote special characters in a prefix or suffix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *              for example, <code>"'#'#"</code> formats 123 to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *              <code>"#123"</code>.  To create a single quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *              itself, use two in a row: <code>"# o''clock"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * <h4>Scientific Notation</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * <p>Numbers in scientific notation are expressed as the product of a mantissa
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * and a power of ten, for example, 1234 can be expressed as 1.234 x 10^3.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * mantissa is often in the range 1.0 <= x < 10.0, but it need not be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * <code>DecimalFormat</code> can be instructed to format and parse scientific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * notation <em>only via a pattern</em>; there is currently no factory method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * that creates a scientific notation format.  In a pattern, the exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * character immediately followed by one or more digit characters indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * scientific notation.  Example: <code>"0.###E0"</code> formats the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * 1234 as <code>"1.234E3"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * <li>The number of digit characters after the exponent character gives the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * minimum exponent digit count.  There is no maximum.  Negative exponents are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * formatted using the localized minus sign, <em>not</em> the prefix and suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * from the pattern.  This allows patterns such as <code>"0.###E0 m/s"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * <li>The minimum and maximum number of integer digits are interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * together:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * <li>If the maximum number of integer digits is greater than their minimum number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * and greater than 1, it forces the exponent to be a multiple of the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * number of integer digits, and the minimum number of integer digits to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 * interpreted as 1.  The most common use of this is to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * <em>engineering notation</em>, in which the exponent is a multiple of three,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * e.g., <code>"##0.#####E0"</code>. Using this pattern, the number 12345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * formats to <code>"12.345E3"</code>, and 123456 formats to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * <code>"123.456E3"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 * <li>Otherwise, the minimum number of integer digits is achieved by adjusting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * exponent.  Example: 0.00123 formatted with <code>"00.###E0"</code> yields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * <code>"12.3E-4"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * <li>The number of significant digits in the mantissa is the sum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * <em>minimum integer</em> and <em>maximum fraction</em> digits, and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * unaffected by the maximum integer digits.  For example, 12345 formatted with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * <code>"##0.##E0"</code> is <code>"12.3E3"</code>. To show all digits, set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * the significant digits count to zero.  The number of significant digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 * does not affect parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * <li>Exponential patterns may not contain grouping separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * <h4>Rounding</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 * <code>DecimalFormat</code> provides rounding modes defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 * {@link java.math.RoundingMode} for formatting.  By default, it uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * {@link java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * <h4>Digits</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * For formatting, <code>DecimalFormat</code> uses the ten consecutive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * characters starting with the localized zero digit defined in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * <code>DecimalFormatSymbols</code> object as digits. For parsing, these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * digits as well as all Unicode decimal digits, as defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * {@link Character#digit Character.digit}, are recognized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * <h4>Special Values</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * <p><code>NaN</code> is formatted as a string, which typically has a single character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 * <code>&#92;uFFFD</code>.  This string is determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * <code>DecimalFormatSymbols</code> object.  This is the only value for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * the prefixes and suffixes are not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * <p>Infinity is formatted as a string, which typically has a single character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * <code>&#92;u221E</code>, with the positive or negative prefixes and suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 * applied.  The infinity string is determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 * <code>DecimalFormatSymbols</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 * <p>Negative zero (<code>"-0"</code>) parses to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 * <li><code>BigDecimal(0)</code> if <code>isParseBigDecimal()</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 * true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 * <li><code>Long(0)</code> if <code>isParseBigDecimal()</code> is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 *     and <code>isParseIntegerOnly()</code> is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * <li><code>Double(-0.0)</code> if both <code>isParseBigDecimal()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * and <code>isParseIntegerOnly()</code> are false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 * <h4><a name="synchronization">Synchronization</a></h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * Decimal formats are generally not synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * It is recommended to create separate format instances for each thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 * If multiple threads access a format concurrently, it must be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 * externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 * <h4>Example</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 * <strong>// Print out a number using the localized number, integer, currency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 * // and percent format for each locale</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 * Locale[] locales = NumberFormat.getAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 * double myNumber = -1234.56;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 * NumberFormat form;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
 * for (int j=0; j<4; ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
 *     System.out.println("FORMAT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
 *     for (int i = 0; i < locales.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
 *         if (locales[i].getCountry().length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 *            continue; // Skip language-only locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 *         System.out.print(locales[i].getDisplayName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 *         switch (j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 *         case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
 *             form = NumberFormat.getInstance(locales[i]); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 *         case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
 *             form = NumberFormat.getIntegerInstance(locales[i]); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 *         case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 *             form = NumberFormat.getCurrencyInstance(locales[i]); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 *         default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
 *             form = NumberFormat.getPercentInstance(locales[i]); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
 *         if (form instanceof DecimalFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
 *             System.out.print(": " + ((DecimalFormat) form).toPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 *         System.out.print(" -> " + form.format(myNumber));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 *             System.out.println(" -> " + form.parse(form.format(myNumber)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
 *         } catch (ParseException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 * @see          <a href="http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html">Java Tutorial</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 * @see          NumberFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 * @see          DecimalFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 * @see          ParsePosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 * @author       Mark Davis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * @author       Alan Liu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
public class DecimalFormat extends NumberFormat {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Creates a DecimalFormat using the default pattern and symbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * for the default locale. This is a convenient way to obtain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * DecimalFormat when internationalization is not the main concern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * To obtain standard formats for a given locale, use the factory methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * on NumberFormat such as getNumberInstance. These factories will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * return the most appropriate sub-class of NumberFormat for a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @see java.text.NumberFormat#getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @see java.text.NumberFormat#getNumberInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @see java.text.NumberFormat#getCurrencyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @see java.text.NumberFormat#getPercentInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public DecimalFormat() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   396
        Locale def = Locale.getDefault(Locale.Category.FORMAT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // try to get the pattern from the cache
7003
7d8d9506b4ee 6991380: (cal) Calendar.cachedLocaleData should be transitioned from Hashtable to ConcurrentHashMap
okutsu
parents: 6489
diff changeset
   398
        String pattern = cachedLocaleData.get(def);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (pattern == null) {  /* cache miss */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            // Get the pattern for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            ResourceBundle rb = LocaleData.getNumberFormatData(def);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            String[] all = rb.getStringArray("NumberPatterns");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            pattern = all[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            /* update cache */
7003
7d8d9506b4ee 6991380: (cal) Calendar.cachedLocaleData should be transitioned from Hashtable to ConcurrentHashMap
okutsu
parents: 6489
diff changeset
   405
            cachedLocaleData.putIfAbsent(def, pattern);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // Always applyPattern after the symbols are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        this.symbols = new DecimalFormatSymbols(def);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        applyPattern(pattern, false);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * for the default locale. This is a convenient way to obtain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * DecimalFormat when internationalization is not the main concern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * To obtain standard formats for a given locale, use the factory methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * on NumberFormat such as getNumberInstance. These factories will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * return the most appropriate sub-class of NumberFormat for a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param pattern A non-localized pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @exception NullPointerException if <code>pattern</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @exception IllegalArgumentException if the given pattern is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @see java.text.NumberFormat#getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @see java.text.NumberFormat#getNumberInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @see java.text.NumberFormat#getCurrencyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @see java.text.NumberFormat#getPercentInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public DecimalFormat(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        // Always applyPattern after the symbols are set
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   434
        this.symbols = new DecimalFormatSymbols(Locale.getDefault(Locale.Category.FORMAT));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        applyPattern(pattern, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
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
     * Creates a DecimalFormat using the given pattern and symbols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Use this constructor when you need to completely customize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * behavior of the format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * To obtain standard formats for a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * locale, use the factory methods on NumberFormat such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * getInstance or getCurrencyInstance. If you need only minor adjustments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * to a standard format, you can modify the format returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * a NumberFormat factory method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param pattern a non-localized pattern string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param symbols the set of symbols to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @exception NullPointerException if any of the given arguments is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @exception IllegalArgumentException if the given pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @see java.text.NumberFormat#getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @see java.text.NumberFormat#getNumberInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @see java.text.NumberFormat#getCurrencyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @see java.text.NumberFormat#getPercentInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @see java.text.DecimalFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public DecimalFormat (String pattern, DecimalFormatSymbols symbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // Always applyPattern after the symbols are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        this.symbols = (DecimalFormatSymbols)symbols.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        applyPattern(pattern, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    // Overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Formats a number and appends the resulting text to the given string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * The number can be of any subclass of {@link java.lang.Number}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * This implementation uses the maximum precision permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @param number     the number to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @param toAppendTo the <code>StringBuffer</code> to which the formatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *                   text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param pos        On input: an alignment field, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *                   On output: the offsets of the alignment field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @return           the value passed in as <code>toAppendTo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @exception        IllegalArgumentException if <code>number</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *                   null or not an instance of <code>Number</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @exception        NullPointerException if <code>toAppendTo</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *                   <code>pos</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @exception        ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *                   mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @see              java.text.FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public final StringBuffer format(Object number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                     StringBuffer toAppendTo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                     FieldPosition pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (number instanceof Long || number instanceof Integer ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                   number instanceof Short || number instanceof Byte ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                   number instanceof AtomicInteger ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                   number instanceof AtomicLong ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                   (number instanceof BigInteger &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    ((BigInteger)number).bitLength () < 64)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return format(((Number)number).longValue(), toAppendTo, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } else if (number instanceof BigDecimal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return format((BigDecimal)number, toAppendTo, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        } else if (number instanceof BigInteger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            return format((BigInteger)number, toAppendTo, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        } else if (number instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            return format(((Number)number).doubleValue(), toAppendTo, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            throw new IllegalArgumentException("Cannot format given Object as a Number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Formats a double to produce a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @param number    The double to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param result    where the text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param fieldPosition    On input: an alignment field, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * On output: the offsets of the alignment field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @exception ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *            mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @return The formatted number string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @see java.text.FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public StringBuffer format(double number, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                               FieldPosition fieldPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        fieldPosition.setBeginIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        fieldPosition.setEndIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        return format(number, result, fieldPosition.getFieldDelegate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Formats a double to produce a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @param number    The double to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param result    where the text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @param delegate notified of locations of sub fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @exception       ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *                  mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @return The formatted number string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    private StringBuffer format(double number, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                                FieldDelegate delegate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (Double.isNaN(number) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
           (Double.isInfinite(number) && multiplier == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            int iFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            result.append(symbols.getNaN());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                               iFieldStart, result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        /* Detecting whether a double is negative is easy with the exception of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
         * the value -0.0.  This is a double which has a zero mantissa (and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
         * exponent), but a negative sign bit.  It is semantically distinct from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
         * a zero with a positive sign bit, and this distinction is important
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
         * to certain kinds of computations.  However, it's a little tricky to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
         * detect, since (-0.0 == 0.0) and !(-0.0 < 0.0).  How then, you may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
         * ask, does it behave distinctly from +0.0?  Well, 1/(-0.0) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         * -Infinity.  Proper detection of -0.0 is needed to deal with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
         * issues raised by bugs 4106658, 4106667, and 4147706.  Liu 7/6/98.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        boolean isNegative = ((number < 0.0) || (number == 0.0 && 1/number < 0.0)) ^ (multiplier < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (multiplier != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            number *= multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (Double.isInfinite(number)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                append(result, negativePrefix, delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                       getNegativePrefixFieldPositions(), Field.SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                append(result, positivePrefix, delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                       getPositivePrefixFieldPositions(), Field.SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            int iFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            result.append(symbols.getInfinity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                               iFieldStart, result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                append(result, negativeSuffix, delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                       getNegativeSuffixFieldPositions(), Field.SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                append(result, positiveSuffix, delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                       getPositiveSuffixFieldPositions(), Field.SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            number = -number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // at this point we are guaranteed a nonnegative finite number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        assert(number >= 0 && !Double.isInfinite(number));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        synchronized(digitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            int maxIntDigits = super.getMaximumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            int minIntDigits = super.getMinimumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            int maxFraDigits = super.getMaximumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            int minFraDigits = super.getMinimumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            digitList.set(isNegative, number, useExponentialNotation ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                          maxIntDigits + maxFraDigits : maxFraDigits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                          !useExponentialNotation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            return subformat(result, delegate, isNegative, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                       maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Format a long to produce a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @param number    The long to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @param result    where the text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @param fieldPosition    On input: an alignment field, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * On output: the offsets of the alignment field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @exception       ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *                  mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @return The formatted number string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @see java.text.FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public StringBuffer format(long number, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                               FieldPosition fieldPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        fieldPosition.setBeginIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        fieldPosition.setEndIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return format(number, result, fieldPosition.getFieldDelegate());
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 delegate notified of locations of sub fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @return The formatted number string
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
     * @see java.text.FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    private StringBuffer format(long number, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                               FieldDelegate delegate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        boolean isNegative = (number < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            number = -number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        // In general, long values always represent real finite numbers, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // we don't have to check for +/- Infinity or NaN.  However, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        // is one case we have to be careful of:  The multiplier can push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        // a number near MIN_VALUE or MAX_VALUE outside the legal range.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        // check for this before multiplying, and if it happens we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        // BigInteger instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        boolean useBigInteger = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (number < 0) { // This can only happen if number == Long.MIN_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            if (multiplier != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                useBigInteger = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        } else if (multiplier != 1 && multiplier != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            long cutoff = Long.MAX_VALUE / multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (cutoff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                cutoff = -cutoff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            useBigInteger = (number > cutoff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (useBigInteger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                number = -number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            BigInteger bigIntegerValue = BigInteger.valueOf(number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return format(bigIntegerValue, result, delegate, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        number *= multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (number == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            isNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            if (multiplier < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                number = -number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                isNegative = !isNegative;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        synchronized(digitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            int maxIntDigits = super.getMaximumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            int minIntDigits = super.getMinimumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            int maxFraDigits = super.getMaximumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            int minFraDigits = super.getMinimumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            digitList.set(isNegative, number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                     useExponentialNotation ? maxIntDigits + maxFraDigits : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return subformat(result, delegate, isNegative, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                       maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Formats a BigDecimal to produce a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @param number    The BigDecimal to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @param result    where the text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @param fieldPosition    On input: an alignment field, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * On output: the offsets of the alignment field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @return The formatted number string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @exception        ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *                   mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @see java.text.FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    private StringBuffer format(BigDecimal number, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                FieldPosition fieldPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        fieldPosition.setBeginIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        fieldPosition.setEndIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        return format(number, result, fieldPosition.getFieldDelegate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Formats a BigDecimal to produce a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @param number    The BigDecimal to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @param result    where the text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @param delegate notified of locations of sub fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @exception        ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *                   mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @return The formatted number string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    private StringBuffer format(BigDecimal number, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                FieldDelegate delegate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (multiplier != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            number = number.multiply(getBigDecimalMultiplier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        boolean isNegative = number.signum() == -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            number = number.negate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        synchronized(digitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            int maxIntDigits = getMaximumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            int minIntDigits = getMinimumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            int maxFraDigits = getMaximumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            int minFraDigits = getMinimumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            int maximumDigits = maxIntDigits + maxFraDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            digitList.set(isNegative, number, useExponentialNotation ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                ((maximumDigits < 0) ? Integer.MAX_VALUE : maximumDigits) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                maxFraDigits, !useExponentialNotation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            return subformat(result, delegate, isNegative, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Format a BigInteger to produce a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @param number    The BigInteger to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @param result    where the text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @param fieldPosition    On input: an alignment field, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * On output: the offsets of the alignment field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @return The formatted number string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @exception        ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *                   mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @see java.text.FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    private StringBuffer format(BigInteger number, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                               FieldPosition fieldPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        fieldPosition.setBeginIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        fieldPosition.setEndIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return format(number, result, fieldPosition.getFieldDelegate(), false);
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
     * Format a BigInteger to produce a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @param number    The BigInteger to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @param result    where the text is to be appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @param delegate notified of locations of sub fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @return The formatted number string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @exception        ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *                   mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @see java.text.FieldPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    private StringBuffer format(BigInteger number, StringBuffer result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                               FieldDelegate delegate, boolean formatLong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (multiplier != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            number = number.multiply(getBigIntegerMultiplier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        boolean isNegative = number.signum() == -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            number = number.negate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        synchronized(digitList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            int maxIntDigits, minIntDigits, maxFraDigits, minFraDigits, maximumDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (formatLong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                maxIntDigits = super.getMaximumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                minIntDigits = super.getMinimumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                maxFraDigits = super.getMaximumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                minFraDigits = super.getMinimumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                maximumDigits = maxIntDigits + maxFraDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                maxIntDigits = getMaximumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                minIntDigits = getMinimumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                maxFraDigits = getMaximumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                minFraDigits = getMinimumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                maximumDigits = maxIntDigits + maxFraDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                if (maximumDigits < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    maximumDigits = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            digitList.set(isNegative, number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                          useExponentialNotation ? maximumDigits : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return subformat(result, delegate, isNegative, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Formats an Object producing an <code>AttributedCharacterIterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * You can use the returned <code>AttributedCharacterIterator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * to build the resulting String, as well as to determine information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * about the resulting String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * Each attribute key of the AttributedCharacterIterator will be of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * <code>NumberFormat.Field</code>, with the attribute value being the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * same as the attribute key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @exception NullPointerException if obj is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @exception IllegalArgumentException when the Format cannot format the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *            given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @exception        ArithmeticException if rounding is needed with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *                   mode being set to RoundingMode.UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * @param obj The object to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @return AttributedCharacterIterator describing the formatted value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        CharacterIteratorFieldDelegate delegate =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                         new CharacterIteratorFieldDelegate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (obj instanceof Double || obj instanceof Float) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            format(((Number)obj).doubleValue(), sb, delegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        } else if (obj instanceof Long || obj instanceof Integer ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                   obj instanceof Short || obj instanceof Byte ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                   obj instanceof AtomicInteger || obj instanceof AtomicLong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            format(((Number)obj).longValue(), sb, delegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        } else if (obj instanceof BigDecimal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            format((BigDecimal)obj, sb, delegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        } else if (obj instanceof BigInteger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            format((BigInteger)obj, sb, delegate, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        } else if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                "formatToCharacterIterator must be passed non-null object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                "Cannot format given Object as a Number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        return delegate.getIterator(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * Complete the formatting of a finite number.  On entry, the digitList must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * be filled in with the correct digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    private StringBuffer subformat(StringBuffer result, FieldDelegate delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                                   boolean isNegative, boolean isInteger,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                                   int maxIntDigits, int minIntDigits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                   int maxFraDigits, int minFraDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        // NOTE: This isn't required anymore because DigitList takes care of this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        //  // The negative of the exponent represents the number of leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        //  // zeros between the decimal and the first non-zero digit, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        //  // a value < 0.1 (e.g., for 0.00123, -fExponent == 2).  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        //  // is more than the maximum fraction digits, then we have an underflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        //  // for the printed representation.  We recognize this here and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        //  // the DigitList representation to zero in this situation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        //  if (-digitList.decimalAt >= getMaximumFractionDigits())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        //  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        //      digitList.count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        //  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        char zero = symbols.getZeroDigit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        int zeroDelta = zero - '0'; // '0' is the DigitList representation of zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        char grouping = symbols.getGroupingSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        char decimal = isCurrencyFormat ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            symbols.getMonetaryDecimalSeparator() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            symbols.getDecimalSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        /* Per bug 4147706, DecimalFormat must respect the sign of numbers which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         * format as zero.  This allows sensible computations and preserves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * relations such as signum(1/x) = signum(x), where x is +Infinity or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * -Infinity.  Prior to this fix, we always formatted zero values as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         * they were positive.  Liu 7/6/98.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        if (digitList.isZero()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            digitList.decimalAt = 0; // Normalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            append(result, negativePrefix, delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                   getNegativePrefixFieldPositions(), Field.SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            append(result, positivePrefix, delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                   getPositivePrefixFieldPositions(), Field.SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (useExponentialNotation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            int iFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            int iFieldEnd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            int fFieldStart = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            // Minimum integer digits are handled in exponential format by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            // adjusting the exponent.  For example, 0.01234 with 3 minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            // integer digits is "123.4E-4".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            // Maximum integer digits are interpreted as indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            // repeating range.  This is useful for engineering notation, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            // which the exponent is restricted to a multiple of 3.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            // example, 0.01234 with 3 maximum integer digits is "12.34e-3".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            // If maximum integer digits are > 1 and are larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            // minimum integer digits, then minimum integer digits are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            // ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            int exponent = digitList.decimalAt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            int repeat = maxIntDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            int minimumIntegerDigits = minIntDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            if (repeat > 1 && repeat > minIntDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                // A repeating range is defined; adjust to it as follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                // If repeat == 3, we have 6,5,4=>3; 3,2,1=>0; 0,-1,-2=>-3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                // -3,-4,-5=>-6, etc. This takes into account that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                // exponent we have here is off by one from what we expect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                // it is for the format 0.MMMMMx10^n.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                if (exponent >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    exponent = ((exponent - 1) / repeat) * repeat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    // integer division rounds towards 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    exponent = ((exponent - repeat) / repeat) * repeat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                minimumIntegerDigits = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                // No repeating range is defined; use minimum integer digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                exponent -= minimumIntegerDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            // We now output a minimum number of digits, and more if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            // are more digits, up to the maximum number of digits.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            // place the decimal point after the "integer" digits, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            // are the first (decimalAt - exponent) digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            int minimumDigits = minIntDigits + minFraDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            if (minimumDigits < 0) {    // overflow?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                minimumDigits = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            // The number of integer digits is handled specially if the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            // is zero, since then there may be no digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            int integerDigits = digitList.isZero() ? minimumIntegerDigits :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    digitList.decimalAt - exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (minimumDigits < integerDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                minimumDigits = integerDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            int totalDigits = digitList.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            if (minimumDigits > totalDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                totalDigits = minimumDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            boolean addedDecimalSeparator = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            for (int i=0; i<totalDigits; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                if (i == integerDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    // Record field information for caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    iFieldEnd = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    result.append(decimal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    addedDecimalSeparator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    // Record field information for caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    fFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                result.append((i < digitList.count) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                              (char)(digitList.digits[i] + zeroDelta) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                              zero);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            if (decimalSeparatorAlwaysShown && totalDigits == integerDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                // Record field information for caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                iFieldEnd = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                result.append(decimal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                addedDecimalSeparator = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                // Record field information for caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                fFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            // Record field information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (iFieldEnd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                iFieldEnd = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                               iFieldStart, iFieldEnd, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            if (addedDecimalSeparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                delegate.formatted(Field.DECIMAL_SEPARATOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                                   Field.DECIMAL_SEPARATOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                                   iFieldEnd, fFieldStart, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            if (fFieldStart == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                fFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            delegate.formatted(FRACTION_FIELD, Field.FRACTION, Field.FRACTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                               fFieldStart, result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            // The exponent is output using the pattern-specified minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            // exponent digits.  There is no maximum limit to the exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            // digits, since truncating the exponent would result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            // unacceptable inaccuracy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            int fieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            result.append(symbols.getExponentSeparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            delegate.formatted(Field.EXPONENT_SYMBOL, Field.EXPONENT_SYMBOL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                               fieldStart, result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            // For zero values, we force the exponent to zero.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            // must do this here, and not earlier, because the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            // is used to determine integer digit count above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            if (digitList.isZero()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                exponent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            boolean negativeExponent = exponent < 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            if (negativeExponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                exponent = -exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                fieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                result.append(symbols.getMinusSign());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                delegate.formatted(Field.EXPONENT_SIGN, Field.EXPONENT_SIGN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                                   fieldStart, result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            digitList.set(negativeExponent, exponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            int eFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            for (int i=digitList.decimalAt; i<minExponentDigits; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                result.append(zero);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            for (int i=0; i<digitList.decimalAt; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                result.append((i < digitList.count) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                          (char)(digitList.digits[i] + zeroDelta) : zero);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            delegate.formatted(Field.EXPONENT, Field.EXPONENT, eFieldStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                               result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            int iFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            // Output the integer portion.  Here 'count' is the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            // number of integer digits we will display, including both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            // leading zeros required to satisfy getMinimumIntegerDigits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            // and actual digits present in the number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            int count = minIntDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            int digitIndex = 0; // Index into digitList.fDigits[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            if (digitList.decimalAt > 0 && count < digitList.decimalAt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                count = digitList.decimalAt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            // Handle the case where getMaximumIntegerDigits() is smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            // than the real number of integer digits.  If this is so, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            // output the least significant max integer digits.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            // the value 1997 printed with 2 max integer digits is just "97".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            if (count > maxIntDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                count = maxIntDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                digitIndex = digitList.decimalAt - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            int sizeBeforeIntegerPart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            for (int i=count-1; i>=0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                if (i < digitList.decimalAt && digitIndex < digitList.count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    // Output a real digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    result.append((char)(digitList.digits[digitIndex++] + zeroDelta));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    // Output a leading zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    result.append(zero);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                // Output grouping separator if necessary.  Don't output a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                // grouping separator if i==0 though; that's at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                // the integer part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                if (isGroupingUsed() && i>0 && (groupingSize != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                    (i % groupingSize == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    int gStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    result.append(grouping);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    delegate.formatted(Field.GROUPING_SEPARATOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                                       Field.GROUPING_SEPARATOR, gStart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                                       result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            // Determine whether or not there are any printable fractional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            // digits.  If we've used up the digits we know there aren't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            boolean fractionPresent = (minFraDigits > 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                (!isInteger && digitIndex < digitList.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            // If there is no fraction present, and we haven't printed any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            // integer digits, then print a zero.  Otherwise we won't print
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            // _any_ digits, and we won't be able to parse this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            if (!fractionPresent && result.length() == sizeBeforeIntegerPart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                result.append(zero);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                               iFieldStart, result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            // Output the decimal separator if we always do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            int sStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            if (decimalSeparatorAlwaysShown || fractionPresent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                result.append(decimal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            if (sStart != result.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                delegate.formatted(Field.DECIMAL_SEPARATOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                                   Field.DECIMAL_SEPARATOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                                   sStart, result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            int fFieldStart = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            for (int i=0; i < maxFraDigits; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                // Here is where we escape from the loop.  We escape if we've
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                // output the maximum fraction digits (specified in the for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                // expression above).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                // We also stop when we've output the minimum digits and either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                // we have an integer, so there is no fractional stuff to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                // display, or we're out of significant digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                if (i >= minFraDigits &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    (isInteger || digitIndex >= digitList.count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                // Output leading fractional zeros. These are zeros that come
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                // after the decimal but before any significant digits. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                // are only output if abs(number being formatted) < 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                if (-1-i > (digitList.decimalAt-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    result.append(zero);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                // Output a digit, if we have any precision left, or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                // zero if we don't.  We don't want to output noise digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                if (!isInteger && digitIndex < digitList.count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    result.append((char)(digitList.digits[digitIndex++] + zeroDelta));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                    result.append(zero);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            // Record field information for caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            delegate.formatted(FRACTION_FIELD, Field.FRACTION, Field.FRACTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                               fFieldStart, result.length(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        if (isNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            append(result, negativeSuffix, delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                   getNegativeSuffixFieldPositions(), Field.SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            append(result, positiveSuffix, delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                   getPositiveSuffixFieldPositions(), Field.SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Appends the String <code>string</code> to <code>result</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * <code>delegate</code> is notified of all  the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * <code>FieldPosition</code>s in <code>positions</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * If one of the <code>FieldPosition</code>s in <code>positions</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * identifies a <code>SIGN</code> attribute, it is mapped to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <code>signAttribute</code>. This is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * to map the <code>SIGN</code> attribute to the <code>EXPONENT</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * attribute as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * This is used by <code>subformat</code> to add the prefix/suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    private void append(StringBuffer result, String string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                        FieldDelegate delegate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                        FieldPosition[] positions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                        Format.Field signAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        int start = result.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        if (string.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            result.append(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            for (int counter = 0, max = positions.length; counter < max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                 counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                FieldPosition fp = positions[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                Format.Field attribute = fp.getFieldAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                if (attribute == Field.SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    attribute = signAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                delegate.formatted(attribute, attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                                   start + fp.getBeginIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                                   start + fp.getEndIndex(), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * Parses text from a string to produce a <code>Number</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * The method attempts to parse text starting at the index given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * <code>pos</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * If parsing succeeds, then the index of <code>pos</code> is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * to the index after the last character used (parsing does not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * use all characters up to the end of the string), and the parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * number is returned. The updated <code>pos</code> can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * indicate the starting point for the next call to this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * If an error occurs, then the index of <code>pos</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * changed, the error index of <code>pos</code> is set to the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * the character where the error occurred, and null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * The subclass returned depends on the value of {@link #isParseBigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * as well as on the string being parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *   <li>If <code>isParseBigDecimal()</code> is false (the default),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *       most integer values are returned as <code>Long</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     *       objects, no matter how they are written: <code>"17"</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *       <code>"17.000"</code> both parse to <code>Long(17)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *       Values that cannot fit into a <code>Long</code> are returned as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *       <code>Double</code>s. This includes values with a fractional part,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     *       infinite values, <code>NaN</code>, and the value -0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     *       <code>DecimalFormat</code> does <em>not</em> decide whether to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *       return a <code>Double</code> or a <code>Long</code> based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *       presence of a decimal separator in the source string. Doing so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *       would prevent integers that overflow the mantissa of a double,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *       such as <code>"-9,223,372,036,854,775,808.00"</code>, from being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *       parsed accurately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *       <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *       Callers may use the <code>Number</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *       <code>doubleValue</code>, <code>longValue</code>, etc., to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *       the type they want.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *   <li>If <code>isParseBigDecimal()</code> is true, values are returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     *       as <code>BigDecimal</code> objects. The values are the ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *       constructed by {@link java.math.BigDecimal#BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *       for corresponding strings in locale-independent format. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     *       special cases negative and positive infinity and NaN are returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *       as <code>Double</code> instances holding the values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *       corresponding <code>Double</code> constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * <code>DecimalFormat</code> parses all Unicode characters that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * decimal digits, as defined by <code>Character.digit()</code>. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * addition, <code>DecimalFormat</code> also recognizes as digits the ten
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * consecutive characters starting with the localized zero digit defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * the <code>DecimalFormatSymbols</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * @param text the string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @param pos  A <code>ParsePosition</code> object with index and error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *             index information as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @return     the parsed value, or <code>null</code> if the parse fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * @exception  NullPointerException if <code>text</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *             <code>pos</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    public Number parse(String text, ParsePosition pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        // special case NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            pos.index = pos.index + symbols.getNaN().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            return new Double(Double.NaN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        boolean[] status = new boolean[STATUS_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        // special case INFINITY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        if (status[STATUS_INFINITE]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            if (status[STATUS_POSITIVE] == (multiplier >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                return new Double(Double.POSITIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                return new Double(Double.NEGATIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        if (multiplier == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            if (digitList.isZero()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                return new Double(Double.NaN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            } else if (status[STATUS_POSITIVE]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                return new Double(Double.POSITIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                return new Double(Double.NEGATIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        if (isParseBigDecimal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            BigDecimal bigDecimalResult = digitList.getBigDecimal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            if (multiplier != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                catch (ArithmeticException e) {  // non-terminating decimal expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                    bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier(), roundingMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            if (!status[STATUS_POSITIVE]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                bigDecimalResult = bigDecimalResult.negate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            return bigDecimalResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            boolean gotDouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            boolean gotLongMinimum = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            double  doubleResult = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            long    longResult = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            // Finally, have DigitList parse the digits into a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            if (digitList.fitsIntoLong(status[STATUS_POSITIVE], isParseIntegerOnly())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                gotDouble = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                longResult = digitList.getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                if (longResult < 0) {  // got Long.MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                    gotLongMinimum = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                doubleResult = digitList.getDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            // Divide by multiplier. We have to be careful here not to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            // unneeded conversions between double and long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            if (multiplier != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                if (gotDouble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    doubleResult /= multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    // Avoid converting to double if we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    if (longResult % multiplier == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                        longResult /= multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                        doubleResult = ((double)longResult) / multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                        gotDouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            if (!status[STATUS_POSITIVE] && !gotLongMinimum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                doubleResult = -doubleResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                longResult = -longResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            // At this point, if we divided the result by the multiplier, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            // result may fit into a long.  We check for this case and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            // a long if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            // We must do this AFTER applying the negative (if appropriate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            // in order to handle the case of LONG_MIN; otherwise, if we do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            // this with a positive value -LONG_MIN, the double is > 0, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            // the long is < 0. We also must retain a double in the case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            // -0.0, which will compare as == to a long 0 cast to a double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            // (bug 4162852).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            if (multiplier != 1 && gotDouble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                longResult = (long)doubleResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                gotDouble = ((doubleResult != (double)longResult) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                            (doubleResult == 0.0 && 1/doubleResult < 0.0)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                            !isParseIntegerOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            return gotDouble ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                (Number)new Double(doubleResult) : (Number)new Long(longResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Return a BigInteger multiplier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    private BigInteger getBigIntegerMultiplier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        if (bigIntegerMultiplier == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            bigIntegerMultiplier = BigInteger.valueOf(multiplier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        return bigIntegerMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    private transient BigInteger bigIntegerMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * Return a BigDecimal multiplier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    private BigDecimal getBigDecimalMultiplier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        if (bigDecimalMultiplier == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            bigDecimalMultiplier = new BigDecimal(multiplier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        return bigDecimalMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    private transient BigDecimal bigDecimalMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    private static final int STATUS_INFINITE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    private static final int STATUS_POSITIVE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    private static final int STATUS_LENGTH   = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * Parse the given text into a number.  The text is parsed beginning at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * parsePosition, until an unparseable character is seen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @param text The string to parse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * @param parsePosition The position at which to being parsing.  Upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * return, the first unparseable character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @param digits The DigitList to set to the parsed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @param isExponent If true, parse an exponent.  This means no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * infinite values and integer only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * @param status Upon return contains boolean status flags indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * whether the value was infinite and whether it was positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    private final boolean subparse(String text, ParsePosition parsePosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                   String positivePrefix, String negativePrefix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                   DigitList digits, boolean isExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                   boolean status[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        int position = parsePosition.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        int oldStart = parsePosition.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        int backup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        boolean gotPositive, gotNegative;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        // check for positivePrefix; take longest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        gotPositive = text.regionMatches(position, positivePrefix, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                                         positivePrefix.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        gotNegative = text.regionMatches(position, negativePrefix, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                                         negativePrefix.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        if (gotPositive && gotNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            if (positivePrefix.length() > negativePrefix.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                gotNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            } else if (positivePrefix.length() < negativePrefix.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                gotPositive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        if (gotPositive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            position += positivePrefix.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        } else if (gotNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            position += negativePrefix.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            parsePosition.errorIndex = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        // process digits or Inf, find decimal position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        status[STATUS_INFINITE] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        if (!isExponent && text.regionMatches(position,symbols.getInfinity(),0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                          symbols.getInfinity().length())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            position += symbols.getInfinity().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            status[STATUS_INFINITE] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            // We now have a string of digits, possibly with grouping symbols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            // and decimal points.  We want to process these into a DigitList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            // We don't want to put a bunch of leading zeros into the DigitList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            // though, so we keep track of the location of the decimal point,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            // put only significant digits into the DigitList, and adjust the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            // exponent as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            digits.decimalAt = digits.count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            char zero = symbols.getZeroDigit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            char decimal = isCurrencyFormat ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                symbols.getMonetaryDecimalSeparator() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                symbols.getDecimalSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            char grouping = symbols.getGroupingSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            String exponentString = symbols.getExponentSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            boolean sawDecimal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            boolean sawExponent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            boolean sawDigit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            int exponent = 0; // Set to the exponent value, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            // We have to track digitCount ourselves, because digits.count will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            // pin when the maximum allowable digits is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            int digitCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            backup = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            for (; position < text.length(); ++position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                char ch = text.charAt(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                /* We recognize all digit ranges, not only the Latin digit range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                 * '0'..'9'.  We do so by using the Character.digit() method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                 * which converts a valid Unicode digit to the range 0..9.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                 * The character 'ch' may be a digit.  If so, place its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                 * from 0 to 9 in 'digit'.  First try using the locale digit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                 * which may or MAY NOT be a standard Unicode digit range.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                 * this fails, try using the standard Unicode digit ranges by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                 * calling Character.digit().  If this also fails, digit will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                 * have a value outside the range 0..9.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                int digit = ch - zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                if (digit < 0 || digit > 9) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                    digit = Character.digit(ch, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                if (digit == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                    // Cancel out backup setting (see grouping handler below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                    backup = -1; // Do this BEFORE continue statement below!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                    sawDigit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                    // Handle leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                    if (digits.count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                        // Ignore leading zeros in integer part of number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                        if (!sawDecimal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                        // If we have seen the decimal, but no significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                        // digits yet, then we account for leading zeros by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                        // decrementing the digits.decimalAt into negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                        // values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                        --digits.decimalAt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                        ++digitCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                        digits.append((char)(digit + '0'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                } else if (digit > 0 && digit <= 9) { // [sic] digit==0 handled above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                    sawDigit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                    ++digitCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                    digits.append((char)(digit + '0'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                    // Cancel out backup setting (see grouping handler below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                    backup = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                } else if (!isExponent && ch == decimal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                    // If we're only parsing integers, or if we ALREADY saw the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                    // decimal, then don't parse this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                    if (isParseIntegerOnly() || sawDecimal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                    digits.decimalAt = digitCount; // Not digits.count!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                    sawDecimal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                } else if (!isExponent && ch == grouping && isGroupingUsed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                    if (sawDecimal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                    // Ignore grouping characters, if we are using them, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                    // require that they be followed by a digit.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                    // we backup and reprocess them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                    backup = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                } else if (!isExponent && text.regionMatches(position, exponentString, 0, exponentString.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                             && !sawExponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                    // Process the exponent by recursively calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                     ParsePosition pos = new ParsePosition(position + exponentString.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    boolean[] stat = new boolean[STATUS_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                    DigitList exponentDigits = new DigitList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    if (subparse(text, pos, "", Character.toString(symbols.getMinusSign()), exponentDigits, true, stat) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                        exponentDigits.fitsIntoLong(stat[STATUS_POSITIVE], true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                        position = pos.index; // Advance past the exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                        exponent = (int)exponentDigits.getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                        if (!stat[STATUS_POSITIVE]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                            exponent = -exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                        sawExponent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    break; // Whether we fail or succeed, we exit this loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            if (backup != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                position = backup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            // If there was no decimal point we have an integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            if (!sawDecimal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                digits.decimalAt = digitCount; // Not digits.count!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            // Adjust for exponent, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            digits.decimalAt += exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            // If none of the text string was recognized.  For example, parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            // "x" with pattern "#0.00" (return index and error index both 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            // parse "$" with pattern "$#0.00". (return index 0 and error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            // index 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            if (!sawDigit && digitCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                parsePosition.index = oldStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                parsePosition.errorIndex = oldStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        // check for suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        if (!isExponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            if (gotPositive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                gotPositive = text.regionMatches(position,positiveSuffix,0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                                                 positiveSuffix.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            if (gotNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                gotNegative = text.regionMatches(position,negativeSuffix,0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                                                 negativeSuffix.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        // if both match, take longest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        if (gotPositive && gotNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            if (positiveSuffix.length() > negativeSuffix.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                gotNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            } else if (positiveSuffix.length() < negativeSuffix.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                gotPositive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        // fail if neither or both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        if (gotPositive == gotNegative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            parsePosition.errorIndex = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        parsePosition.index = position +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            (gotPositive ? positiveSuffix.length() : negativeSuffix.length()); // mark success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            parsePosition.index = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        status[STATUS_POSITIVE] = gotPositive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        if (parsePosition.index == oldStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            parsePosition.errorIndex = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * Returns a copy of the decimal format symbols, which is generally not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * changed by the programmer or user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * @return a copy of the desired DecimalFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * @see java.text.DecimalFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    public DecimalFormatSymbols getDecimalFormatSymbols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            // don't allow multiple references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            return (DecimalFormatSymbols) symbols.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        } catch (Exception foo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            return null; // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * Sets the decimal format symbols, which is generally not changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * by the programmer or user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * @param newSymbols desired DecimalFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * @see java.text.DecimalFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            // don't allow multiple references
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            symbols = (DecimalFormatSymbols) newSymbols.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            expandAffixes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        } catch (Exception foo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            // should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * Get the positive prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * <P>Examples: +123, $123, sFr123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
    public String getPositivePrefix () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        return positivePrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * Set the positive prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * <P>Examples: +123, $123, sFr123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    public void setPositivePrefix (String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        positivePrefix = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        posPrefixPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        positivePrefixFieldPositions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * Returns the FieldPositions of the fields in the prefix used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * positive numbers. This is not used if the user has explicitly set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * a positive prefix via <code>setPositivePrefix</code>. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * lazily created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * @return FieldPositions in positive prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    private FieldPosition[] getPositivePrefixFieldPositions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if (positivePrefixFieldPositions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            if (posPrefixPattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                positivePrefixFieldPositions = expandAffix(posPrefixPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                positivePrefixFieldPositions = EmptyFieldPositionArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        return positivePrefixFieldPositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * Get the negative prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * <P>Examples: -123, ($123) (with negative suffix), sFr-123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    public String getNegativePrefix () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        return negativePrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * Set the negative prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * <P>Examples: -123, ($123) (with negative suffix), sFr-123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    public void setNegativePrefix (String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        negativePrefix = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        negPrefixPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * Returns the FieldPositions of the fields in the prefix used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * negative numbers. This is not used if the user has explicitly set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * a negative prefix via <code>setNegativePrefix</code>. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * lazily created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * @return FieldPositions in positive prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    private FieldPosition[] getNegativePrefixFieldPositions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        if (negativePrefixFieldPositions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            if (negPrefixPattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                negativePrefixFieldPositions = expandAffix(negPrefixPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                negativePrefixFieldPositions = EmptyFieldPositionArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        return negativePrefixFieldPositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * Get the positive suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * <P>Example: 123%
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    public String getPositiveSuffix () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        return positiveSuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * Set the positive suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * <P>Example: 123%
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    public void setPositiveSuffix (String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        positiveSuffix = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        posSuffixPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * Returns the FieldPositions of the fields in the suffix used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * positive numbers. This is not used if the user has explicitly set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * a positive suffix via <code>setPositiveSuffix</code>. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * lazily created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * @return FieldPositions in positive prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
    private FieldPosition[] getPositiveSuffixFieldPositions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        if (positiveSuffixFieldPositions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            if (posSuffixPattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                positiveSuffixFieldPositions = expandAffix(posSuffixPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                positiveSuffixFieldPositions = EmptyFieldPositionArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        return positiveSuffixFieldPositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * Get the negative suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * <P>Examples: -123%, ($123) (with positive suffixes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    public String getNegativeSuffix () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        return negativeSuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * Set the negative suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * <P>Examples: 123%
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
    public void setNegativeSuffix (String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        negativeSuffix = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        negSuffixPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * Returns the FieldPositions of the fields in the suffix used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * negative numbers. This is not used if the user has explicitly set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * a negative suffix via <code>setNegativeSuffix</code>. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * lazily created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * @return FieldPositions in positive prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    private FieldPosition[] getNegativeSuffixFieldPositions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        if (negativeSuffixFieldPositions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            if (negSuffixPattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                negativeSuffixFieldPositions = expandAffix(negSuffixPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                negativeSuffixFieldPositions = EmptyFieldPositionArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        return negativeSuffixFieldPositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * Gets the multiplier for use in percent, per mille, and similar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * @see #setMultiplier(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
    public int getMultiplier () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        return multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * Sets the multiplier for use in percent, per mille, and similar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * For a percent format, set the multiplier to 100 and the suffixes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * have '%' (for Arabic, use the Arabic percent sign).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * For a per mille format, set the multiplier to 1000 and the suffixes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * have '&#92;u2030'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * <P>Example: with multiplier 100, 1.23 is formatted as "123", and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * "123" is parsed into 1.23.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @see #getMultiplier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    public void setMultiplier (int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        multiplier = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        bigDecimalMultiplier = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        bigIntegerMultiplier = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * Return the grouping size. Grouping size is the number of digits between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * grouping separators in the integer portion of a number.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * in the number "123,456.78", the grouping size is 3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * @see #setGroupingSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * @see java.text.NumberFormat#isGroupingUsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * @see java.text.DecimalFormatSymbols#getGroupingSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
    public int getGroupingSize () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        return groupingSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * Set the grouping size. Grouping size is the number of digits between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * grouping separators in the integer portion of a number.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * in the number "123,456.78", the grouping size is 3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * The value passed in is converted to a byte, which may lose information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * @see #getGroupingSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * @see java.text.NumberFormat#setGroupingUsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * @see java.text.DecimalFormatSymbols#setGroupingSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    public void setGroupingSize (int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        groupingSize = (byte)newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * Allows you to get the behavior of the decimal separator with integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * (The decimal separator will always appear with decimals.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * <P>Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    public boolean isDecimalSeparatorAlwaysShown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        return decimalSeparatorAlwaysShown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * Allows you to set the behavior of the decimal separator with integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * (The decimal separator will always appear with decimals.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * <P>Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
    public void setDecimalSeparatorAlwaysShown(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        decimalSeparatorAlwaysShown = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * Returns whether the {@link #parse(java.lang.String, java.text.ParsePosition)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * method returns <code>BigDecimal</code>. The default value is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * @see #setParseBigDecimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    public boolean isParseBigDecimal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        return parseBigDecimal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * Sets whether the {@link #parse(java.lang.String, java.text.ParsePosition)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * method returns <code>BigDecimal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * @see #isParseBigDecimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    public void setParseBigDecimal(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        parseBigDecimal = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * Standard override; no change in semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
    public Object clone() {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 7003
diff changeset
  1894
        DecimalFormat other = (DecimalFormat) super.clone();
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 7003
diff changeset
  1895
        other.symbols = (DecimalFormatSymbols) symbols.clone();
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 7003
diff changeset
  1896
        other.digitList = (DigitList) digitList.clone();
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 7003
diff changeset
  1897
        return other;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * Overrides equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
    public boolean equals(Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        if (obj == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        if (!super.equals(obj)) return false; // super does class check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        DecimalFormat other = (DecimalFormat) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        return ((posPrefixPattern == other.posPrefixPattern &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                 positivePrefix.equals(other.positivePrefix))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                || (posPrefixPattern != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                    posPrefixPattern.equals(other.posPrefixPattern)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            && ((posSuffixPattern == other.posSuffixPattern &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                 positiveSuffix.equals(other.positiveSuffix))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                || (posSuffixPattern != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                    posSuffixPattern.equals(other.posSuffixPattern)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            && ((negPrefixPattern == other.negPrefixPattern &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                 negativePrefix.equals(other.negativePrefix))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                || (negPrefixPattern != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                    negPrefixPattern.equals(other.negPrefixPattern)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            && ((negSuffixPattern == other.negSuffixPattern &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                 negativeSuffix.equals(other.negativeSuffix))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                || (negSuffixPattern != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                    negSuffixPattern.equals(other.negSuffixPattern)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            && multiplier == other.multiplier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            && groupingSize == other.groupingSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            && decimalSeparatorAlwaysShown == other.decimalSeparatorAlwaysShown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            && parseBigDecimal == other.parseBigDecimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            && useExponentialNotation == other.useExponentialNotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            && (!useExponentialNotation ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                minExponentDigits == other.minExponentDigits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            && maximumIntegerDigits == other.maximumIntegerDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            && minimumIntegerDigits == other.minimumIntegerDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            && maximumFractionDigits == other.maximumFractionDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            && minimumFractionDigits == other.minimumFractionDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            && roundingMode == other.roundingMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            && symbols.equals(other.symbols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * Overrides hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        return super.hashCode() * 37 + positivePrefix.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        // just enough fields for a reasonable distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * Synthesizes a pattern string that represents the current state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * of this Format object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * @see #applyPattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    public String toPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        return toPattern( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * Synthesizes a localized pattern string that represents the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * state of this Format object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * @see #applyPattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    public String toLocalizedPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        return toPattern( true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * Expand the affix pattern strings into the expanded affix strings.  If any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * affix pattern string is null, do not expand it.  This method should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * called any time the symbols or the affix patterns change in order to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * the expanded affix strings up to date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
    private void expandAffixes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        // Reuse one StringBuffer for better performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        if (posPrefixPattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            positivePrefix = expandAffix(posPrefixPattern, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            positivePrefixFieldPositions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        if (posSuffixPattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            positiveSuffix = expandAffix(posSuffixPattern, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            positiveSuffixFieldPositions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        if (negPrefixPattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            negativePrefix = expandAffix(negPrefixPattern, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            negativePrefixFieldPositions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        if (negSuffixPattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            negativeSuffix = expandAffix(negSuffixPattern, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            negativeSuffixFieldPositions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     * Expand an affix pattern into an affix string.  All characters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * pattern are literal unless prefixed by QUOTE.  The following characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * after QUOTE are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * PATTERN_MINUS, and CURRENCY_SIGN.  If CURRENCY_SIGN is doubled (QUOTE +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * CURRENCY_SIGN + CURRENCY_SIGN), it is interpreted as an ISO 4217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     * currency code.  Any other character after a QUOTE represents itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * QUOTE must be followed by another character; QUOTE may not occur by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * itself at the end of the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * @param pattern the non-null, possibly empty pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * @param buffer a scratch StringBuffer; its contents will be lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * @return the expanded equivalent of pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    private String expandAffix(String pattern, StringBuffer buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        buffer.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        for (int i=0; i<pattern.length(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            char c = pattern.charAt(i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            if (c == QUOTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                c = pattern.charAt(i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                case CURRENCY_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                    if (i<pattern.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                        pattern.charAt(i) == CURRENCY_SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                        ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                        buffer.append(symbols.getInternationalCurrencySymbol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                        buffer.append(symbols.getCurrencySymbol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                case PATTERN_PERCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                    c = symbols.getPercent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                case PATTERN_PER_MILLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                    c = symbols.getPerMill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                case PATTERN_MINUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                    c = symbols.getMinusSign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * Expand an affix pattern into an array of FieldPositions describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * how the pattern would be expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * All characters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * pattern are literal unless prefixed by QUOTE.  The following characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * after QUOTE are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * PATTERN_MINUS, and CURRENCY_SIGN.  If CURRENCY_SIGN is doubled (QUOTE +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * CURRENCY_SIGN + CURRENCY_SIGN), it is interpreted as an ISO 4217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * currency code.  Any other character after a QUOTE represents itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * QUOTE must be followed by another character; QUOTE may not occur by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * itself at the end of the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     * @param pattern the non-null, possibly empty pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * @return FieldPosition array of the resulting fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    private FieldPosition[] expandAffix(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        ArrayList positions = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        int stringIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        for (int i=0; i<pattern.length(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            char c = pattern.charAt(i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            if (c == QUOTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                int field = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                Format.Field fieldID = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                c = pattern.charAt(i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                case CURRENCY_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                    String string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                    if (i<pattern.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                        pattern.charAt(i) == CURRENCY_SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                        ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                        string = symbols.getInternationalCurrencySymbol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                        string = symbols.getCurrencySymbol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                    if (string.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                        if (positions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                            positions = new ArrayList(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                        FieldPosition fp = new FieldPosition(Field.CURRENCY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                        fp.setBeginIndex(stringIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                        fp.setEndIndex(stringIndex + string.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                        positions.add(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                        stringIndex += string.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                case PATTERN_PERCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                    c = symbols.getPercent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                    field = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                    fieldID = Field.PERCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                case PATTERN_PER_MILLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                    c = symbols.getPerMill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                    field = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                    fieldID = Field.PERMILLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                case PATTERN_MINUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                    c = symbols.getMinusSign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                    field = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                    fieldID = Field.SIGN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                if (fieldID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                    if (positions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                        positions = new ArrayList(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                    FieldPosition fp = new FieldPosition(fieldID, field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                    fp.setBeginIndex(stringIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    fp.setEndIndex(stringIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                    positions.add(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            stringIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        if (positions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            return (FieldPosition[])positions.toArray(EmptyFieldPositionArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        return EmptyFieldPositionArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * Appends an affix pattern to the given StringBuffer, quoting special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * characters as needed.  Uses the internal affix pattern, if that exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * or the literal affix, if the internal affix pattern is null.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * appended string will generate the same affix pattern (or literal affix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * when passed to toPattern().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * @param buffer the affix string is appended to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * @param affixPattern a pattern such as posPrefixPattern; may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * @param expAffix a corresponding expanded affix, such as positivePrefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * Ignored unless affixPattern is null.  If affixPattern is null, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * expAffix is appended as a literal affix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * @param localized true if the appended pattern should contain localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * pattern characters; otherwise, non-localized pattern chars are appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
    private void appendAffix(StringBuffer buffer, String affixPattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                             String expAffix, boolean localized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        if (affixPattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            appendAffix(buffer, expAffix, localized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            for (int pos=0; pos<affixPattern.length(); pos=i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                i = affixPattern.indexOf(QUOTE, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                if (i < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                    appendAffix(buffer, affixPattern.substring(pos), localized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                if (i > pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                    appendAffix(buffer, affixPattern.substring(pos, i), localized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                char c = affixPattern.charAt(++i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                if (c == QUOTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                    buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                    // Fall through and append another QUOTE below
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                } else if (c == CURRENCY_SIGN &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                           i<affixPattern.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                           affixPattern.charAt(i) == CURRENCY_SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                    ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                    buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                    // Fall through and append another CURRENCY_SIGN below
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                } else if (localized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                    switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                    case PATTERN_PERCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                        c = symbols.getPercent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                    case PATTERN_PER_MILLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                        c = symbols.getPerMill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                    case PATTERN_MINUS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                        c = symbols.getMinusSign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * Append an affix to the given StringBuffer, using quotes if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * there are special characters.  Single quotes themselves must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     * escaped in either case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
    private void appendAffix(StringBuffer buffer, String affix, boolean localized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        boolean needQuote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        if (localized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            needQuote = affix.indexOf(symbols.getZeroDigit()) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                || affix.indexOf(symbols.getGroupingSeparator()) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                || affix.indexOf(symbols.getDecimalSeparator()) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                || affix.indexOf(symbols.getPercent()) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                || affix.indexOf(symbols.getPerMill()) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                || affix.indexOf(symbols.getDigit()) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                || affix.indexOf(symbols.getPatternSeparator()) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                || affix.indexOf(symbols.getMinusSign()) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                || affix.indexOf(CURRENCY_SIGN) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            needQuote = affix.indexOf(PATTERN_ZERO_DIGIT) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                || affix.indexOf(PATTERN_GROUPING_SEPARATOR) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                || affix.indexOf(PATTERN_DECIMAL_SEPARATOR) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                || affix.indexOf(PATTERN_PERCENT) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                || affix.indexOf(PATTERN_PER_MILLE) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                || affix.indexOf(PATTERN_DIGIT) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                || affix.indexOf(PATTERN_SEPARATOR) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                || affix.indexOf(PATTERN_MINUS) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                || affix.indexOf(CURRENCY_SIGN) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        if (needQuote) buffer.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        if (affix.indexOf('\'') < 0) buffer.append(affix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            for (int j=0; j<affix.length(); ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                char c = affix.charAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                if (c == '\'') buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        if (needQuote) buffer.append('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * Does the real work of generating a pattern.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
    private String toPattern(boolean localized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
        StringBuffer result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        for (int j = 1; j >= 0; --j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            if (j == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                appendAffix(result, posPrefixPattern, positivePrefix, localized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            else appendAffix(result, negPrefixPattern, negativePrefix, localized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            int digitCount = useExponentialNotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                        ? getMaximumIntegerDigits()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                        : Math.max(groupingSize, getMinimumIntegerDigits())+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            for (i = digitCount; i > 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                if (i != digitCount && isGroupingUsed() && groupingSize != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                    i % groupingSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                    result.append(localized ? symbols.getGroupingSeparator() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                                  PATTERN_GROUPING_SEPARATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                result.append(i <= getMinimumIntegerDigits()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                    ? (localized ? symbols.getZeroDigit() : PATTERN_ZERO_DIGIT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                    : (localized ? symbols.getDigit() : PATTERN_DIGIT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            if (getMaximumFractionDigits() > 0 || decimalSeparatorAlwaysShown)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                result.append(localized ? symbols.getDecimalSeparator() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                              PATTERN_DECIMAL_SEPARATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            for (i = 0; i < getMaximumFractionDigits(); ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                if (i < getMinimumFractionDigits()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                    result.append(localized ? symbols.getZeroDigit() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                                  PATTERN_ZERO_DIGIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                    result.append(localized ? symbols.getDigit() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                                  PATTERN_DIGIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        if (useExponentialNotation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            result.append(localized ? symbols.getExponentSeparator() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                  PATTERN_EXPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        for (i=0; i<minExponentDigits; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                    result.append(localized ? symbols.getZeroDigit() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                                  PATTERN_ZERO_DIGIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            if (j == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                appendAffix(result, posSuffixPattern, positiveSuffix, localized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                if ((negSuffixPattern == posSuffixPattern && // n == p == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                     negativeSuffix.equals(positiveSuffix))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                    || (negSuffixPattern != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                        negSuffixPattern.equals(posSuffixPattern))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                    if ((negPrefixPattern != null && posPrefixPattern != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                         negPrefixPattern.equals("'-" + posPrefixPattern)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                        (negPrefixPattern == posPrefixPattern && // n == p == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                         negativePrefix.equals(symbols.getMinusSign() + positivePrefix)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                result.append(localized ? symbols.getPatternSeparator() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                              PATTERN_SEPARATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            } else appendAffix(result, negSuffixPattern, negativeSuffix, localized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     * Apply the given pattern to this Format object.  A pattern is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * short-hand specification for the various formatting properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     * These properties can also be changed individually through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * various setter methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * There is no limit to integer digits set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * by this routine, since that is the typical end-user desire;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * use setMaximumInteger if you want to set a real value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     * For negative numbers, use a second pattern, separated by a semicolon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     * <P>Example <code>"#,#00.0#"</code> -> 1,234.56
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * <P>This means a minimum of 2 integer digits, 1 fraction digit, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * a maximum of 2 fraction digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * <p>Example: <code>"#,#00.0#;(#,#00.0#)"</code> for negatives in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     * parentheses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     * <p>In negative patterns, the minimum and maximum counts are ignored;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     * these are presumed to be set in the positive pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * @exception NullPointerException if <code>pattern</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * @exception IllegalArgumentException if the given pattern is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
    public void applyPattern(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        applyPattern(pattern, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * Apply the given pattern to this Format object.  The pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     * is assumed to be in a localized notation. A pattern is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     * short-hand specification for the various formatting properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     * These properties can also be changed individually through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     * various setter methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * There is no limit to integer digits set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * by this routine, since that is the typical end-user desire;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     * use setMaximumInteger if you want to set a real value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * For negative numbers, use a second pattern, separated by a semicolon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * <P>Example <code>"#,#00.0#"</code> -> 1,234.56
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * <P>This means a minimum of 2 integer digits, 1 fraction digit, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     * a maximum of 2 fraction digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     * <p>Example: <code>"#,#00.0#;(#,#00.0#)"</code> for negatives in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * parentheses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * <p>In negative patterns, the minimum and maximum counts are ignored;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     * these are presumed to be set in the positive pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * @exception NullPointerException if <code>pattern</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * @exception IllegalArgumentException if the given pattern is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
    public void applyLocalizedPattern(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        applyPattern(pattern, true);
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
     * Does the real work of applying a pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
    private void applyPattern(String pattern, boolean localized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        char zeroDigit         = PATTERN_ZERO_DIGIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        char groupingSeparator = PATTERN_GROUPING_SEPARATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        char decimalSeparator  = PATTERN_DECIMAL_SEPARATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        char percent           = PATTERN_PERCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        char perMill           = PATTERN_PER_MILLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        char digit             = PATTERN_DIGIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        char separator         = PATTERN_SEPARATOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        String exponent          = PATTERN_EXPONENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        char minus             = PATTERN_MINUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        if (localized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            zeroDigit         = symbols.getZeroDigit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            groupingSeparator = symbols.getGroupingSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            decimalSeparator  = symbols.getDecimalSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            percent           = symbols.getPercent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            perMill           = symbols.getPerMill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            digit             = symbols.getDigit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            separator         = symbols.getPatternSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            exponent          = symbols.getExponentSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            minus             = symbols.getMinusSign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        boolean gotNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        decimalSeparatorAlwaysShown = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        isCurrencyFormat = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        useExponentialNotation = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        // Two variables are used to record the subrange of the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        // occupied by phase 1.  This is used during the processing of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        // second pattern (the one representing negative numbers) to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        // that no deviation exists in phase 1 between the two patterns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        int phaseOneStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        int phaseOneLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        for (int j = 1; j >= 0 && start < pattern.length(); --j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            boolean inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            StringBuffer prefix = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            StringBuffer suffix = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            int decimalPos = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            int multiplier = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            int digitLeftCount = 0, zeroDigitCount = 0, digitRightCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            byte groupingCount = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            // The phase ranges from 0 to 2.  Phase 0 is the prefix.  Phase 1 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            // the section of the pattern with digits, decimal separator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            // grouping characters.  Phase 2 is the suffix.  In phases 0 and 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            // percent, per mille, and currency symbols are recognized and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            // translated.  The separation of the characters into phases is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            // strictly enforced; if phase 1 characters are to appear in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            // suffix, for example, they must be quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            int phase = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
            // The affix is either the prefix or the suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            StringBuffer affix = prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            for (int pos = start; pos < pattern.length(); ++pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                char ch = pattern.charAt(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                switch (phase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                    // Process the prefix / suffix characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                    if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                        // A quote within quotes indicates either the closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                        // quote or two quotes, which is a quote literal. That
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                        // is, we have the second quote in 'do' or 'don''t'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                        if (ch == QUOTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                            if ((pos+1) < pattern.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                                pattern.charAt(pos+1) == QUOTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                                ++pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                                affix.append("''"); // 'don''t'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
                                inQuote = false; // 'do'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                        // Process unquoted characters seen in prefix or suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                        // phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                        if (ch == digit ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                            ch == zeroDigit ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                            ch == groupingSeparator ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                            ch == decimalSeparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                            phase = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                            if (j == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                                phaseOneStart = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                            --pos; // Reprocess this character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                        } else if (ch == CURRENCY_SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                            // Use lookahead to determine if the currency sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                            // is doubled or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                            boolean doubled = (pos + 1) < pattern.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                                pattern.charAt(pos + 1) == CURRENCY_SIGN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                            if (doubled) { // Skip over the doubled character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                             ++pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                            isCurrencyFormat = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                            affix.append(doubled ? "'\u00A4\u00A4" : "'\u00A4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                        } else if (ch == QUOTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                            // A quote outside quotes indicates either the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                            // opening quote or two quotes, which is a quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                            // literal. That is, we have the first quote in 'do'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                            // or o''clock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                            if (ch == QUOTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                                if ((pos+1) < pattern.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                                    pattern.charAt(pos+1) == QUOTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                                    ++pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                                    affix.append("''"); // o''clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                                    inQuote = true; // 'do'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                        } else if (ch == separator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                            // Don't allow separators before we see digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                            // characters of phase 1, and don't allow separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
                            // in the second pattern (j == 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
                            if (phase == 0 || j == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
                                throw new IllegalArgumentException("Unquoted special character '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                                    ch + "' in pattern \"" + pattern + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                            start = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                            pos = pattern.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                        // Next handle characters which are appended directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
                        else if (ch == percent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                            if (multiplier != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                                throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                                    pattern + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                            multiplier = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                            affix.append("'%");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                        } else if (ch == perMill) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                            if (multiplier != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                                throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                                    pattern + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                            multiplier = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                            affix.append("'\u2030");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                        } else if (ch == minus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                            affix.append("'-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                    // Note that if we are within quotes, or if this is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                    // unquoted, non-special character, then we usually fall
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                    // through to here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                    affix.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                    // Phase one must be identical in the two sub-patterns. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                    // enforce this by doing a direct comparison. While
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                    // processing the first sub-pattern, we just record its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                    // length. While processing the second, we compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                    // characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                    if (j == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                        ++phaseOneLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                        if (--phaseOneLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                            phase = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                            affix = suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                    // Process the digits, decimal, and grouping characters. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                    // record five pieces of information. We expect the digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                    // to occur in the pattern ####0000.####, and we record the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                    // number of left digits, zero (central) digits, and right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                    // digits. The position of the last grouping character is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                    // recorded (should be somewhere within the first two blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                    // of characters), as is the position of the decimal point,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                    // if any (should be in the zero digits). If there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                    // decimal point, then there should be no right digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                    if (ch == digit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                        if (zeroDigitCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                            ++digitRightCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                            ++digitLeftCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                        if (groupingCount >= 0 && decimalPos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                            ++groupingCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
                    } else if (ch == zeroDigit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                        if (digitRightCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                            throw new IllegalArgumentException("Unexpected '0' in pattern \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                                pattern + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                        ++zeroDigitCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                        if (groupingCount >= 0 && decimalPos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                            ++groupingCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                    } else if (ch == groupingSeparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                        groupingCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                    } else if (ch == decimalSeparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                        if (decimalPos >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                            throw new IllegalArgumentException("Multiple decimal separators in pattern \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                                pattern + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                        decimalPos = digitLeftCount + zeroDigitCount + digitRightCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                    } else if (pattern.regionMatches(pos, exponent, 0, exponent.length())){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                        if (useExponentialNotation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                            throw new IllegalArgumentException("Multiple exponential " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                                "symbols in pattern \"" + pattern + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                        useExponentialNotation = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
                        minExponentDigits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                        // Use lookahead to parse out the exponential part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                        // of the pattern, then jump into phase 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                        pos = pos+exponent.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                         while (pos < pattern.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                               pattern.charAt(pos) == zeroDigit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                            ++minExponentDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                            ++phaseOneLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                            ++pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
                        if ((digitLeftCount + zeroDigitCount) < 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                            minExponentDigits < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                            throw new IllegalArgumentException("Malformed exponential " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                                "pattern \"" + pattern + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                        // Transition to phase 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                        phase = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                        affix = suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                        --pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                        phase = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                        affix = suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                        --pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                        --phaseOneLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            // Handle patterns with no '0' pattern character. These patterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
            // are legal, but must be interpreted.  "##.###" -> "#0.###".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
            // ".###" -> ".0##".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
            /* We allow patterns of the form "####" to produce a zeroDigitCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
             * of zero (got that?); although this seems like it might make it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
             * possible for format() to produce empty strings, format() checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
             * for this condition and outputs a zero digit in this situation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
             * Having a zeroDigitCount of zero yields a minimum integer digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
             * of zero, which allows proper round-trip patterns.  That is, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
             * don't want "#" to become "#0" when toPattern() is called (even
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
             * though that's what it really is, semantically).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
            if (zeroDigitCount == 0 && digitLeftCount > 0 && decimalPos >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                // Handle "###.###" and "###." and ".###"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                int n = decimalPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                if (n == 0) { // Handle ".###"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                    ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                digitRightCount = digitLeftCount - n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                digitLeftCount = n - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                zeroDigitCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            // Do syntax checking on the digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            if ((decimalPos < 0 && digitRightCount > 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                (decimalPos >= 0 && (decimalPos < digitLeftCount ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                 decimalPos > (digitLeftCount + zeroDigitCount))) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                 groupingCount == 0 || inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                throw new IllegalArgumentException("Malformed pattern \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                    pattern + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            if (j == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                posPrefixPattern = prefix.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                posSuffixPattern = suffix.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                negPrefixPattern = posPrefixPattern;   // assume these for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                negSuffixPattern = posSuffixPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                int digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                /* The effectiveDecimalPos is the position the decimal is at or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                 * would be at if there is no decimal. Note that if decimalPos<0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                 * then digitTotalCount == digitLeftCount + zeroDigitCount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
                int effectiveDecimalPos = decimalPos >= 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
                    decimalPos : digitTotalCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
                setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
                setMaximumIntegerDigits(useExponentialNotation ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
                    digitLeftCount + getMinimumIntegerDigits() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
                    MAXIMUM_INTEGER_DIGITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
                setMaximumFractionDigits(decimalPos >= 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                    (digitTotalCount - decimalPos) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                setMinimumFractionDigits(decimalPos >= 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                    (digitLeftCount + zeroDigitCount - decimalPos) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
                setGroupingUsed(groupingCount > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
                this.groupingSize = (groupingCount > 0) ? groupingCount : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
                this.multiplier = multiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
                setDecimalSeparatorAlwaysShown(decimalPos == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                    decimalPos == digitTotalCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                negPrefixPattern = prefix.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                negSuffixPattern = suffix.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                gotNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
        if (pattern.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
            posPrefixPattern = posSuffixPattern = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            setMinimumIntegerDigits(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            setMaximumIntegerDigits(MAXIMUM_INTEGER_DIGITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
            setMinimumFractionDigits(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
            setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        // If there was no negative pattern, or if the negative pattern is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
        // identical to the positive pattern, then prepend the minus sign to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        // the positive pattern to form the negative pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        if (!gotNegative ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
            (negPrefixPattern.equals(posPrefixPattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
             && negSuffixPattern.equals(posSuffixPattern))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            negSuffixPattern = posSuffixPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
            negPrefixPattern = "'-" + posPrefixPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
        expandAffixes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
     * Sets the maximum number of digits allowed in the integer portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
     * number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
     * For formatting numbers other than <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
     * <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
     * 309 is used. Negative input values are replaced with 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
     * @see NumberFormat#setMaximumIntegerDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
    public void setMaximumIntegerDigits(int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        maximumIntegerDigits = Math.min(Math.max(0, newValue), MAXIMUM_INTEGER_DIGITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
        super.setMaximumIntegerDigits((maximumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            DOUBLE_INTEGER_DIGITS : maximumIntegerDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
        if (minimumIntegerDigits > maximumIntegerDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            minimumIntegerDigits = maximumIntegerDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
            super.setMinimumIntegerDigits((minimumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                DOUBLE_INTEGER_DIGITS : minimumIntegerDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
     * Sets the minimum number of digits allowed in the integer portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
     * number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
     * For formatting numbers other than <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
     * <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
     * 309 is used. Negative input values are replaced with 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
     * @see NumberFormat#setMinimumIntegerDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
    public void setMinimumIntegerDigits(int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        minimumIntegerDigits = Math.min(Math.max(0, newValue), MAXIMUM_INTEGER_DIGITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
        super.setMinimumIntegerDigits((minimumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
            DOUBLE_INTEGER_DIGITS : minimumIntegerDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
        if (minimumIntegerDigits > maximumIntegerDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
            maximumIntegerDigits = minimumIntegerDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            super.setMaximumIntegerDigits((maximumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                DOUBLE_INTEGER_DIGITS : maximumIntegerDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
     * Sets the maximum number of digits allowed in the fraction portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
     * number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
     * For formatting numbers other than <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
     * <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     * 340 is used. Negative input values are replaced with 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     * @see NumberFormat#setMaximumFractionDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
    public void setMaximumFractionDigits(int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
        maximumFractionDigits = Math.min(Math.max(0, newValue), MAXIMUM_FRACTION_DIGITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
        super.setMaximumFractionDigits((maximumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
            DOUBLE_FRACTION_DIGITS : maximumFractionDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
        if (minimumFractionDigits > maximumFractionDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
            minimumFractionDigits = maximumFractionDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
            super.setMinimumFractionDigits((minimumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
                DOUBLE_FRACTION_DIGITS : minimumFractionDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
     * Sets the minimum number of digits allowed in the fraction portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
     * number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
     * For formatting numbers other than <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
     * <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
     * 340 is used. Negative input values are replaced with 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
     * @see NumberFormat#setMinimumFractionDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
    public void setMinimumFractionDigits(int newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
        minimumFractionDigits = Math.min(Math.max(0, newValue), MAXIMUM_FRACTION_DIGITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        super.setMinimumFractionDigits((minimumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            DOUBLE_FRACTION_DIGITS : minimumFractionDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        if (minimumFractionDigits > maximumFractionDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
            maximumFractionDigits = minimumFractionDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
            super.setMaximumFractionDigits((maximumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                DOUBLE_FRACTION_DIGITS : maximumFractionDigits);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
     * Gets the maximum number of digits allowed in the integer portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
     * number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     * For formatting numbers other than <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     * <code>BigDecimal</code> objects, the lower of the return value and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
     * 309 is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
     * @see #setMaximumIntegerDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
    public int getMaximumIntegerDigits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        return maximumIntegerDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
     * Gets the minimum number of digits allowed in the integer portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
     * number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
     * For formatting numbers other than <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
     * <code>BigDecimal</code> objects, the lower of the return value and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
     * 309 is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
     * @see #setMinimumIntegerDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
    public int getMinimumIntegerDigits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        return minimumIntegerDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
     * Gets the maximum number of digits allowed in the fraction portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
     * number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
     * For formatting numbers other than <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
     * <code>BigDecimal</code> objects, the lower of the return value and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
     * 340 is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
     * @see #setMaximumFractionDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
    public int getMaximumFractionDigits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
        return maximumFractionDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
     * Gets the minimum number of digits allowed in the fraction portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
     * number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
     * For formatting numbers other than <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * <code>BigDecimal</code> objects, the lower of the return value and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * 340 is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * @see #setMinimumFractionDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
    public int getMinimumFractionDigits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
        return minimumFractionDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     * Gets the currency used by this decimal format when formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     * currency values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
     * The currency is obtained by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
     * {@link DecimalFormatSymbols#getCurrency DecimalFormatSymbols.getCurrency}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
     * on this number format's symbols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
     * @return the currency used by this decimal format, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
    public Currency getCurrency() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
        return symbols.getCurrency();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
     * Sets the currency used by this number format when formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
     * currency values. This does not update the minimum or maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
     * number of fraction digits used by the number format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
     * The currency is set by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     * {@link DecimalFormatSymbols#setCurrency DecimalFormatSymbols.setCurrency}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     * on this number format's symbols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     * @param currency the new currency to be used by this decimal format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * @exception NullPointerException if <code>currency</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
    public void setCurrency(Currency currency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
        if (currency != symbols.getCurrency()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
            symbols.setCurrency(currency);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
            if (isCurrencyFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                expandAffixes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
     * Gets the {@link java.math.RoundingMode} used in this DecimalFormat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
     * @return The <code>RoundingMode</code> used for this DecimalFormat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
     * @see #setRoundingMode(RoundingMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
    public RoundingMode getRoundingMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
        return roundingMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
     * Sets the {@link java.math.RoundingMode} used in this DecimalFormat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
     * @param roundingMode The <code>RoundingMode</code> to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
     * @see #getRoundingMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
     * @exception NullPointerException if <code>roundingMode</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
    public void setRoundingMode(RoundingMode roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        if (roundingMode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
        this.roundingMode = roundingMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
        digitList.setRoundingMode(roundingMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
     * Adjusts the minimum and maximum fraction digits to values that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     * are reasonable for the currency's default fraction digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
    void adjustForCurrencyDefaultFractionDigits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        Currency currency = symbols.getCurrency();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        if (currency == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
                currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        if (currency != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
            int digits = currency.getDefaultFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
            if (digits != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                int oldMinDigits = getMinimumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
                // Common patterns are "#.##", "#.00", "#".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                // Try to adjust all of them in a reasonable way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                if (oldMinDigits == getMaximumFractionDigits()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                    setMinimumFractionDigits(digits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
                    setMaximumFractionDigits(digits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                    setMinimumFractionDigits(Math.min(digits, oldMinDigits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
                    setMaximumFractionDigits(digits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
     * Reads the default serializable fields from the stream and performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     * validations and adjustments for older serialized versions. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
     * validations and adjustments are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
     * Verify that the superclass's digit count fields correctly reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
     * the limits imposed on formatting numbers other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
     * <code>BigInteger</code> and <code>BigDecimal</code> objects. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
     * limits are stored in the superclass for serialization compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
     * with older versions, while the limits for <code>BigInteger</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
     * <code>BigDecimal</code> objects are kept in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
     * If, in the superclass, the minimum or maximum integer digit count is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
     * larger than <code>DOUBLE_INTEGER_DIGITS</code> or if the minimum or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
     * maximum fraction digit count is larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
     * <code>DOUBLE_FRACTION_DIGITS</code>, then the stream data is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
     * and this method throws an <code>InvalidObjectException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
     * If <code>serialVersionOnStream</code> is less than 4, initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
     * <code>roundingMode</code> to {@link java.math.RoundingMode#HALF_EVEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
     * RoundingMode.HALF_EVEN}.  This field is new with version 4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
     * If <code>serialVersionOnStream</code> is less than 3, then call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
     * the setters for the minimum and maximum integer and fraction digits with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
     * the values of the corresponding superclass getters to initialize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
     * fields in this class. The fields in this class are new with version 3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
     * If <code>serialVersionOnStream</code> is less than 1, indicating that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
     * the stream was written by JDK 1.1, initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
     * <code>useExponentialNotation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
     * to false, since it was not present in JDK 1.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
     * Set <code>serialVersionOnStream</code> to the maximum allowed value so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
     * that default serialization will work properly if this object is streamed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
     * out again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
     * <p>Stream versions older than 2 will not have the affix pattern variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
     * <code>posPrefixPattern</code> etc.  As a result, they will be initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
     * to <code>null</code>, which means the affix strings will be taken as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
     * literal values.  This is exactly what we want, since that corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
     * the pre-version-2 behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
    private void readObject(ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
        stream.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
        digitList = new DigitList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
        if (serialVersionOnStream < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
            setRoundingMode(RoundingMode.HALF_EVEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
        // We only need to check the maximum counts because NumberFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
        // .readObject has already ensured that the maximum is greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
        // minimum count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
        if (super.getMaximumIntegerDigits() > DOUBLE_INTEGER_DIGITS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
            super.getMaximumFractionDigits() > DOUBLE_FRACTION_DIGITS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
            throw new InvalidObjectException("Digit count out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        if (serialVersionOnStream < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
            setMaximumIntegerDigits(super.getMaximumIntegerDigits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
            setMinimumIntegerDigits(super.getMinimumIntegerDigits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            setMaximumFractionDigits(super.getMaximumFractionDigits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            setMinimumFractionDigits(super.getMinimumFractionDigits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
        if (serialVersionOnStream < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            // Didn't have exponential fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
            useExponentialNotation = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
        serialVersionOnStream = currentSerialVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
    //----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
    // INSTANCE VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
    //----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
    private transient DigitList digitList = new DigitList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
     * The symbol used as a prefix when formatting positive numbers, e.g. "+".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
     * @see #getPositivePrefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
    private String  positivePrefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
     * The symbol used as a suffix when formatting positive numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
     * This is often an empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
     * @see #getPositiveSuffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
    private String  positiveSuffix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
     * The symbol used as a prefix when formatting negative numbers, e.g. "-".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
     * @see #getNegativePrefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
    private String  negativePrefix = "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
     * The symbol used as a suffix when formatting negative numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
     * This is often an empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
     * @see #getNegativeSuffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
    private String  negativeSuffix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
     * The prefix pattern for non-negative numbers.  This variable corresponds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
     * to <code>positivePrefix</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
     * <p>This pattern is expanded by the method <code>expandAffix()</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
     * <code>positivePrefix</code> to update the latter to reflect changes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     * <code>symbols</code>.  If this variable is <code>null</code> then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * <code>positivePrefix</code> is taken as a literal value that does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     * change when <code>symbols</code> changes.  This variable is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
     * <code>null</code> for <code>DecimalFormat</code> objects older than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
     * stream version 2 restored from stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
    private String posPrefixPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
     * The suffix pattern for non-negative numbers.  This variable corresponds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
     * to <code>positiveSuffix</code>.  This variable is analogous to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
     * <code>posPrefixPattern</code>; see that variable for further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
     * documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
    private String posSuffixPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
     * The prefix pattern for negative numbers.  This variable corresponds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
     * to <code>negativePrefix</code>.  This variable is analogous to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
     * <code>posPrefixPattern</code>; see that variable for further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
     * documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
    private String negPrefixPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
     * The suffix pattern for negative numbers.  This variable corresponds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
     * to <code>negativeSuffix</code>.  This variable is analogous to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
     * <code>posPrefixPattern</code>; see that variable for further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
     * documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
    private String negSuffixPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
     * The multiplier for use in percent, per mille, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
     * @see #getMultiplier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
    private int     multiplier = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
     * The number of digits between grouping separators in the integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
     * portion of a number.  Must be greater than 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
     * <code>NumberFormat.groupingUsed</code> is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
     * @see #getGroupingSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
     * @see java.text.NumberFormat#isGroupingUsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
    private byte    groupingSize = 3;  // invariant, > 0 if useThousands
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
     * If true, forces the decimal separator to always appear in a formatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
     * number, even if the fractional part of the number is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
     * @see #isDecimalSeparatorAlwaysShown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
    private boolean decimalSeparatorAlwaysShown = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
     * If true, parse returns BigDecimal wherever possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
     * @see #isParseBigDecimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
    private boolean parseBigDecimal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
     * True if this object represents a currency format.  This determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
     * whether the monetary decimal separator is used instead of the normal one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
    private transient boolean isCurrencyFormat = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
     * The <code>DecimalFormatSymbols</code> object used by this format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
     * It contains the symbols used to format numbers, e.g. the grouping separator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
     * decimal separator, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
     * @see #setDecimalFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
     * @see java.text.DecimalFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
    private DecimalFormatSymbols symbols = null; // LIU new DecimalFormatSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
     * True to force the use of exponential (i.e. scientific) notation when formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
     * numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
    private boolean useExponentialNotation;  // Newly persistent in the Java 2 platform v.1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
     * FieldPositions describing the positive prefix String. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
     * lazily created. Use <code>getPositivePrefixFieldPositions</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
     * when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
    private transient FieldPosition[] positivePrefixFieldPositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
     * FieldPositions describing the positive suffix String. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
     * lazily created. Use <code>getPositiveSuffixFieldPositions</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
     * when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
    private transient FieldPosition[] positiveSuffixFieldPositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
     * FieldPositions describing the negative prefix String. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
     * lazily created. Use <code>getNegativePrefixFieldPositions</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
     * when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
    private transient FieldPosition[] negativePrefixFieldPositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
     * FieldPositions describing the negative suffix String. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
     * lazily created. Use <code>getNegativeSuffixFieldPositions</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
     * when needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    private transient FieldPosition[] negativeSuffixFieldPositions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
     * The minimum number of digits used to display the exponent when a number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
     * formatted in exponential notation.  This field is ignored if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
     * <code>useExponentialNotation</code> is not true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
    private byte    minExponentDigits;       // Newly persistent in the Java 2 platform v.1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
     * The maximum number of digits allowed in the integer portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
     * <code>BigInteger</code> or <code>BigDecimal</code> number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
     * <code>maximumIntegerDigits</code> must be greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
     * <code>minimumIntegerDigits</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
     * @see #getMaximumIntegerDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
    private int    maximumIntegerDigits = super.getMaximumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
     * The minimum number of digits allowed in the integer portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
     * <code>BigInteger</code> or <code>BigDecimal</code> number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
     * <code>minimumIntegerDigits</code> must be less than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
     * <code>maximumIntegerDigits</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
     * @see #getMinimumIntegerDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
    private int    minimumIntegerDigits = super.getMinimumIntegerDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
     * The maximum number of digits allowed in the fractional portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
     * <code>BigInteger</code> or <code>BigDecimal</code> number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
     * <code>maximumFractionDigits</code> must be greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
     * <code>minimumFractionDigits</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
     * @see #getMaximumFractionDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
    private int    maximumFractionDigits = super.getMaximumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
     * The minimum number of digits allowed in the fractional portion of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
     * <code>BigInteger</code> or <code>BigDecimal</code> number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
     * <code>minimumFractionDigits</code> must be less than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
     * <code>maximumFractionDigits</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
     * @see #getMinimumFractionDigits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
    private int    minimumFractionDigits = super.getMinimumFractionDigits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
     * The {@link java.math.RoundingMode} used in this DecimalFormat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
    private RoundingMode roundingMode = RoundingMode.HALF_EVEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
    //----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
    static final int currentSerialVersion = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
     * The internal serial version which says which version was written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
     * Possible values are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
     * <li><b>0</b> (default): versions before the Java 2 platform v1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
     * <li><b>1</b>: version for 1.2, which includes the two new fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     *      <code>useExponentialNotation</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
     *      <code>minExponentDigits</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
     * <li><b>2</b>: version for 1.3 and later, which adds four new fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
     *      <code>posPrefixPattern</code>, <code>posSuffixPattern</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
     *      <code>negPrefixPattern</code>, and <code>negSuffixPattern</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
     * <li><b>3</b>: version for 1.5 and later, which adds five new fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
     *      <code>maximumIntegerDigits</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
     *      <code>minimumIntegerDigits</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
     *      <code>maximumFractionDigits</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
     *      <code>minimumFractionDigits</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
     *      <code>parseBigDecimal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
     * <li><b>4</b>: version for 1.6 and later, which adds one new field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
     *      <code>roundingMode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
    private int serialVersionOnStream = currentSerialVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
    //----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
    // CONSTANTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
    //----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
    // Constants for characters used in programmatic (unlocalized) patterns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
    private static final char       PATTERN_ZERO_DIGIT         = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
    private static final char       PATTERN_GROUPING_SEPARATOR = ',';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
    private static final char       PATTERN_DECIMAL_SEPARATOR  = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
    private static final char       PATTERN_PER_MILLE          = '\u2030';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
    private static final char       PATTERN_PERCENT            = '%';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
    private static final char       PATTERN_DIGIT              = '#';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
    private static final char       PATTERN_SEPARATOR          = ';';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
    private static final String     PATTERN_EXPONENT           = "E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
    private static final char       PATTERN_MINUS              = '-';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * The CURRENCY_SIGN is the standard Unicode symbol for currency.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     * is used in patterns and substituted with either the currency symbol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     * or if it is doubled, with the international currency symbol.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
     * CURRENCY_SIGN is seen in a pattern, then the decimal separator is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
     * replaced with the monetary decimal separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
     * The CURRENCY_SIGN is not localized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
    private static final char       CURRENCY_SIGN = '\u00A4';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
    private static final char       QUOTE = '\'';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
    private static FieldPosition[] EmptyFieldPositionArray = new FieldPosition[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
    // Upper limit on integer and fraction digits for a Java double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
    static final int DOUBLE_INTEGER_DIGITS  = 309;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
    static final int DOUBLE_FRACTION_DIGITS = 340;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
    // Upper limit on integer and fraction digits for BigDecimal and BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
    static final int MAXIMUM_INTEGER_DIGITS  = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
    static final int MAXIMUM_FRACTION_DIGITS = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
    // Proclaim JDK 1.1 serial compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
    static final long serialVersionUID = 864413376551465018L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
     * Cache to hold the NumberPattern of a Locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
     */
7003
7d8d9506b4ee 6991380: (cal) Calendar.cachedLocaleData should be transitioned from Hashtable to ConcurrentHashMap
okutsu
parents: 6489
diff changeset
  3272
    private static final ConcurrentMap<Locale, String> cachedLocaleData
7d8d9506b4ee 6991380: (cal) Calendar.cachedLocaleData should be transitioned from Hashtable to ConcurrentHashMap
okutsu
parents: 6489
diff changeset
  3273
        = new ConcurrentHashMap<Locale, String>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
}