rt/emul/compact/src/main/java/java/text/DecimalFormatSymbols.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:40:35 +0200
branchjdk7-b147
changeset 1334 588d5bf7a560
child 1339 8cc04f85a683
permissions -rw-r--r--
Set of JDK classes needed to run javac
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.IOException;
jtulach@1334
    42
import java.io.ObjectInputStream;
jtulach@1334
    43
import java.io.Serializable;
jtulach@1334
    44
import java.text.spi.DecimalFormatSymbolsProvider;
jtulach@1334
    45
import java.util.Currency;
jtulach@1334
    46
import java.util.Locale;
jtulach@1334
    47
import java.util.ResourceBundle;
jtulach@1334
    48
import java.util.concurrent.ConcurrentHashMap;
jtulach@1334
    49
jtulach@1334
    50
import sun.util.LocaleServiceProviderPool;
jtulach@1334
    51
import sun.util.resources.LocaleData;
jtulach@1334
    52
jtulach@1334
    53
/**
jtulach@1334
    54
 * This class represents the set of symbols (such as the decimal separator,
jtulach@1334
    55
 * the grouping separator, and so on) needed by <code>DecimalFormat</code>
jtulach@1334
    56
 * to format numbers. <code>DecimalFormat</code> creates for itself an instance of
jtulach@1334
    57
 * <code>DecimalFormatSymbols</code> from its locale data.  If you need to change any
jtulach@1334
    58
 * of these symbols, you can get the <code>DecimalFormatSymbols</code> object from
jtulach@1334
    59
 * your <code>DecimalFormat</code> and modify it.
jtulach@1334
    60
 *
jtulach@1334
    61
 * @see          java.util.Locale
jtulach@1334
    62
 * @see          DecimalFormat
jtulach@1334
    63
 * @author       Mark Davis
jtulach@1334
    64
 * @author       Alan Liu
jtulach@1334
    65
 */
jtulach@1334
    66
jtulach@1334
    67
