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