1 /*
2 * Copyright (c) 1999, 2012, 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
48 import java.util.Locale;
49 import java.util.Set;
50
51 /**
52 * Concrete implementation of the {@link java.text.spi.NumberFormatProvider
53 * NumberFormatProvider} class for the JRE LocaleProviderAdapter.
54 *
55 * @author Naoto Sato
56 * @author Masayoshi Okutsu
57 */
58 public class NumberFormatProviderImpl extends NumberFormatProvider implements AvailableLanguageTags {
59
60 // Constants used by factory methods to specify a style of format.
61 private static final int NUMBERSTYLE = 0;
62 private static final int CURRENCYSTYLE = 1;
63 private static final int PERCENTSTYLE = 2;
64 private static final int SCIENTIFICSTYLE = 3;
65 private static final int INTEGERSTYLE = 4;
66
67 private final LocaleProviderAdapter.Type type;
68 private final Set<String> langtags;
69
70 public NumberFormatProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
71 this.type = type;
72 this.langtags = langtags;
73 }
74
75 /**
76 * Returns an array of all locales for which this locale service provider
77 * can provide localized objects or names.
78 *
79 * @return An array of all locales for which this locale service provider
80 * can provide localized objects or names.
81 */
82 @Override
83 public Locale[] getAvailableLocales() {
84 return LocaleProviderAdapter.forType(type).getAvailableLocales();
85 }
86
87 @Override
88 public boolean isSupportedLocale(Locale locale) {
89 return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
90 }
91
92 /**
93 * Returns a new <code>NumberFormat</code> instance which formats
94 * monetary values for the specified locale.
95 *
96 * @param locale the desired locale.
97 * @exception NullPointerException if <code>locale</code> is null
98 * @exception IllegalArgumentException if <code>locale</code> isn't
99 * one of the locales returned from
100 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
101 * getAvailableLocales()}.
102 * @return a currency formatter
103 * @see java.text.NumberFormat#getCurrencyInstance(java.util.Locale)
104 */
105 @Override
106 public NumberFormat getCurrencyInstance(Locale locale) {
107 return getInstance(locale, CURRENCYSTYLE);
108 }
109
205 }
206 if (currency != null) {
207 int digits = currency.getDefaultFractionDigits();
208 if (digits != -1) {
209 int oldMinDigits = format.getMinimumFractionDigits();
210 // Common patterns are "#.##", "#.00", "#".
211 // Try to adjust all of them in a reasonable way.
212 if (oldMinDigits == format.getMaximumFractionDigits()) {
213 format.setMinimumFractionDigits(digits);
214 format.setMaximumFractionDigits(digits);
215 } else {
216 format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
217 format.setMaximumFractionDigits(digits);
218 }
219 }
220 }
221 }
222
223 @Override
224 public Set<String> getAvailableLanguageTags() {
225 return langtags;
226 }
227 }
|
1 /*
2 * Copyright (c) 1999, 2014, 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
48 import java.util.Locale;
49 import java.util.Set;
50
51 /**
52 * Concrete implementation of the {@link java.text.spi.NumberFormatProvider
53 * NumberFormatProvider} class for the JRE LocaleProviderAdapter.
54 *
55 * @author Naoto Sato
56 * @author Masayoshi Okutsu
57 */
58 public class NumberFormatProviderImpl extends NumberFormatProvider implements AvailableLanguageTags {
59
60 // Constants used by factory methods to specify a style of format.
61 private static final int NUMBERSTYLE = 0;
62 private static final int CURRENCYSTYLE = 1;
63 private static final int PERCENTSTYLE = 2;
64 private static final int SCIENTIFICSTYLE = 3;
65 private static final int INTEGERSTYLE = 4;
66
67 private final LocaleProviderAdapter.Type type;
68
69 public NumberFormatProviderImpl(LocaleProviderAdapter.Type type) {
70 this.type = type;
71 }
72
73 /**
74 * Returns an array of all locales for which this locale service provider
75 * can provide localized objects or names.
76 *
77 * @return An array of all locales for which this locale service provider
78 * can provide localized objects or names.
79 */
80 @Override
81 public Locale[] getAvailableLocales() {
82 return LocaleProviderAdapter.forType(type).getAvailableLocales();
83 }
84
85 @Override
86 public boolean isSupportedLocale(Locale locale) {
87 return LocaleProviderAdapter.isSupportedLocale(locale, type, DateFormatProviderImpl.getLangTags(type));
88 }
89
90 /**
91 * Returns a new <code>NumberFormat</code> instance which formats
92 * monetary values for the specified locale.
93 *
94 * @param locale the desired locale.
95 * @exception NullPointerException if <code>locale</code> is null
96 * @exception IllegalArgumentException if <code>locale</code> isn't
97 * one of the locales returned from
98 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
99 * getAvailableLocales()}.
100 * @return a currency formatter
101 * @see java.text.NumberFormat#getCurrencyInstance(java.util.Locale)
102 */
103 @Override
104 public NumberFormat getCurrencyInstance(Locale locale) {
105 return getInstance(locale, CURRENCYSTYLE);
106 }
107
203 }
204 if (currency != null) {
205 int digits = currency.getDefaultFractionDigits();
206 if (digits != -1) {
207 int oldMinDigits = format.getMinimumFractionDigits();
208 // Common patterns are "#.##", "#.00", "#".
209 // Try to adjust all of them in a reasonable way.
210 if (oldMinDigits == format.getMaximumFractionDigits()) {
211 format.setMinimumFractionDigits(digits);
212 format.setMaximumFractionDigits(digits);
213 } else {
214 format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
215 format.setMaximumFractionDigits(digits);
216 }
217 }
218 }
219 }
220
221 @Override
222 public Set<String> getAvailableLanguageTags() {
223 return DateFormatProviderImpl.getLangTags(type);
224 }
225 }
|