1 /*
2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
46 import java.text.NumberFormat;
47 import java.text.spi.NumberFormatProvider;
48 import java.util.Currency;
49 import java.util.Locale;
50 import java.util.Objects;
51 import java.util.Set;
52
53 /**
54 * Concrete implementation of the {@link java.text.spi.NumberFormatProvider
55 * NumberFormatProvider} class for the JRE LocaleProviderAdapter.
56 *
57 * @author Naoto Sato
58 * @author Masayoshi Okutsu
59 */
60 public class NumberFormatProviderImpl extends NumberFormatProvider implements AvailableLanguageTags {
61
62 // Constants used by factory methods to specify a style of format.
63 private static final int NUMBERSTYLE = 0;
64 private static final int CURRENCYSTYLE = 1;
65 private static final int PERCENTSTYLE = 2;
66 private static final int SCIENTIFICSTYLE = 3;
67 private static final int INTEGERSTYLE = 4;
68
69 private final LocaleProviderAdapter.Type type;
70 private final Set<String> langtags;
71
72 public NumberFormatProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
73 this.type = type;
74 this.langtags = langtags;
75 }
76
77 /**
78 * Returns an array of all locales for which this locale service provider
79 * can provide localized objects or names.
80 *
81 * @return An array of all locales for which this locale service provider
82 * can provide localized objects or names.
83 */
84 @Override
85 public Locale[] getAvailableLocales() {
86 return LocaleProviderAdapter.forType(type).getAvailableLocales();
167 @Override
168 public NumberFormat getPercentInstance(Locale locale) {
169 return getInstance(locale, PERCENTSTYLE);
170 }
171
172 private NumberFormat getInstance(Locale locale,
173 int choice) {
174 if (locale == null) {
175 throw new NullPointerException();
176 }
177
178 // Check for region override
179 Locale override = locale.getUnicodeLocaleType("nu") == null ?
180 CalendarDataUtility.findRegionOverride(locale) :
181 locale;
182
183 LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
184 String[] numberPatterns = adapter.getLocaleResources(override).getNumberPatterns();
185 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(override);
186 int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
187 DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
188
189 if (choice == INTEGERSTYLE) {
190 format.setMaximumFractionDigits(0);
191 format.setDecimalSeparatorAlwaysShown(false);
192 format.setParseIntegerOnly(true);
193 } else if (choice == CURRENCYSTYLE) {
194 adjustForCurrencyDefaultFractionDigits(format, symbols);
195 }
196
197 return format;
198 }
199
200 /**
201 * Adjusts the minimum and maximum fraction digits to values that
202 * are reasonable for the currency's default fraction digits.
203 */
204 private static void adjustForCurrencyDefaultFractionDigits(
205 DecimalFormat format, DecimalFormatSymbols symbols) {
206 Currency currency = symbols.getCurrency();
|
1 /*
2 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
46 import java.text.NumberFormat;
47 import java.text.spi.NumberFormatProvider;
48 import java.util.Currency;
49 import java.util.Locale;
50 import java.util.Objects;
51 import java.util.Set;
52
53 /**
54 * Concrete implementation of the {@link java.text.spi.NumberFormatProvider
55 * NumberFormatProvider} class for the JRE LocaleProviderAdapter.
56 *
57 * @author Naoto Sato
58 * @author Masayoshi Okutsu
59 */
60 public class NumberFormatProviderImpl extends NumberFormatProvider implements AvailableLanguageTags {
61
62 // Constants used by factory methods to specify a style of format.
63 private static final int NUMBERSTYLE = 0;
64 private static final int CURRENCYSTYLE = 1;
65 private static final int PERCENTSTYLE = 2;
66 private static final int ACCOUNTINGSTYLE = 3;
67 private static final int INTEGERSTYLE = 4;
68
69 private final LocaleProviderAdapter.Type type;
70 private final Set<String> langtags;
71
72 public NumberFormatProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
73 this.type = type;
74 this.langtags = langtags;
75 }
76
77 /**
78 * Returns an array of all locales for which this locale service provider
79 * can provide localized objects or names.
80 *
81 * @return An array of all locales for which this locale service provider
82 * can provide localized objects or names.
83 */
84 @Override
85 public Locale[] getAvailableLocales() {
86 return LocaleProviderAdapter.forType(type).getAvailableLocales();
167 @Override
168 public NumberFormat getPercentInstance(Locale locale) {
169 return getInstance(locale, PERCENTSTYLE);
170 }
171
172 private NumberFormat getInstance(Locale locale,
173 int choice) {
174 if (locale == null) {
175 throw new NullPointerException();
176 }
177
178 // Check for region override
179 Locale override = locale.getUnicodeLocaleType("nu") == null ?
180 CalendarDataUtility.findRegionOverride(locale) :
181 locale;
182
183 LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
184 String[] numberPatterns = adapter.getLocaleResources(override).getNumberPatterns();
185 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(override);
186 int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
187 if (choice == CURRENCYSTYLE &&
188 numberPatterns.length > ACCOUNTINGSTYLE &&
189 !numberPatterns[ACCOUNTINGSTYLE].isEmpty() &&
190 "account".equalsIgnoreCase(override.getUnicodeLocaleType("cf"))) {
191 entry = ACCOUNTINGSTYLE;
192 }
193 DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
194
195 if (choice == INTEGERSTYLE) {
196 format.setMaximumFractionDigits(0);
197 format.setDecimalSeparatorAlwaysShown(false);
198 format.setParseIntegerOnly(true);
199 } else if (choice == CURRENCYSTYLE) {
200 adjustForCurrencyDefaultFractionDigits(format, symbols);
201 }
202
203 return format;
204 }
205
206 /**
207 * Adjusts the minimum and maximum fraction digits to values that
208 * are reasonable for the currency's default fraction digits.
209 */
210 private static void adjustForCurrencyDefaultFractionDigits(
211 DecimalFormat format, DecimalFormatSymbols symbols) {
212 Currency currency = symbols.getCurrency();
|