public class DecimalFormatSymbols implements Cloneable, Serializable {
jtulach@1334
    68
jtulach@1334
    69
    /**
jtulach@1334
    70
     * Create a DecimalFormatSymbols object for the default locale.
jtulach@1334
    71
     * This constructor can only construct instances for the locales
jtulach@1334
    72
     * supported by the Java runtime environment, not for those
jtulach@1334
    73
     * supported by installed
jtulach@1334
    74
     * {@link java.text.spi.DecimalFormatSymbolsProvider DecimalFormatSymbolsProvider}
jtulach@1334
    75
     * implementations. For full locale coverage, use the
jtulach@1334
    76
     * {@link #getInstance(Locale) getInstance} method.
jtulach@1334
    77
     */
jtulach@1334
    78
    public DecimalFormatSymbols() {
jtulach@1334
    79
        initialize( Locale.getDefault(Locale.Category.FORMAT) );
jtulach@1334
    80
    }
jtulach@1334
    81
jtulach@1334
    82
    /**
jtulach@1334
    83
     * Create a DecimalFormatSymbols object for the given locale.
jtulach@1334
    84
     * This constructor can only construct instances for the locales
jtulach@1334
    85
     * supported by the Java runtime environment, not for those
jtulach@1334
    86
     * supported by installed
jtulach@1334
    87
     * {@link java.text.spi.DecimalFormatSymbolsProvider DecimalFormatSymbolsProvider}
jtulach@1334
    88
     * implementations. For full locale coverage, use the
jtulach@1334
    89
     * {@link #getInstance(Locale) getInstance} method.
jtulach@1334
    90
     *
jtulach@1334
    91
     * @exception NullPointerException if <code>locale</code> is null
jtulach@1334
    92
     */
jtulach@1334
    93
    public DecimalFormatSymbols( Locale locale ) {
jtulach@1334
    94
        initialize( locale );
jtulach@1334
    95
    }
jtulach@1334
    96
jtulach@1334
    97
    /**
jtulach@1334
    98
     * Returns an array of all locales for which the
jtulach@1334
    99
     * <code>getInstance</code> methods of this class can return
jtulach@1334
   100
     * localized instances.
jtulach@1334
   101
     * The returned array represents the union of locales supported by the Java
jtulach@1334
   102
     * runtime and by installed
jtulach@1334
   103
     * {@link java.text.spi.DecimalFormatSymbolsProvider DecimalFormatSymbolsProvider}
jtulach@1334
   104
     * implementations.  It must contain at least a <code>Locale</code>
jtulach@1334
   105
     * instance equal to {@link java.util.Locale#US Locale.US}.
jtulach@1334
   106
     *
jtulach@1334
   107
     * @return An array of locales for which localized
jtulach@1334
   108
     *         <code>DecimalFormatSymbols</code> instances are available.
jtulach@1334
   109
     * @since 1.6
jtulach@1334
   110
     */
jtulach@1334
   111
    public static Locale[] getAvailableLocales() {
jtulach@1334
   112
        LocaleServiceProviderPool pool =
jtulach@1334
   113
            LocaleServiceProviderPool.getPool(DecimalFormatSymbolsProvider.class);
jtulach@1334
   114
        return pool.getAvailableLocales();
jtulach@1334
   115
    }
jtulach@1334
   116
jtulach@1334
   117
    /**
jtulach@1334
   118
     * Gets the <code>DecimalFormatSymbols</code> instance for the default
jtulach@1334
   119
     * locale.  This method provides access to <code>DecimalFormatSymbols</code>
jtulach@1334
   120
     * instances for locales supported by the Java runtime itself as well
jtulach@1334
   121
     * as for those supported by installed
jtulach@1334
   122
     * {@link java.text.spi.DecimalFormatSymbolsProvider
jtulach@1334
   123
     * DecimalFormatSymbolsProvider} implementations.
jtulach@1334
   124
     * @return a <code>DecimalFormatSymbols</code> instance.
jtulach@1334
   125
     * @since 1.6
jtulach@1334
   126
     */
jtulach@1334
   127
    public static final DecimalFormatSymbols getInstance() {
jtulach@1334
   128
        return getInstance(Locale.getDefault(Locale.Category.FORMAT));
jtulach@1334
   129
    }
jtulach@1334
   130
jtulach@1334
   131
    /**
jtulach@1334
   132
     * Gets the <code>DecimalFormatSymbols</code> instance for the specified
jtulach@1334
   133
     * locale.  This method provides access to <code>DecimalFormatSymbols</code>
jtulach@1334
   134
     * instances for locales supported by the Java runtime itself as well
jtulach@1334
   135
     * as for those supported by installed
jtulach@1334
   136
     * {@link java.text.spi.DecimalFormatSymbolsProvider
jtulach@1334
   137
     * DecimalFormatSymbolsProvider} implementations.
jtulach@1334
   138
     * @param locale the desired locale.
jtulach@1334
   139
     * @return a <code>DecimalFormatSymbols</code> instance.
jtulach@1334
   140
     * @exception NullPointerException if <code>locale</code> is null
jtulach@1334
   141
     * @since 1.6
jtulach@1334
   142
     */
jtulach@1334
   143
    public static final DecimalFormatSymbols getInstance(Locale locale) {
jtulach@1334
   144
jtulach@1334
   145
        // Check whether a provider can provide an implementation that's closer
jtulach@1334
   146
        // to the requested locale than what the Java runtime itself can provide.
jtulach@1334
   147
        LocaleServiceProviderPool pool =
jtulach@1334
   148
            LocaleServiceProviderPool.getPool(DecimalFormatSymbolsProvider.class);
jtulach@1334
   149
        if (pool.hasProviders()) {
jtulach@1334
   150
            DecimalFormatSymbols providersInstance = pool.getLocalizedObject(
jtulach@1334
   151
                                DecimalFormatSymbolsGetter.INSTANCE, locale);
jtulach@1334
   152
            if (providersInstance != null) {
jtulach@1334
   153
                return providersInstance;
jtulach@1334
   154
            }
jtulach@1334
   155
        }
jtulach@1334
   156
jtulach@1334
   157
        return new DecimalFormatSymbols(locale);
jtulach@1334
   158
    }
jtulach@1334
   159
jtulach@1334
   160
    /**
jtulach@1334
   161
     * Gets the character used for zero. Different for Arabic, etc.
jtulach@1334
   162
     */
jtulach@1334
   163
    public char getZeroDigit() {
jtulach@1334
   164
        return zeroDigit;
jtulach@1334
   165
    }
jtulach@1334
   166
jtulach@1334
   167
    /**
jtulach@1334
   168
     * Sets the character used for zero. Different for Arabic, etc.
jtulach@1334
   169
     */
jtulach@1334
   170
    public void setZeroDigit(char zeroDigit) {
jtulach@1334
   171
        this.zeroDigit = zeroDigit;
jtulach@1334
   172
    }
jtulach@1334
   173
jtulach@1334
   174
    /**
jtulach@1334
   175
     * Gets the character used for thousands separator. Different for French, etc.
jtulach@1334
   176
     */
jtulach@1334
   177
    public char getGroupingSeparator() {
jtulach@1334
   178
        return groupingSeparator;
jtulach@1334
   179
    }
jtulach@1334
   180
jtulach@1334
   181
    /**
jtulach@1334
   182
     * Sets the character used for thousands separator. Different for French, etc.
jtulach@1334
   183
     */
jtulach@1334
   184
    public void setGroupingSeparator(char groupingSeparator) {
jtulach@1334
   185
        this.groupingSeparator = groupingSeparator;
jtulach@1334
   186
    }
jtulach@1334
   187
jtulach@1334
   188
    /**
jtulach@1334
   189
     * Gets the character used for decimal sign. Different for French, etc.
jtulach@1334
   190
     */
jtulach@1334
   191
    public char getDecimalSeparator() {
jtulach@1334
   192
        return decimalSeparator;
jtulach@1334
   193
    }
jtulach@1334
   194
jtulach@1334
   195
    /**
jtulach@1334
   196
     * Sets the character used for decimal sign. Different for French, etc.
jtulach@1334
   197
     */
jtulach@1334
   198
    public void setDecimalSeparator(char decimalSeparator) {
jtulach@1334
   199
        this.decimalSeparator = decimalSeparator;
jtulach@1334
   200
    }
jtulach@1334
   201
jtulach@1334
   202
    /**
jtulach@1334
   203
     * Gets the character used for per mille sign. Different for Arabic, etc.
jtulach@1334
   204
     */
jtulach@1334
   205
    public char getPerMill() {
jtulach@1334
   206
        return perMill;
jtulach@1334
   207
    }
jtulach@1334
   208
jtulach@1334
   209
    /**
jtulach@1334
   210
     * Sets the character used for per mille sign. Different for Arabic, etc.
jtulach@1334
   211
     */
jtulach@1334
   212
    public void setPerMill(char perMill) {
jtulach@1334
   213
        this.perMill = perMill;
jtulach@1334
   214
    }
jtulach@1334
   215
jtulach@1334
   216
    /**
jtulach@1334
   217
     * Gets the character used for percent sign. Different for Arabic, etc.
jtulach@1334
   218
     */
jtulach@1334
   219
    public char getPercent() {
jtulach@1334
   220
        return percent;
jtulach@1334
   221
    }
jtulach@1334
   222
jtulach@1334
   223
    /**
jtulach@1334
   224
     * Sets the character used for percent sign. Different for Arabic, etc.
jtulach@1334
   225
     */
jtulach@1334
   226
    public void setPercent(char percent) {
jtulach@1334
   227
        this.percent = percent;
jtulach@1334
   228
    }
jtulach@1334
   229
jtulach@1334
   230
    /**
jtulach@1334
   231
     * Gets the character used for a digit in a pattern.
jtulach@1334
   232
     */
jtulach@1334
   233
    public char getDigit() {
jtulach@1334
   234
        return digit;
jtulach@1334
   235
    }
jtulach@1334
   236
jtulach@1334
   237
    /**
jtulach@1334
   238
     * Sets the character used for a digit in a pattern.
jtulach@1334
   239
     */
jtulach@1334
   240
    public void setDigit(char digit) {
jtulach@1334
   241
        this.digit = digit;
jtulach@1334
   242
    }
jtulach@1334
   243
jtulach@1334
   244
    /**
jtulach@1334
   245
     * Gets the character used to separate positive and negative subpatterns
jtulach@1334
   246
     * in a pattern.
jtulach@1334
   247
     */
jtulach@1334
   248
    public char getPatternSeparator() {
jtulach@1334
   249
        return patternSeparator;
jtulach@1334
   250
    }
jtulach@1334
   251
jtulach@1334
   252
    /**
jtulach@1334
   253
     * Sets the character used to separate positive and negative subpatterns
jtulach@1334
   254
     * in a pattern.
jtulach@1334
   255
     */
jtulach@1334
   256
    public void setPatternSeparator(char patternSeparator) {
jtulach@1334
   257
        this.patternSeparator = patternSeparator;
jtulach@1334
   258
    }
jtulach@1334
   259
jtulach@1334
   260
    /**
jtulach@1334
   261
     * Gets the string used to represent infinity. Almost always left
jtulach@1334
   262
     * unchanged.
jtulach@1334
   263
     */
jtulach@1334
   264
    public String getInfinity() {
jtulach@1334
   265
        return infinity;
jtulach@1334
   266
    }
jtulach@1334
   267
jtulach@1334
   268
    /**
jtulach@1334
   269
     * Sets the string used to represent infinity. Almost always left
jtulach@1334
   270
     * unchanged.
jtulach@1334
   271
     */
jtulach@1334
   272
    public void setInfinity(String infinity) {
jtulach@1334
   273
        this.infinity = infinity;
jtulach@1334
   274
    }
jtulach@1334
   275
jtulach@1334
   276
    /**
jtulach@1334
   277
     * Gets the string used to represent "not a number". Almost always left
jtulach@1334
   278
     * unchanged.
jtulach@1334
   279
     */
jtulach@1334
   280
    public String getNaN() {
jtulach@1334
   281
        return NaN;
jtulach@1334
   282
    }
jtulach@1334
   283
jtulach@1334
   284
    /**
jtulach@1334
   285
     * Sets the string used to represent "not a number". Almost always left
jtulach@1334
   286
     * unchanged.
jtulach@1334
   287
     */
jtulach@1334
   288
    public void setNaN(String NaN) {
jtulach@1334
   289
        this.NaN = NaN;
jtulach@1334
   290
    }
jtulach@1334
   291
jtulach@1334
   292
    /**
jtulach@1334
   293
     * Gets the character used to represent minus sign. If no explicit
jtulach@1334
   294
     * negative format is specified, one is formed by prefixing
jtulach@1334
   295
     * minusSign to the positive format.
jtulach@1334
   296
     */
jtulach@1334
   297
    public char getMinusSign() {
jtulach@1334
   298
        return minusSign;
jtulach@1334
   299
    }
jtulach@1334
   300
jtulach@1334
   301
    /**
jtulach@1334
   302
     * Sets the character used to represent minus sign. If no explicit
jtulach@1334
   303
     * negative format is specified, one is formed by prefixing
jtulach@1334
   304
     * minusSign to the positive format.
jtulach@1334
   305
     */
jtulach@1334
   306
    public void setMinusSign(char minusSign) {
jtulach@1334
   307
        this.minusSign = minusSign;
jtulach@1334
   308
    }
jtulach@1334
   309
jtulach@1334
   310
    /**
jtulach@1334
   311
     * Returns the currency symbol for the currency of these
jtulach@1334
   312
     * DecimalFormatSymbols in their locale.
jtulach@1334
   313
     * @since 1.2
jtulach@1334
   314
     */
jtulach@1334
   315
    public String getCurrencySymbol()
jtulach@1334
   316
    {
jtulach@1334
   317
        return currencySymbol;
jtulach@1334
   318
    }
jtulach@1334
   319
jtulach@1334
   320
    /**
jtulach@1334
   321
     * Sets the currency symbol for the currency of these
jtulach@1334
   322
     * DecimalFormatSymbols in their locale.
jtulach@1334
   323
     * @since 1.2
jtulach@1334
   324
     */
jtulach@1334
   325
    public void setCurrencySymbol(String currency)
jtulach@1334
   326
    {
jtulach@1334
   327
        currencySymbol = currency;
jtulach@1334
   328
    }
jtulach@1334
   329
jtulach@1334
   330
    /**
jtulach@1334
   331
     * Returns the ISO 4217 currency code of the currency of these
jtulach@1334
   332
     * DecimalFormatSymbols.
jtulach@1334
   333
     * @since 1.2
jtulach@1334
   334
     */
jtulach@1334
   335
    public String getInternationalCurrencySymbol()
jtulach@1334
   336
    {
jtulach@1334
   337
        return intlCurrencySymbol;
jtulach@1334
   338
    }
jtulach@1334
   339
jtulach@1334
   340
    /**
jtulach@1334
   341
     * Sets the ISO 4217 currency code of the currency of these
jtulach@1334
   342
     * DecimalFormatSymbols.
jtulach@1334
   343
     * If the currency code is valid (as defined by
jtulach@1334
   344
     * {@link java.util.Currency#getInstance(java.lang.String) Currency.getInstance}),
jtulach@1334
   345
     * this also sets the currency attribute to the corresponding Currency
jtulach@1334
   346
     * instance and the currency symbol attribute to the currency's symbol
jtulach@1334
   347
     * in the DecimalFormatSymbols' locale. If the currency code is not valid,
jtulach@1334
   348
     * then the currency attribute is set to null and the currency symbol
jtulach@1334
   349
     * attribute is not modified.
jtulach@1334
   350
     *
jtulach@1334
   351
     * @see #setCurrency
jtulach@1334
   352
     * @see #setCurrencySymbol
jtulach@1334
   353
     * @since 1.2
jtulach@1334
   354
     */
jtulach@1334
   355
    public void setInternationalCurrencySymbol(String currencyCode)
jtulach@1334
   356
    {
jtulach@1334
   357
        intlCurrencySymbol = currencyCode;
jtulach@1334
   358
        currency = null;
jtulach@1334
   359
        if (currencyCode != null) {
jtulach@1334
   360
            try {
jtulach@1334
   361
                currency = Currency.getInstance(currencyCode);
jtulach@1334
   362
                currencySymbol = currency.getSymbol();
jtulach@1334
   363
            } catch (IllegalArgumentException e) {
jtulach@1334
   364
            }
jtulach@1334
   365
        }
jtulach@1334
   366
    }
jtulach@1334
   367
jtulach@1334
   368
    /**
jtulach@1334
   369
     * Gets the currency of these DecimalFormatSymbols. May be null if the
jtulach@1334
   370
     * currency symbol attribute was previously set to a value that's not
jtulach@1334
   371
     * a valid ISO 4217 currency code.
jtulach@1334
   372
     *
jtulach@1334
   373
     * @return the currency used, or null
jtulach@1334
   374
     * @since 1.4
jtulach@1334
   375
     */
jtulach@1334
   376
    public Currency getCurrency() {
jtulach@1334
   377
        return currency;
jtulach@1334
   378
    }
jtulach@1334
   379
jtulach@1334
   380
    /**
jtulach@1334
   381
     * Sets the currency of these DecimalFormatSymbols.
jtulach@1334
   382
     * This also sets the currency symbol attribute to the currency's symbol
jtulach@1334
   383
     * in the DecimalFormatSymbols' locale, and the international currency
jtulach@1334
   384
     * symbol attribute to the currency's ISO 4217 currency code.
jtulach@1334
   385
     *
jtulach@1334
   386
     * @param currency the new currency to be used
jtulach@1334
   387
     * @exception NullPointerException if <code>currency</code> is null
jtulach@1334
   388
     * @since 1.4
jtulach@1334
   389
     * @see #setCurrencySymbol
jtulach@1334
   390
     * @see #setInternationalCurrencySymbol
jtulach@1334
   391
     */
jtulach@1334
   392
    public void setCurrency(Currency currency) {
jtulach@1334
   393
        if (currency == null) {
jtulach@1334
   394
            throw new NullPointerException();
jtulach@1334
   395
        }
jtulach@1334
   396
        this.currency = currency;
jtulach@1334
   397
        intlCurrencySymbol = currency.getCurrencyCode();
jtulach@1334
   398
        currencySymbol = currency.getSymbol(locale);
jtulach@1334
   399
    }
jtulach@1334
   400
jtulach@1334
   401
jtulach@1334
   402
    /**
jtulach@1334
   403
     * Returns the monetary decimal separator.
jtulach@1334
   404
     * @since 1.2
jtulach@1334
   405
     */
jtulach@1334
   406
    public char getMonetaryDecimalSeparator()
jtulach@1334
   407
    {
jtulach@1334
   408
        return monetarySeparator;
jtulach@1334
   409
    }
jtulach@1334
   410
jtulach@1334
   411
    /**
jtulach@1334
   412
     * Sets the monetary decimal separator.
jtulach@1334
   413
     * @since 1.2
jtulach@1334
   414
     */
jtulach@1334
   415
    public void setMonetaryDecimalSeparator(char sep)
jtulach@1334
   416
    {
jtulach@1334
   417
        monetarySeparator = sep;
jtulach@1334
   418
    }
jtulach@1334
   419
jtulach@1334
   420
    //------------------------------------------------------------
jtulach@1334
   421
    // BEGIN   Package Private methods ... to be made public later
jtulach@1334
   422
    //------------------------------------------------------------
jtulach@1334
   423
jtulach@1334
   424
    /**
jtulach@1334
   425
     * Returns the character used to separate the mantissa from the exponent.
jtulach@1334
   426
     */
jtulach@1334
   427
    char getExponentialSymbol()
jtulach@1334
   428
    {
jtulach@1334
   429
        return exponential;
jtulach@1334
   430
    }
jtulach@1334
   431
  /**
jtulach@1334
   432
   * Returns the string used to separate the mantissa from the exponent.
jtulach@1334
   433
   * Examples: "x10^" for 1.23x10^4, "E" for 1.23E4.
jtulach@1334
   434
   *
jtulach@1334
   435
   * @return the exponent separator string
jtulach@1334
   436
   * @see #setExponentSeparator(java.lang.String)
jtulach@1334
   437
   * @since 1.6
jtulach@1334
   438
   */
jtulach@1334
   439
    public String getExponentSeparator()
jtulach@1334
   440
    {
jtulach@1334
   441
        return exponentialSeparator;
jtulach@1334
   442
    }
jtulach@1334
   443
jtulach@1334
   444
    /**
jtulach@1334
   445
     * Sets the character used to separate the mantissa from the exponent.
jtulach@1334
   446
     */
jtulach@1334
   447
    void setExponentialSymbol(char exp)
jtulach@1334
   448
    {
jtulach@1334
   449
        exponential = exp;
jtulach@1334
   450
    }
jtulach@1334
   451
jtulach@1334
   452
  /**
jtulach@1334
   453
   * Sets the string used to separate the mantissa from the exponent.
jtulach@1334
   454
   * Examples: "x10^" for 1.23x10^4, "E" for 1.23E4.
jtulach@1334
   455
   *
jtulach@1334
   456
   * @param exp the exponent separator string
jtulach@1334
   457
   * @exception NullPointerException if <code>exp</code> is null
jtulach@1334
   458
   * @see #getExponentSeparator()
jtulach@1334
   459
   * @since 1.6
jtulach@1334
   460
   */
jtulach@1334
   461
    public void setExponentSeparator(String exp)
jtulach@1334
   462
    {
jtulach@1334
   463
        if (exp == null) {
jtulach@1334
   464
            throw new NullPointerException();
jtulach@1334
   465
        }
jtulach@1334
   466
        exponentialSeparator = exp;
jtulach@1334
   467
     }
jtulach@1334
   468
jtulach@1334
   469
jtulach@1334
   470
    //------------------------------------------------------------
jtulach@1334
   471
    // END     Package Private methods ... to be made public later
jtulach@1334
   472
    //------------------------------------------------------------
jtulach@1334
   473
jtulach@1334
   474
    /**
jtulach@1334
   475
     * Standard override.
jtulach@1334
   476
     */
jtulach@1334
   477
    public Object clone() {
jtulach@1334
   478
        try {
jtulach@1334
   479
            return (DecimalFormatSymbols)super.clone();
jtulach@1334
   480
            // other fields are bit-copied
jtulach@1334
   481
        } catch (CloneNotSupportedException e) {
jtulach@1334
   482
            throw new InternalError();
jtulach@1334
   483
        }
jtulach@1334
   484
    }
jtulach@1334
   485
jtulach@1334
   486
    /**
jtulach@1334
   487
     * Override equals.
jtulach@1334
   488
     */
jtulach@1334
   489
    public boolean equals(Object obj) {
jtulach@1334
   490
        if (obj == null) return false;
jtulach@1334
   491
        if (this == obj) return true;
jtulach@1334
   492
        if (getClass() != obj.getClass()) return false;
jtulach@1334
   493
        DecimalFormatSymbols other = (DecimalFormatSymbols) obj;
jtulach@1334
   494
        return (zeroDigit == other.zeroDigit &&
jtulach@1334
   495
        groupingSeparator == other.groupingSeparator &&
jtulach@1334
   496
        decimalSeparator == other.decimalSeparator &&
jtulach@1334
   497
        percent == other.percent &&
jtulach@1334
   498
        perMill == other.perMill &&
jtulach@1334
   499
        digit == other.digit &&
jtulach@1334
   500
        minusSign == other.minusSign &&
jtulach@1334
   501
        patternSeparator == other.patternSeparator &&
jtulach@1334
   502
        infinity.equals(other.infinity) &&
jtulach@1334
   503
        NaN.equals(other.NaN) &&
jtulach@1334
   504
        currencySymbol.equals(other.currencySymbol) &&
jtulach@1334
   505
        intlCurrencySymbol.equals(other.intlCurrencySymbol) &&
jtulach@1334
   506
        currency == other.currency &&
jtulach@1334
   507
        monetarySeparator == other.monetarySeparator &&
jtulach@1334
   508
        exponentialSeparator.equals(other.exponentialSeparator) &&
jtulach@1334
   509
        locale.equals(other.locale));
jtulach@1334
   510
    }
jtulach@1334
   511
jtulach@1334
   512
    /**
jtulach@1334
   513
     * Override hashCode.
jtulach@1334
   514
     */
jtulach@1334
   515
    public int hashCode() {
jtulach@1334
   516
            int result = zeroDigit;
jtulach@1334
   517
            result = result * 37 + groupingSeparator;
jtulach@1334
   518
            result = result * 37 + decimalSeparator;
jtulach@1334
   519
            return result;
jtulach@1334
   520
    }
jtulach@1334
   521
jtulach@1334
   522
    /**
jtulach@1334
   523
     * Initializes the symbols from the FormatData resource bundle.
jtulach@1334
   524
     */
jtulach@1334
   525
    private void initialize( Locale locale ) {
jtulach@1334
   526
        this.locale = locale;
jtulach@1334
   527
jtulach@1334
   528
        // get resource bundle data - try the cache first
jtulach@1334
   529
        boolean needCacheUpdate = false;
jtulach@1334
   530
        Object[] data = cachedLocaleData.get(locale);
jtulach@1334
   531
        if (data == null) {  /* cache miss */
jtulach@1334
   532
            // When numbering system is thai (Locale's extension contains u-nu-thai),
jtulach@1334
   533
            // we read the data from th_TH_TH.
jtulach@1334
   534
            Locale lookupLocale = locale;
jtulach@1334
   535
            String numberType = locale.getUnicodeLocaleType("nu");
jtulach@1334
   536
            if (numberType != null && numberType.equals("thai")) {
jtulach@1334
   537
                lookupLocale = new Locale("th", "TH", "TH");
jtulach@1334
   538
            }
jtulach@1334
   539
            data = new Object[3];
jtulach@1334
   540
            ResourceBundle rb = LocaleData.getNumberFormatData(lookupLocale);
jtulach@1334
   541
            data[0] = rb.getStringArray("NumberElements");
jtulach@1334
   542
            needCacheUpdate = true;
jtulach@1334
   543
        }
jtulach@1334
   544
jtulach@1334
   545
        String[] numberElements = (String[]) data[0];
jtulach@1334
   546
jtulach@1334
   547
        decimalSeparator = numberElements[0].charAt(0);
jtulach@1334
   548
        groupingSeparator = numberElements[1].charAt(0);
jtulach@1334
   549
        patternSeparator = numberElements[2].charAt(0);
jtulach@1334
   550
        percent = numberElements[3].charAt(0);
jtulach@1334
   551
        zeroDigit = numberElements[4].charAt(0); //different for Arabic,etc.
jtulach@1334
   552
        digit = numberElements[5].charAt(0);
jtulach@1334
   553
        minusSign = numberElements[6].charAt(0);
jtulach@1334
   554
        exponential = numberElements[7].charAt(0);
jtulach@1334
   555
        exponentialSeparator = numberElements[7]; //string representation new since 1.6
jtulach@1334
   556
        perMill = numberElements[8].charAt(0);
jtulach@1334
   557
        infinity  = numberElements[9];
jtulach@1334
   558
        NaN = numberElements[10];
jtulach@1334
   559
jtulach@1334
   560
        // Try to obtain the currency used in the locale's country.
jtulach@1334
   561
        // Check for empty country string separately because it's a valid
jtulach@1334
   562
        // country ID for Locale (and used for the C locale), but not a valid
jtulach@1334
   563
        // ISO 3166 country code, and exceptions are expensive.
jtulach@1334
   564
        if (!"".equals(locale.getCountry())) {
jtulach@1334
   565
            try {
jtulach@1334
   566
                currency = Currency.getInstance(locale);
jtulach@1334
   567
            } catch (IllegalArgumentException e) {
jtulach@1334
   568
                // use default values below for compatibility
jtulach@1334
   569
            }
jtulach@1334
   570
        }
jtulach@1334
   571
        if (currency != null) {
jtulach@1334
   572
            intlCurrencySymbol = currency.getCurrencyCode();
jtulach@1334
   573
            if (data[1] != null && data[1] == intlCurrencySymbol) {
jtulach@1334
   574
                currencySymbol = (String) data[2];
jtulach@1334
   575
            } else {
jtulach@1334
   576
                currencySymbol = currency.getSymbol(locale);
jtulach@1334
   577
                data[1] = intlCurrencySymbol;
jtulach@1334
   578
                data[2] = currencySymbol;
jtulach@1334
   579
                needCacheUpdate = true;
jtulach@1334
   580
            }
jtulach@1334
   581
        } else {
jtulach@1334
   582
            // default values
jtulach@1334
   583
            intlCurrencySymbol = "XXX";
jtulach@1334
   584
            try {
jtulach@1334
   585
                currency = Currency.getInstance(intlCurrencySymbol);
jtulach@1334
   586
            } catch (IllegalArgumentException e) {
jtulach@1334
   587
            }
jtulach@1334
   588
            currencySymbol = "\u00A4";
jtulach@1334
   589
        }
jtulach@1334
   590
        // Currently the monetary decimal separator is the same as the
jtulach@1334
   591
        // standard decimal separator for all locales that we support.
jtulach@1334
   592
        // If that changes, add a new entry to NumberElements.
jtulach@1334
   593
        monetarySeparator = decimalSeparator;
jtulach@1334
   594
jtulach@1334
   595
        if (needCacheUpdate) {
jtulach@1334
   596
            cachedLocaleData.putIfAbsent(locale, data);
jtulach@1334
   597
        }
jtulach@1334
   598
    }
jtulach@1334
   599
jtulach@1334
   600
    /**
jtulach@1334
   601
     * Reads the default serializable fields, provides default values for objects
jtulach@1334
   602
     * in older serial versions, and initializes non-serializable fields.
jtulach@1334
   603
     * If <code>serialVersionOnStream</code>
jtulach@1334
   604
     * is less than 1, initializes <code>monetarySeparator</code> to be
jtulach@1334
   605
     * the same as <code>decimalSeparator</code> and <code>exponential</code>
jtulach@1334
   606
     * to be 'E'.
jtulach@1334
   607
     * If <code>serialVersionOnStream</code> is less than 2,
jtulach@1334
   608
     * initializes <code>locale</code>to the root locale, and initializes
jtulach@1334
   609
     * If <code>serialVersionOnStream</code> is less than 3, it initializes
jtulach@1334
   610
     * <code>exponentialSeparator</code> using <code>exponential</code>.
jtulach@1334
   611
     * Sets <code>serialVersionOnStream</code> back to the maximum allowed value so that
jtulach@1334
   612
     * default serialization will work properly if this object is streamed out again.
jtulach@1334
   613
     * Initializes the currency from the intlCurrencySymbol field.
jtulach@1334
   614
     *
jtulach@1334
   615
     * @since JDK 1.1.6
jtulach@1334
   616
     */
jtulach@1334
   617
    private void readObject(ObjectInputStream stream)
jtulach@1334
   618
            throws IOException, ClassNotFoundException {
jtulach@1334
   619
        stream.defaultReadObject();
jtulach@1334
   620
        if (serialVersionOnStream < 1) {
jtulach@1334
   621
            // Didn't have monetarySeparator or exponential field;
jtulach@1334
   622
            // use defaults.
jtulach@1334
   623
            monetarySeparator = decimalSeparator;
jtulach@1334
   624
            exponential       = 'E';
jtulach@1334
   625
        }
jtulach@1334
   626
        if (serialVersionOnStream < 2) {
jtulach@1334
   627
            // didn't have locale; use root locale
jtulach@1334
   628
            locale = Locale.ROOT;
jtulach@1334
   629
        }
jtulach@1334
   630
        if (serialVersionOnStream < 3) {
jtulach@1334
   631
            // didn't have exponentialSeparator. Create one using exponential
jtulach@1334
   632
            exponentialSeparator = Character.toString(exponential);
jtulach@1334
   633
        }
jtulach@1334
   634
        serialVersionOnStream = currentSerialVersion;
jtulach@1334
   635
jtulach@1334
   636
        if (intlCurrencySymbol != null) {
jtulach@1334
   637
            try {
jtulach@1334
   638
                 currency = Currency.getInstance(intlCurrencySymbol);
jtulach@1334
   639
            } catch (IllegalArgumentException e) {
jtulach@1334
   640
            }
jtulach@1334
   641
        }
jtulach@1334
   642
    }
jtulach@1334
   643
jtulach@1334
   644
    /**
jtulach@1334
   645
     * Character used for zero.
jtulach@1334
   646
     *
jtulach@1334
   647
     * @serial
jtulach@1334
   648
     * @see #getZeroDigit
jtulach@1334
   649
     */
jtulach@1334
   650
    private  char    zeroDigit;
jtulach@1334
   651
jtulach@1334
   652
    /**
jtulach@1334
   653
     * Character used for thousands separator.
jtulach@1334
   654
     *
jtulach@1334
   655
     * @serial
jtulach@1334
   656
     * @see #getGroupingSeparator
jtulach@1334
   657
     */
jtulach@1334
   658
    private  char    groupingSeparator;
jtulach@1334
   659
jtulach@1334
   660
    /**
jtulach@1334
   661
     * Character used for decimal sign.
jtulach@1334
   662
     *
jtulach@1334
   663
     * @serial
jtulach@1334
   664
     * @see #getDecimalSeparator
jtulach@1334
   665
     */
jtulach@1334
   666
    private  char    decimalSeparator;
jtulach@1334
   667
jtulach@1334
   668
    /**
jtulach@1334
   669
     * Character used for per mille sign.
jtulach@1334
   670
     *
jtulach@1334
   671
     * @serial
jtulach@1334
   672
     * @see #getPerMill
jtulach@1334
   673
     */
jtulach@1334
   674
    private  char    perMill;
jtulach@1334
   675
jtulach@1334
   676
    /**
jtulach@1334
   677
     * Character used for percent sign.
jtulach@1334
   678
     * @serial
jtulach@1334
   679
     * @see #getPercent
jtulach@1334
   680
     */
jtulach@1334
   681
    private  char    percent;
jtulach@1334
   682
jtulach@1334
   683
    /**
jtulach@1334
   684
     * Character used for a digit in a pattern.
jtulach@1334
   685
     *
jtulach@1334
   686
     * @serial
jtulach@1334
   687
     * @see #getDigit
jtulach@1334
   688
     */
jtulach@1334
   689
    private  char    digit;
jtulach@1334
   690
jtulach@1334
   691
    /**
jtulach@1334
   692
     * Character used to separate positive and negative subpatterns
jtulach@1334
   693
     * in a pattern.
jtulach@1334
   694
     *
jtulach@1334
   695
     * @serial
jtulach@1334
   696
     * @see #getPatternSeparator
jtulach@1334
   697
     */
jtulach@1334
   698
    private  char    patternSeparator;
jtulach@1334
   699
jtulach@1334
   700
    /**
jtulach@1334
   701
     * String used to represent infinity.
jtulach@1334
   702
     * @serial
jtulach@1334
   703
     * @see #getInfinity
jtulach@1334
   704
     */
jtulach@1334
   705
    private  String  infinity;
jtulach@1334
   706
jtulach@1334
   707
    /**
jtulach@1334
   708
     * String used to represent "not a number".
jtulach@1334
   709
     * @serial
jtulach@1334
   710
     * @see #getNaN
jtulach@1334
   711
     */
jtulach@1334
   712
    private  String  NaN;
jtulach@1334
   713
jtulach@1334
   714
    /**
jtulach@1334
   715
     * Character used to represent minus sign.
jtulach@1334
   716
     * @serial
jtulach@1334
   717
     * @see #getMinusSign
jtulach@1334
   718
     */
jtulach@1334
   719
    private  char    minusSign;
jtulach@1334
   720
jtulach@1334
   721
    /**
jtulach@1334
   722
     * String denoting the local currency, e.g. "$".
jtulach@1334
   723
     * @serial
jtulach@1334
   724
     * @see #getCurrencySymbol
jtulach@1334
   725
     */
jtulach@1334
   726
    private  String  currencySymbol;
jtulach@1334
   727
jtulach@1334
   728
    /**
jtulach@1334
   729
     * ISO 4217 currency code denoting the local currency, e.g. "USD".
jtulach@1334
   730
     * @serial
jtulach@1334
   731
     * @see #getInternationalCurrencySymbol
jtulach@1334
   732
     */
jtulach@1334
   733
    private  String  intlCurrencySymbol;
jtulach@1334
   734
jtulach@1334
   735
    /**
jtulach@1334
   736
     * The decimal separator used when formatting currency values.
jtulach@1334
   737
     * @serial
jtulach@1334
   738
     * @since JDK 1.1.6
jtulach@1334
   739
     * @see #getMonetaryDecimalSeparator
jtulach@1334
   740
     */
jtulach@1334
   741
    private  char    monetarySeparator; // Field new in JDK 1.1.6
jtulach@1334
   742
jtulach@1334
   743
    /**
jtulach@1334
   744
     * The character used to distinguish the exponent in a number formatted
jtulach@1334
   745
     * in exponential notation, e.g. 'E' for a number such as "1.23E45".
jtulach@1334
   746
     * <p>
jtulach@1334
   747
     * Note that the public API provides no way to set this field,
jtulach@1334
   748
     * even though it is supported by the implementation and the stream format.
jtulach@1334
   749
     * The intent is that this will be added to the API in the future.
jtulach@1334
   750
     *
jtulach@1334
   751
     * @serial
jtulach@1334
   752
     * @since JDK 1.1.6
jtulach@1334
   753
     */
jtulach@1334
   754
    private  char    exponential;       // Field new in JDK 1.1.6
jtulach@1334
   755
jtulach@1334
   756
  /**
jtulach@1334
   757
   * The string used to separate the mantissa from the exponent.
jtulach@1334
   758
   * Examples: "x10^" for 1.23x10^4, "E" for 1.23E4.
jtulach@1334
   759
   * <p>
jtulach@1334
   760
   * If both <code>exponential</code> and <code>exponentialSeparator</code>
jtulach@1334
   761
   * exist, this <code>exponentialSeparator</code> has the precedence.
jtulach@1334
   762
   *
jtulach@1334
   763
   * @serial
jtulach@1334
   764
   * @since 1.6
jtulach@1334
   765
   */
jtulach@1334
   766
    private  String    exponentialSeparator;       // Field new in JDK 1.6
jtulach@1334
   767
jtulach@1334
   768
    /**
jtulach@1334
   769
     * The locale of these currency format symbols.
jtulach@1334
   770
     *
jtulach@1334
   771
     * @serial
jtulach@1334
   772
     * @since 1.4
jtulach@1334
   773
     */
jtulach@1334
   774
    private Locale locale;
jtulach@1334
   775
jtulach@1334
   776
    // currency; only the ISO code is serialized.
jtulach@1334
   777
    private transient Currency currency;
jtulach@1334
   778
jtulach@1334
   779
    // Proclaim JDK 1.1 FCS compatibility
jtulach@1334
   780
    static final long serialVersionUID = 5772796243397350300L;
jtulach@1334
   781
jtulach@1334
   782
    // The internal serial version which says which version was written
jtulach@1334
   783
    // - 0 (default) for version up to JDK 1.1.5
jtulach@1334
   784
    // - 1 for version from JDK 1.1.6, which includes two new fields:
jtulach@1334
   785
    //     monetarySeparator and exponential.
jtulach@1334
   786
    // - 2 for version from J2SE 1.4, which includes locale field.
jtulach@1334
   787
    // - 3 for version from J2SE 1.6, which includes exponentialSeparator field.
jtulach@1334
   788
    private static final int currentSerialVersion = 3;
jtulach@1334
   789
jtulach@1334
   790
    /**
jtulach@1334
   791
     * Describes the version of <code>DecimalFormatSymbols</code> present on the stream.
jtulach@1334
   792
     * Possible values are:
jtulach@1334
   793
     * <ul>
jtulach@1334
   794
     * <li><b>0</b> (or uninitialized): versions prior to JDK 1.1.6.
jtulach@1334
   795
     *
jtulach@1334
   796
     * <li><b>1</b>: Versions written by JDK 1.1.6 or later, which include
jtulach@1334
   797
     *      two new fields: <code>monetarySeparator</code> and <code>exponential</code>.
jtulach@1334
   798
     * <li><b>2</b>: Versions written by J2SE 1.4 or later, which include a
jtulach@1334
   799
     *      new <code>locale</code> field.
jtulach@1334
   800
     * <li><b>3</b>: Versions written by J2SE 1.6 or later, which include a
jtulach@1334
   801
     *      new <code>exponentialSeparator</code> field.
jtulach@1334
   802
     * </ul>
jtulach@1334
   803
     * When streaming out a <code>DecimalFormatSymbols</code>, the most recent format
jtulach@1334
   804
     * (corresponding to the highest allowable <code>serialVersionOnStream</code>)
jtulach@1334
   805
     * is always written.
jtulach@1334
   806
     *
jtulach@1334
   807
     * @serial
jtulach@1334
   808
     * @since JDK 1.1.6
jtulach@1334
   809
     */
jtulach@1334
   810
    private int serialVersionOnStream = currentSerialVersion;
jtulach@1334
   811
jtulach@1334
   812
    /**
jtulach@1334
   813
     * cache to hold the NumberElements and the Currency
jtulach@1334
   814
     * of a Locale.
jtulach@1334
   815
     */
jtulach@1334
   816
    private static final ConcurrentHashMap<Locale, Object[]> cachedLocaleData = new ConcurrentHashMap<Locale, Object[]>(3);
jtulach@1334
   817
jtulach@1334
   818
    /**
jtulach@1334
   819
     * Obtains a DecimalFormatSymbols instance from a DecimalFormatSymbolsProvider
jtulach@1334
   820
     * implementation.
jtulach@1334
   821
     */
jtulach@1334
   822
    private static class DecimalFormatSymbolsGetter
jtulach@1334
   823
        implements LocaleServiceProviderPool.LocalizedObjectGetter<DecimalFormatSymbolsProvider,
jtulach@1334
   824
                                                                   DecimalFormatSymbols> {
jtulach@1334
   825
        private static final DecimalFormatSymbolsGetter INSTANCE =
jtulach@1334
   826
            new DecimalFormatSymbolsGetter();
jtulach@1334
   827
jtulach@1334
   828
        public DecimalFormatSymbols getObject(
jtulach@1334
   829
                                DecimalFormatSymbolsProvider decimalFormatSymbolsProvider,
jtulach@1334
   830
                                Locale locale,
jtulach@1334
   831
                                String key,
jtulach@1334
   832
                                Object... params) {
jtulach@1334
   833
            assert params.length == 0;
jtulach@1334
   834
            return decimalFormatSymbolsProvider.getInstance(locale);
jtulach@1334
   835
        }
jtulach@1334
   836
    }
jtulach@1334
   837
}