jdk/src/share/classes/sun/util/locale/provider/NumberFormatProviderImpl.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 13583 dc0017b1a452
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     1
/*
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     4
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    10
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    15
 * accompanied this code).
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    16
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    20
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    23
 * questions.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    24
 */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    25
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    26
/*
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    27
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    28
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    29
 * (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    30
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    31
 * The original version of this source code and documentation
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    32
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    33
 * subsidiary of IBM. These materials are provided under terms
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    34
 * of a License Agreement between Taligent and Sun. This technology
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    35
 * is protected by multiple US and International patents.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    36
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    37
 * This notice and attribution to Taligent may not be removed.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    38
 * Taligent is a registered trademark of Taligent, Inc.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    39
 */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    40
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    41
package sun.util.locale.provider;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    42
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    43
import java.text.DecimalFormat;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    44
import java.text.DecimalFormatSymbols;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    45
import java.text.NumberFormat;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    46
import java.text.spi.NumberFormatProvider;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    47
import java.util.Currency;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    48
import java.util.Locale;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    49
import java.util.Set;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    50
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    51
/**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    52
 * Concrete implementation of the  {@link java.text.spi.NumberFormatProvider
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    53
 * NumberFormatProvider} class for the JRE LocaleProviderAdapter.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    54
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    55
 * @author Naoto Sato
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    56
 * @author Masayoshi Okutsu
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    57
 */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    58
