blob: 98187487b5ca2ab7945922b432f6c667ca1e9b6a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright (c) 2007 Sun Microsystems, Inc. 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23/*
24 *
25 */
26
27package com.foo;
28
29import java.text.*;
30import java.text.spi.*;
31import java.util.*;
32
33import com.foobar.Utils;
34
35public class NumberFormatProviderImpl extends NumberFormatProvider {
36
37 static Locale[] avail = {
38 Locale.JAPAN,
39 new Locale("ja", "JP", "osaka"),
40 new Locale("ja", "JP", "kyoto"),
41 new Locale("zz")};
42
43 static String[] dialect = {
44 "\u3067\u3059\u3002",
45 "\u3084\u3002",
46 "\u3069\u3059\u3002",
47 "-zz"
48 };
49
50 static String[] patterns = {
51 "#,##0.###{0};-#,##0.###{1}", // decimal pattern
52 "\u00A4#,##0{0};-\u00A4#,##0{1}", // currency pattern
53 "#,##0%{0}" // percent pattern
54 };
55 // Constants used by factory methods to specify a style of format.
56 static final int NUMBERSTYLE = 0;
57 static final int CURRENCYSTYLE = 1;
58 static final int PERCENTSTYLE = 2;
59
60 public Locale[] getAvailableLocales() {
61 return avail;
62 }
63
64 public NumberFormat getCurrencyInstance(Locale locale) {
65 for (int i = 0; i < avail.length; i ++) {
66 if (Utils.supportsLocale(avail[i], locale)) {
67 String pattern =
68 MessageFormat.format(patterns[CURRENCYSTYLE],
69 dialect[i],
70 dialect[i]);
71 DecimalFormat df = new DecimalFormat(pattern,
72 DecimalFormatSymbols.getInstance(locale));
73 adjustForCurrencyDefaultFractionDigits(df);
74 return df;
75 }
76 }
77 throw new IllegalArgumentException("locale is not supported: "+locale);
78 }
79
80 public NumberFormat getIntegerInstance(Locale locale) {
81 for (int i = 0; i < avail.length; i ++) {
82 if (Utils.supportsLocale(avail[i], locale)) {
83 String pattern =
84 MessageFormat.format(patterns[NUMBERSTYLE],
85 dialect[i],
86 dialect[i]);
87 DecimalFormat df = new DecimalFormat(pattern,
88 DecimalFormatSymbols.getInstance(locale));
89 df.setMaximumFractionDigits(0);
90 df.setDecimalSeparatorAlwaysShown(false);
91 df.setParseIntegerOnly(true);
92 return df;
93 }
94 }
95 throw new IllegalArgumentException("locale is not supported: "+locale);
96 }
97
98 public NumberFormat getNumberInstance(Locale locale) {
99 for (int i = 0; i < avail.length; i ++) {
100 if (Utils.supportsLocale(avail[i], locale)) {
101 String pattern =
102 MessageFormat.format(patterns[NUMBERSTYLE],
103 dialect[i],
104 dialect[i]);
105 return new DecimalFormat(pattern,
106 DecimalFormatSymbols.getInstance(locale));
107 }
108 }
109 throw new IllegalArgumentException("locale is not supported: "+locale);
110 }
111
112 public NumberFormat getPercentInstance(Locale locale) {
113 for (int i = 0; i < avail.length; i ++) {
114 if (Utils.supportsLocale(avail[i], locale)) {
115 String pattern =
116 MessageFormat.format(patterns[PERCENTSTYLE],
117 dialect[i]);
118 return new DecimalFormat(pattern,
119 DecimalFormatSymbols.getInstance(locale));
120 }
121 }
122 throw new IllegalArgumentException("locale is not supported: "+locale);
123 }
124
125 /**
126 * Adjusts the minimum and maximum fraction digits to values that
127 * are reasonable for the currency's default fraction digits.
128 */
129 void adjustForCurrencyDefaultFractionDigits(DecimalFormat df) {
130 DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
131 Currency currency = dfs.getCurrency();
132 if (currency == null) {
133 try {
134 currency = Currency.getInstance(dfs.getInternationalCurrencySymbol());
135 } catch (IllegalArgumentException e) {
136 }
137 }
138 if (currency != null) {
139 int digits = currency.getDefaultFractionDigits();
140 if (digits != -1) {
141 int oldMinDigits = df.getMinimumFractionDigits();
142 // Common patterns are "#.##", "#.00", "#".
143 // Try to adjust all of them in a reasonable way.
144 if (oldMinDigits == df.getMaximumFractionDigits()) {
145 df.setMinimumFractionDigits(digits);
146 df.setMaximumFractionDigits(digits);
147 } else {
148 df.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
149 df.setMaximumFractionDigits(digits);
150 }
151 }
152 }
153 }
154}