1 /*
2 * Copyright (c) 1999, 2017, 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
23 * questions.
24 */
25
26 /*
27 *
28 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
29 * (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved
30 *
31 * The original version of this source code and documentation
32 * is copyrighted and owned by Taligent, Inc., a wholly-owned
33 * subsidiary of IBM. These materials are provided under terms
34 * of a License Agreement between Taligent and Sun. This technology
35 * is protected by multiple US and International patents.
36 *
37 * This notice and attribution to Taligent may not be removed.
38 * Taligent is a registered trademark of Taligent, Inc.
39 */
40
41 package sun.util.locale.provider;
42
43 import java.text.DecimalFormat;
44 import java.text.DecimalFormatSymbols;
45 import java.text.NumberFormat;
46 import java.text.spi.NumberFormatProvider;
47 import java.util.Currency;
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;
206 try {
207 currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
208 } catch (IllegalArgumentException e) {
209 }
210 }
211 if (currency != null) {
212 int digits = currency.getDefaultFractionDigits();
213 if (digits != -1) {
214 int oldMinDigits = format.getMinimumFractionDigits();
215 // Common patterns are "#.##", "#.00", "#".
216 // Try to adjust all of them in a reasonable way.
217 if (oldMinDigits == format.getMaximumFractionDigits()) {
218 format.setMinimumFractionDigits(digits);
219 format.setMaximumFractionDigits(digits);
220 } else {
221 format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
222 format.setMaximumFractionDigits(digits);
223 }
224 }
225 }
226 }
227
228 @Override
229 public Set<String> getAvailableLanguageTags() {
230 return langtags;
231 }
232 }
|
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
23 * questions.
24 */
25
26 /*
27 *
28 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
29 * (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved
30 *
31 * The original version of this source code and documentation
32 * is copyrighted and owned by Taligent, Inc., a wholly-owned
33 * subsidiary of IBM. These materials are provided under terms
34 * of a License Agreement between Taligent and Sun. This technology
35 * is protected by multiple US and International patents.
36 *
37 * This notice and attribution to Taligent may not be removed.
38 * Taligent is a registered trademark of Taligent, Inc.
39 */
40
41 package sun.util.locale.provider;
42
43 import java.text.CompactNumberFormat;
44 import java.text.DecimalFormat;
45 import java.text.DecimalFormatSymbols;
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;
208 try {
209 currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
210 } catch (IllegalArgumentException e) {
211 }
212 }
213 if (currency != null) {
214 int digits = currency.getDefaultFractionDigits();
215 if (digits != -1) {
216 int oldMinDigits = format.getMinimumFractionDigits();
217 // Common patterns are "#.##", "#.00", "#".
218 // Try to adjust all of them in a reasonable way.
219 if (oldMinDigits == format.getMaximumFractionDigits()) {
220 format.setMinimumFractionDigits(digits);
221 format.setMaximumFractionDigits(digits);
222 } else {
223 format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
224 format.setMaximumFractionDigits(digits);
225 }
226 }
227 }
228 }
229
230 /**
231 * Returns a new {@code NumberFormat} instance which formats
232 * a number in its compact form for the specified
233 * {@code locale} and {@code formatStyle}.
234 *
235 * @param locale the desired locale
236 * @param formatStyle the style for formatting a number
237 * @throws NullPointerException if {@code locale} or {@code formatStyle}
238 * is {@code null}
239 * @throws IllegalArgumentException if {@code locale} isn't
240 * one of the locales returned from
241 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
242 * getAvailableLocales()}.
243 * @return a compact number formatter
244 *
245 * @see java.text.NumberFormat#getCompactNumberInstance(Locale,
246 * NumberFormat.Style)
247 * @since 12
248 */
249 @Override
250 public NumberFormat getCompactNumberInstance(Locale locale,
251 NumberFormat.Style formatStyle) {
252
253 Objects.requireNonNull(locale);
254 Objects.requireNonNull(formatStyle);
255
256 // Check for region override
257 Locale override = locale.getUnicodeLocaleType("nu") == null
258 ? CalendarDataUtility.findRegionOverride(locale)
259 : locale;
260
261 LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
262 LocaleResources resource = adapter.getLocaleResources(override);
263
264 String[] numberPatterns = resource.getNumberPatterns();
265 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(override);
266 String[] cnPatterns = resource.getCNPatterns(formatStyle);
267
268 CompactNumberFormat format = new CompactNumberFormat(numberPatterns[0],
269 symbols, cnPatterns);
270 return format;
271 }
272
273 @Override
274 public Set<String> getAvailableLanguageTags() {
275 return langtags;
276 }
277 }
|