public class NumberFormatProviderImpl extends NumberFormatProvider implements AvailableLanguageTags {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    59
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    60
    // Constants used by factory methods to specify a style of format.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    61
    private static final int NUMBERSTYLE = 0;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    62
    private static final int CURRENCYSTYLE = 1;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    63
    private static final int PERCENTSTYLE = 2;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    64
    private static final int SCIENTIFICSTYLE = 3;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    65
    private static final int INTEGERSTYLE = 4;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    66
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    67
    private final LocaleProviderAdapter.Type type;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    68
    private final Set<String> langtags;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    69
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    70
    public NumberFormatProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    71
        this.type = type;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    72
        this.langtags = langtags;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    73
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    74
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    75
    /**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    76
     * Returns an array of all locales for which this locale service provider
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    77
     * can provide localized objects or names.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    78
     *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    79
     * @return An array of all locales for which this locale service provider
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    80
     * can provide localized objects or names.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    81
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    82
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    83
    public Locale[] getAvailableLocales() {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    84
        return LocaleProviderAdapter.forType(type).getAvailableLocales();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    85
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    86
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    87
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    88
    public boolean isSupportedLocale(Locale locale) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    89
        return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    90
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    91
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    92
    /**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    93
     * Returns a new <code>NumberFormat</code> instance which formats
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    94
     * monetary values for the specified locale.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    95
     *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    96
     * @param locale the desired locale.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    97
     * @exception NullPointerException if <code>locale</code> is null
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    98
     * @exception IllegalArgumentException if <code>locale</code> isn't
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    99
     *     one of the locales returned from
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   100
     *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   101
     *     getAvailableLocales()}.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   102
     * @return a currency formatter
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   103
     * @see java.text.NumberFormat#getCurrencyInstance(java.util.Locale)
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   104
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   105
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   106
    public NumberFormat getCurrencyInstance(Locale locale) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   107
        return getInstance(locale, CURRENCYSTYLE);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   108
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   109
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   110
    /**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   111
     * Returns a new <code>NumberFormat</code> instance which formats
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   112
     * integer values for the specified locale.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   113
     * The returned number format is configured to
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   114
     * round floating point numbers to the nearest integer using
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   115
     * half-even rounding (see {@link java.math.RoundingMode#HALF_EVEN HALF_EVEN})
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   116
     * for formatting, and to parse only the integer part of
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   117
     * an input string (see {@link
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   118
     * java.text.NumberFormat#isParseIntegerOnly isParseIntegerOnly}).
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   119
     *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   120
     * @param locale the desired locale
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   121
     * @exception NullPointerException if <code>locale</code> is null
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   122
     * @exception IllegalArgumentException if <code>locale</code> isn't
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   123
     *     one of the locales returned from
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   124
     *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   125
     *     getAvailableLocales()}.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   126
     * @return a number format for integer values
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   127
     * @see java.text.NumberFormat#getIntegerInstance(java.util.Locale)
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   128
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   129
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   130
    public NumberFormat getIntegerInstance(Locale locale) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   131
        return getInstance(locale, INTEGERSTYLE);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   132
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   133
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   134
    /**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   135
     * Returns a new general-purpose <code>NumberFormat</code> instance for
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   136
     * the specified locale.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   137
     *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   138
     * @param locale the desired locale
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   139
     * @exception NullPointerException if <code>locale</code> is null
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   140
     * @exception IllegalArgumentException if <code>locale</code> isn't
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   141
     *     one of the locales returned from
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   142
     *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   143
     *     getAvailableLocales()}.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   144
     * @return a general-purpose number formatter
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   145
     * @see java.text.NumberFormat#getNumberInstance(java.util.Locale)
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   146
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   147
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   148
    public NumberFormat getNumberInstance(Locale locale) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   149
        return getInstance(locale, NUMBERSTYLE);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   150
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   151
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   152
    /**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   153
     * Returns a new <code>NumberFormat</code> instance which formats
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   154
     * percentage values for the specified locale.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   155
     *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   156
     * @param locale the desired locale
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   157
     * @exception NullPointerException if <code>locale</code> is null
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   158
     * @exception IllegalArgumentException if <code>locale</code> isn't
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   159
     *     one of the locales returned from
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   160
     *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   161
     *     getAvailableLocales()}.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   162
     * @return a percent formatter
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   163
     * @see java.text.NumberFormat#getPercentInstance(java.util.Locale)
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   164
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   165
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   166
    public NumberFormat getPercentInstance(Locale locale) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   167
        return getInstance(locale, PERCENTSTYLE);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   168
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   169
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   170
    private NumberFormat getInstance(Locale locale,
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   171
                                            int choice) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   172
        if (locale == null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   173
            throw new NullPointerException();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   174
        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   175
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   176
        LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   177
        String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   178
        DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   179
        int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   180
        DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   181
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   182
        if (choice == INTEGERSTYLE) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   183
            format.setMaximumFractionDigits(0);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   184
            format.setDecimalSeparatorAlwaysShown(false);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   185
            format.setParseIntegerOnly(true);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   186
        } else if (choice == CURRENCYSTYLE) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   187
            adjustForCurrencyDefaultFractionDigits(format, symbols);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   188
        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   189
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   190
        return format;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   191
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   192
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   193
    /**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   194
     * Adjusts the minimum and maximum fraction digits to values that
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   195
     * are reasonable for the currency's default fraction digits.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   196
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   197
    private static void adjustForCurrencyDefaultFractionDigits(
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   198
            DecimalFormat format, DecimalFormatSymbols symbols) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   199
        Currency currency = symbols.getCurrency();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   200
        if (currency == null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   201
            try {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   202
                currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   203
            } catch (IllegalArgumentException e) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   204
            }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   205
        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   206
        if (currency != null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   207
            int digits = currency.getDefaultFractionDigits();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   208
            if (digits != -1) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   209
                int oldMinDigits = format.getMinimumFractionDigits();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   210
                // Common patterns are "#.##", "#.00", "#".
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   211
                // Try to adjust all of them in a reasonable way.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   212
                if (oldMinDigits == format.getMaximumFractionDigits()) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   213
                    format.setMinimumFractionDigits(digits);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   214
                    format.setMaximumFractionDigits(digits);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   215
                } else {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   216
                    format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   217
                    format.setMaximumFractionDigits(digits);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   218
                }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   219
            }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   220
        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   221
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   222
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   223
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   224
    public Set<String> getAvailableLanguageTags() {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   225
        return langtags;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   226
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   227
}