KickJava   Java API By Example, From Geeks To Geeks.

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


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.NumberFormat JavaDoc;
37 import java.text.ParseException JavaDoc;
38 import java.util.Locale JavaDoc;
39
40 import com.nightlabs.config.Config;
41 import com.nightlabs.config.ConfigException;
42
43
44 /**
45  * @author Marco Schulze - marco at nightlabs dot de
46  */

47 public class NumberFormatter
48 {
49     private static NumberFormatter _sharedInstance = null;
50
51     /**
52      * In case there is no shared instance existing yet, it will be
53      * created with the shared instance of Config. Hence, this method
54      * fails if there is no shared instance of Config.
55      *
56      * @return Returns the shared instance of DateFormatter.
57      */

58     public static NumberFormatter sharedInstance()
59     {
60         if (_sharedInstance == null)
61             _sharedInstance = new NumberFormatter(Config.sharedInstance(), Locale.getDefault());
62
63         return _sharedInstance;
64     }
65
66     protected Config config;
67     protected Locale JavaDoc locale;
68
69     /**
70      * @param config
71      */

72     public NumberFormatter(Config config, Locale JavaDoc locale)
73     {
74         this.config = config;
75         this.locale = locale;
76         try {
77             config.createConfigModule(GlobalL10nSettings.class);
78         } catch (ConfigException e) {
79             throw new RuntimeException JavaDoc(e);
80         }
81     }
82
83     private NumberFormatProvider numberFormatProvider = null;
84
85     public NumberFormatProvider getNumberFormatProvider()
86     {
87         if (numberFormatProvider != null)
88             return numberFormatProvider;
89         
90         L10nFormatCfMod l10nFormatCfMod;
91         try {
92             l10nFormatCfMod = (L10nFormatCfMod) ConfigUtil.createConfigModule(
93                     config, L10nFormatCfMod.class, locale.getLanguage(), locale.getCountry());
94
95             String JavaDoc className = l10nFormatCfMod.getNumberFormatProvider();
96
97             Class JavaDoc clazz = Class.forName(className);
98             if (!NumberFormatProvider.class.isAssignableFrom(clazz))
99                 throw new ClassCastException JavaDoc("class \""+className+"\" defined in config module \""+L10nFormatCfMod.class.getName()+"\" does not implement interface \""+NumberFormatProvider.class.getName()+"\"!");
100
101             NumberFormatProvider nfp = (NumberFormatProvider) clazz.newInstance();
102             nfp.init(config, locale.getLanguage(), locale.getCountry());
103
104             this.numberFormatProvider = nfp;
105             return numberFormatProvider;
106         } catch (RuntimeException JavaDoc e) {
107             throw e;
108         } catch (Exception JavaDoc e) {
109             throw new RuntimeException JavaDoc(e);
110         }
111     }
112
113     /**
114      * @param l The value.
115      */

116     public static String JavaDoc formatInt(long l)
117     {
118         return formatInt(l, 1);
119     }
120
121     /**
122      * @param l The value.
123      * @param minIntegerDigitCount The minimal number of digits.
124      */

125     public static String JavaDoc formatInt(long l, int minIntegerDigitCount)
126     {
127         NumberFormat JavaDoc numberFormat = sharedInstance().getNumberFormatProvider().getIntegerFormat(
128                         minIntegerDigitCount);
129         return numberFormat.format(l);
130     }
131
132     /**
133      * @param d The value.
134      * @param decimalDigitCount The number of digits on the right side of the decimal separator.
135      */

136     public static String JavaDoc formatFloat(double d, int decimalDigitCount)
137     {
138         return formatFloat(d, 1, decimalDigitCount, decimalDigitCount);
139     }
140
141     /**
142      * @param d The value.
143      * @param minIntegerDigitCount The minimal number of digits on the left side of the decimal separator.
144      * @param minDecimalDigitCount The minimal number of digits on the right side of the decimal separator.
145      * @param maxDecimalDigitCount The maximal number of digits on the right side of the decimal separator.
146      */

147     public static String JavaDoc formatFloat(double d, int minIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount)
148     {
149         NumberFormat JavaDoc numberFormat = sharedInstance().getNumberFormatProvider().getFloatFormat(
150                         minIntegerDigitCount, minDecimalDigitCount, maxDecimalDigitCount);
151         return numberFormat.format(d);
152     }
153
154     /**
155      * @param d
156      * @param minIntegerDigitCount
157      * @param minDecimalDigitCount
158      * @param maxDecimalDigitCount
159      * @param exponentDigitCount
160      */

161     public static String JavaDoc formatScientific(double d, int minIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount, int exponentDigitCount)
162     {
163         NumberFormat JavaDoc numberFormat = sharedInstance().getNumberFormatProvider().getScientificFormat(
164                         minIntegerDigitCount, minDecimalDigitCount, maxDecimalDigitCount, exponentDigitCount);
165         return numberFormat.format(d);
166     }
167
168     private static long power(long base, int exponent) { // exponent >= 0
169
if (exponent < 0)
170             throw new IllegalArgumentException JavaDoc("exponent < 0!");
171         
172         long result = 1;
173         while (exponent != 0) {
174             if (exponent % 2 != 0)
175                 result = result * base;
176             exponent = exponent / 2;
177             base = base * base;
178         }
179         return result;
180     }
181
182     /**
183      * @param currencyValue The value.
184      * @param currency The currency.
185      */

186     public static String JavaDoc formatCurrency(
187             long currencyValue, Currency currency)
188     {
189         return formatCurrency(currencyValue, currency, true);
190     }
191
192     /**
193      * @param currencyValue The value.
194      * @param currency The currency.
195      * @param includeCurrencySymbol Whether or not to include the currency symbol in the result string.
196      */

197     public static String JavaDoc formatCurrency(
198             long currencyValue, Currency currency, boolean includeCurrencySymbol)
199     {
200         int ddc = currency.getDecimalDigitCount();
201         return formatCurrency(currencyValue, 1, ddc, ddc, currency,
202                 includeCurrencySymbol);
203     }
204
205     /**
206      * @param currencyValue The value.
207      * @param minIntegerDigitCount The minimal number of digits on the left side of the decimal separator.
208      * @param currency The currency.
209      */

210     public static String JavaDoc formatCurrency(
211             long currencyValue, int minIntegerDigitCount,
212             Currency currency)
213     {
214         return formatCurrency(
215                 currencyValue, minIntegerDigitCount,
216                 currency);
217     }
218
219     /**
220      * @param currencyValue The value.
221      * @param minIntegerDigitCount The minimal number of digits on the left side of the decimal separator.
222      * @param currency The currency.
223      * @param includeCurrencySymbol Whether or not to include the currency symbol in the result string.
224      */

225     public static String JavaDoc formatCurrency(
226             long currencyValue, int minIntegerDigitCount,
227             Currency currency, boolean includeCurrencySymbol)
228     {
229         int ddc = currency.getDecimalDigitCount();
230         return formatCurrency(
231                 currencyValue,
232                 minIntegerDigitCount, ddc, ddc, currency,
233                 includeCurrencySymbol);
234     }
235
236     /**
237      * @param currencyValue The value.
238      * @param minIntegerDigitCount The minimal number of digits on the left side of the decimal separator.
239      * @param minDecimalDigitCount The minimal number of digits on the right side of the decimal separator.
240      * @param maxDecimalDigitCount The maximal number of digits on the right side of the decimal separator.
241      * @param currency The currency.
242      */

243     public static String JavaDoc formatCurrency(
244             long currencyValue, int minIntegerDigitCount,
245             int minDecimalDigitCount, int maxDecimalDigitCount,
246             Currency currency)
247     {
248         return formatCurrency(
249                 currencyValue, minIntegerDigitCount,
250                 minDecimalDigitCount, maxDecimalDigitCount,
251                 currency);
252     }
253
254     /**
255      * @param currencyValue The value.
256      * @param minIntegerDigitCount The minimal number of digits on the left side of the decimal separator.
257      * @param minDecimalDigitCount The minimal number of digits on the right side of the decimal separator.
258      * @param maxDecimalDigitCount The maximal number of digits on the right side of the decimal separator.
259      * @param currency The currency.
260      * @param includeCurrencySymbol Whether or not to include the currency symbol in the result string.
261      */

262     public static String JavaDoc formatCurrency(
263             long currencyValue, int minIntegerDigitCount,
264             int minDecimalDigitCount, int maxDecimalDigitCount,
265             Currency currency, boolean includeCurrencySymbol)
266     {
267         NumberFormat JavaDoc numberFormat = sharedInstance().getNumberFormatProvider().getCurrencyFormat(
268                         minIntegerDigitCount, minDecimalDigitCount, maxDecimalDigitCount,
269                         currency.getCurrencySymbol(), includeCurrencySymbol);
270
271         double cvd = (double)currencyValue / power(10, currency.getDecimalDigitCount());
272         return numberFormat.format(cvd);
273     }
274
275     public static long parseCurrency(String JavaDoc valueStr, Currency currency, boolean includeCurrencySymbol)
276         throws ParseException JavaDoc
277     {
278         NumberFormat JavaDoc numberFormat = sharedInstance().getNumberFormatProvider().getCurrencyFormat(
279                 1, currency.getDecimalDigitCount(), currency.getDecimalDigitCount(),
280                 currency.getCurrencySymbol(), includeCurrencySymbol);
281         double d = numberFormat.parse(valueStr).doubleValue();
282         return Math.round(d * power(10, currency.getDecimalDigitCount()));
283     }
284 }
285
Popular Tags