KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > l10n > DefaultNumberFormatProvider


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on Dec 23, 2004
33  */

34 package com.nightlabs.l10n;
35
36 import java.text.DecimalFormat JavaDoc;
37 import java.text.DecimalFormatSymbols JavaDoc;
38 import java.text.NumberFormat JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Locale JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import com.nightlabs.config.Config;
44
45 /**
46  * @author Marco Schulze - marco at nightlabs dot de
47  */

48 public class DefaultNumberFormatProvider implements NumberFormatProvider
49 {
50
51     protected Config config;
52     protected String JavaDoc isoLanguage;
53     protected String JavaDoc isoCountry;
54     protected DefaultNumberFormatCfMod defaultNumberFormatCfMod;
55     
56     public DefaultNumberFormatProvider()
57     {
58     }
59
60     /**
61      * @see com.nightlabs.l10n.NumberFormatProvider#init(Config, String, String)
62      */

63     public void init(Config config, String JavaDoc isoLanguage, String JavaDoc isoCountry)
64     {
65         try {
66             this.config = config;
67             this.isoLanguage = isoLanguage;
68             this.isoCountry = isoCountry;
69             this.defaultNumberFormatCfMod = (DefaultNumberFormatCfMod) ConfigUtil.createConfigModule(config, DefaultNumberFormatCfMod.class, isoLanguage, isoCountry);
70         } catch (RuntimeException JavaDoc x) {
71             throw x;
72         } catch (Exception JavaDoc x) {
73             throw new RuntimeException JavaDoc(x);
74         }
75     }
76
77     /**
78      * key: String key (Long.toHexString(flags)+'_'+Integer.toHexString(decimalDigitCount))
79      */

80     protected Map JavaDoc numberFormats = new HashMap JavaDoc();
81
82     protected DecimalFormatSymbols JavaDoc createDecimalFormatSymbols()
83     {
84         DecimalFormatSymbols JavaDoc dfs = new DecimalFormatSymbols JavaDoc(new Locale JavaDoc(isoLanguage, isoCountry));
85         dfs.setGroupingSeparator(defaultNumberFormatCfMod.getGroupingSeparator());
86         dfs.setDecimalSeparator(defaultNumberFormatCfMod.getDecimalSeparator());
87         return dfs;
88     }
89     
90     protected String JavaDoc createIntegerDigitCountPattern(int integerDigitCount)
91     {
92         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
93         int groupingSize = defaultNumberFormatCfMod.getGroupingSize();
94         for (int i = 1; i <= integerDigitCount || i <= groupingSize; ++i) {
95             sb.insert(0, i <= integerDigitCount ? '0' : '#');
96             if (i % groupingSize == 0)
97                 sb.insert(0, ',');
98         }
99         return sb.toString();
100     }
101     
102     protected String JavaDoc createDecimalDigitCountPattern(int minDecimalDigitCount, int maxDecimalDigitCount)
103     {
104         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
105         for (int i = 1; i <= minDecimalDigitCount || i <= maxDecimalDigitCount; ++i) {
106             sb.append(i <= minDecimalDigitCount ? '0' : '#');
107         }
108         return sb.toString();
109     }
110
111     /**
112      * @see com.nightlabs.l10n.NumberFormatProvider#getIntegerFormat(int)
113      */

114     public NumberFormat JavaDoc getIntegerFormat(int minIntegerDigitCount)
115     {
116         String JavaDoc key = "integer_"+Integer.toHexString(minIntegerDigitCount);
117         DecimalFormat JavaDoc df;
118         synchronized (numberFormats) {
119             df = (DecimalFormat JavaDoc) numberFormats.get(key);
120         }
121         if (df == null) {
122             DecimalFormatSymbols JavaDoc dfs = createDecimalFormatSymbols();
123             String JavaDoc integerDigitCountPattern = createIntegerDigitCountPattern(minIntegerDigitCount);
124
125             df = new DecimalFormat JavaDoc(
126                     defaultNumberFormatCfMod.getPositivePrefix()
127                     +'#'+integerDigitCountPattern
128                     +defaultNumberFormatCfMod.getPositiveSuffix()
129                     +';'
130                     +defaultNumberFormatCfMod.getNegativePrefix()
131                     +'#'+integerDigitCountPattern
132                     +defaultNumberFormatCfMod.getNegativeSuffix(),
133                     dfs);
134
135             synchronized (numberFormats) {
136                 numberFormats.put(key, df);
137             }
138         }
139         
140         return df;
141     }
142
143     /**
144      * @see com.nightlabs.l10n.NumberFormatProvider#getFloatFormat(int, int, int)
145      */

146     public NumberFormat JavaDoc getFloatFormat(int minIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount)
147     {
148         String JavaDoc key = "float_"+Integer.toHexString(minIntegerDigitCount)
149                 +'_'+Integer.toHexString(minDecimalDigitCount)
150                 +'_'+Integer.toHexString(maxDecimalDigitCount);
151         DecimalFormat JavaDoc df;
152         synchronized (numberFormats) {
153             df = (DecimalFormat JavaDoc) numberFormats.get(key);
154         }
155         if (df == null) {
156             DecimalFormatSymbols JavaDoc dfs = createDecimalFormatSymbols();
157             String JavaDoc integerDigitCountPattern = createIntegerDigitCountPattern(minIntegerDigitCount);
158             String JavaDoc decimalDigitCountPattern = createDecimalDigitCountPattern(minDecimalDigitCount, maxDecimalDigitCount);
159
160             df = new DecimalFormat JavaDoc(
161                     defaultNumberFormatCfMod.getPositivePrefix()
162                     +'#'+integerDigitCountPattern
163                     +'.'+decimalDigitCountPattern
164                     +defaultNumberFormatCfMod.getPositiveSuffix()
165                     +';'
166                     +defaultNumberFormatCfMod.getNegativePrefix()
167                     +'#'+integerDigitCountPattern
168                     +'.'+decimalDigitCountPattern
169                     +defaultNumberFormatCfMod.getNegativeSuffix(),
170                     dfs);
171             
172             synchronized (numberFormats) {
173                 numberFormats.put(key, df);
174             }
175         }
176
177         return df;
178     }
179
180     /**
181      * @see com.nightlabs.l10n.NumberFormatProvider#getCurrencyFormat(int, int, int, String, boolean)
182      */

183     public NumberFormat JavaDoc getCurrencyFormat(int minIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount, String JavaDoc currencySymbol, boolean includeCurrencySymbol)
184     {
185         String JavaDoc key = "currency_"+Integer.toHexString(minIntegerDigitCount)
186                 +'_'+Integer.toHexString(minDecimalDigitCount)
187                 +'_'+Integer.toHexString(maxDecimalDigitCount);
188         DecimalFormat JavaDoc df;
189         synchronized (numberFormats) {
190             df = (DecimalFormat JavaDoc) numberFormats.get(key);
191         }
192         if (df == null) {
193             DecimalFormatSymbols JavaDoc dfs = createDecimalFormatSymbols();
194 // dfs.setInternationalCurrencySymbol(null);
195
dfs.setCurrencySymbol(currencySymbol);
196             String JavaDoc integerDigitCountPattern = createIntegerDigitCountPattern(minIntegerDigitCount);
197             String JavaDoc decimalDigitCountPattern = createDecimalDigitCountPattern(minDecimalDigitCount, maxDecimalDigitCount);
198
199             String JavaDoc beginCurrSymbol;
200             String JavaDoc endCurrSymbol;
201             if (!includeCurrencySymbol) {
202                 beginCurrSymbol = "";
203                 endCurrSymbol = "";
204             }
205             else if (DefaultNumberFormatCfMod.CURRENCYSYMBOLPOSITION_BEGIN.equals(defaultNumberFormatCfMod.getCurrencySymbolPosition())) {
206                 beginCurrSymbol = "\u00A4 ";
207                 endCurrSymbol = "";
208             }
209             else {
210                 beginCurrSymbol = "";
211                 endCurrSymbol = " \u00A4";
212             }
213
214             df = new DecimalFormat JavaDoc(
215                     beginCurrSymbol
216                     +defaultNumberFormatCfMod.getPositivePrefix()
217                     +'#'+integerDigitCountPattern
218                     +'.'+decimalDigitCountPattern
219                     +defaultNumberFormatCfMod.getPositiveSuffix()
220                     +endCurrSymbol
221                     +';'
222                     +beginCurrSymbol
223                     +defaultNumberFormatCfMod.getNegativePrefix()
224                     +'#'+integerDigitCountPattern
225                     +'.'+decimalDigitCountPattern
226                     +defaultNumberFormatCfMod.getNegativeSuffix()
227                     +endCurrSymbol,
228                     dfs);
229
230             synchronized (numberFormats) {
231                 numberFormats.put(key, df);
232             }
233         }
234
235         return df;
236     }
237
238     /**
239      * @see com.nightlabs.l10n.NumberFormatProvider#getScientificFormat(int, int, int, int)
240      */

241     public NumberFormat JavaDoc getScientificFormat(int preferredIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount, int exponentDigitCount)
242     {
243         String JavaDoc key = "scientific_"+Integer.toHexString(preferredIntegerDigitCount)
244                 +'_'+Integer.toHexString(minDecimalDigitCount)
245                 +'_'+Integer.toHexString(maxDecimalDigitCount)
246                 +'_'+Integer.toHexString(exponentDigitCount);
247         DecimalFormat JavaDoc df;
248         synchronized (numberFormats) {
249             df = (DecimalFormat JavaDoc) numberFormats.get(key);
250         }
251         if (df == null) {
252             DecimalFormatSymbols JavaDoc dfs = createDecimalFormatSymbols();
253             
254             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
255             for (int i = 1; i <= preferredIntegerDigitCount; ++i) {
256                 sb.append('0');
257             }
258             String JavaDoc integerDigitCountPattern = sb.toString();
259             String JavaDoc decimalDigitCountPattern = createDecimalDigitCountPattern(minDecimalDigitCount, maxDecimalDigitCount);
260             sb.setLength(0);
261             for (int i = 1; i <= exponentDigitCount; ++i) {
262                 sb.append('0');
263             }
264             String JavaDoc exponentDigitCountPattern = sb.toString();
265
266             df = new DecimalFormat JavaDoc(
267                     defaultNumberFormatCfMod.getPositivePrefix()
268                     +'#'+integerDigitCountPattern
269                     +'.'+decimalDigitCountPattern
270                     +'E'+exponentDigitCountPattern
271                     +defaultNumberFormatCfMod.getPositiveSuffix()
272                     +';'
273                     +defaultNumberFormatCfMod.getNegativePrefix()
274                     +'#'+integerDigitCountPattern
275                     +'.'+decimalDigitCountPattern
276                     +'E'+exponentDigitCountPattern
277                     +defaultNumberFormatCfMod.getNegativeSuffix(),
278                     dfs);
279
280             synchronized (numberFormats) {
281                 numberFormats.put(key, df);
282             }
283         }
284
285         return null;
286     }
287
288 }
289
Popular Tags