KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > convertor > FormattingFloatConvertor


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.datatype.convertor;
17
18 import org.outerj.i18n.I18nSupport;
19 import org.outerj.i18n.DecimalFormat;
20
21 import java.util.Locale JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.math.BigDecimal JavaDoc;
24 import java.math.BigInteger JavaDoc;
25
26 /**
27  * A Convertor for {@link Float}s backed by the
28  * {@link java.text.NumberFormat NumberFormat} class.
29  *
30  * <p>A <strong>formatting pattern</strong> can be used. This can either be a locale-dependent
31  * or locale-independent formatting pattern. When looking up a formatting pattern, a mechansim
32  * similar to resource bundle lookup is used. Suppose the locale is nl-BE, then first a formatting
33  * pattern for nl-BE will be sought, then one for nl, and if that is not
34  * found, finally the locale-independent formatting pattern will be used.
35  *
36  * <p>Note: the earlier statement about the fact that this class uses java.text.DecimalFormat
37  * is not entirely correct. In fact, it uses a small wrapper class that will either delegate to
38  * java.text.DecimalFormat or com.ibm.icu.text.DecimalFormat. The com.ibm version will automatically
39  * be used if it is present on the classpath, otherwise the java.text version will be used.
40  *
41  * @version CVS $Id: FormattingFloatConvertor.java 54079 2004-10-08 13:30:28Z vgritsenko $
42  */

43 public class FormattingFloatConvertor implements Convertor {
44     private int variant;
45     /** Locale-specific formatting patterns. */
46     private LocaleMap localizedPatterns;
47     /** Non-locale specific formatting pattern. */
48     private String JavaDoc nonLocalizedPattern;
49
50     public static final int INTEGER = 0;
51     public static final int NUMBER = 1;
52     public static final int CURRENCY = 2;
53     public static final int PERCENT = 3;
54
55     public FormattingFloatConvertor() {
56         this.variant = getDefaultVariant();
57         this.localizedPatterns = new LocaleMap();
58     }
59
60     protected int getDefaultVariant() {
61         return NUMBER;
62     }
63
64     public Object JavaDoc convertFromString(String JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
65         DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
66         Number JavaDoc decimalValue;
67         try {
68             decimalValue = decimalFormat.parse(value);
69         } catch (ParseException JavaDoc e) {
70             return null;
71         }
72
73         if (decimalValue instanceof BigDecimal JavaDoc) {
74             // no need for conversion
75
} else if (decimalValue instanceof Integer JavaDoc) {
76                 decimalValue = new BigDecimal JavaDoc(decimalValue .intValue());
77         } else if (decimalValue instanceof Long JavaDoc) {
78                 decimalValue = new BigDecimal JavaDoc(decimalValue.longValue());
79         } else if (decimalValue instanceof Double JavaDoc) {
80                 decimalValue = new BigDecimal JavaDoc(decimalValue.doubleValue());
81         } else if (decimalValue instanceof BigInteger JavaDoc) {
82                 decimalValue = new BigDecimal JavaDoc((BigInteger JavaDoc)decimalValue);
83         } else {
84                 return null;
85         }
86
87             return decimalValue;
88     }
89
90     public String JavaDoc convertToString(Object JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
91         DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
92         return decimalFormat.format(value);
93     }
94
95     protected final DecimalFormat getDecimalFormat(Locale JavaDoc locale, Convertor.FormatCache formatCache) {
96         DecimalFormat decimalFormat = null;
97         if (formatCache != null)
98             decimalFormat = (DecimalFormat)formatCache.get();
99         if (decimalFormat == null) {
100             decimalFormat = getDecimalFormat(locale);
101             if (formatCache != null)
102                 formatCache.store(decimalFormat);
103         }
104         return decimalFormat;
105     }
106
107     private DecimalFormat getDecimalFormat(Locale JavaDoc locale) {
108         DecimalFormat decimalFormat = null;
109
110         switch (variant) {
111             case INTEGER:
112                 decimalFormat = I18nSupport.getInstance().getIntegerFormat(locale);
113                 break;
114             case NUMBER:
115                 decimalFormat = I18nSupport.getInstance().getNumberFormat(locale);
116                 break;
117             case CURRENCY:
118                 decimalFormat = I18nSupport.getInstance().getCurrencyFormat(locale);
119                 break;
120             case PERCENT:
121                 decimalFormat = I18nSupport.getInstance().getPercentFormat(locale);
122                 break;
123         }
124
125         String JavaDoc pattern = (String JavaDoc)localizedPatterns.get(locale);
126
127         if (pattern != null)
128             decimalFormat.applyLocalizedPattern(pattern);
129         else if (nonLocalizedPattern != null)
130             decimalFormat.applyPattern(nonLocalizedPattern);
131
132         return decimalFormat;
133     }
134
135     public void setVariant(int variant) {
136         if (variant != INTEGER && variant != NUMBER && variant != CURRENCY && variant != PERCENT)
137             throw new IllegalArgumentException JavaDoc("Invalid value for variant parameter.");
138         this.variant = variant;
139     }
140
141     public void addFormattingPattern(Locale JavaDoc locale, String JavaDoc pattern) {
142         localizedPatterns.put(locale, pattern);
143     }
144
145     public void setNonLocalizedPattern(String JavaDoc pattern) {
146         this.nonLocalizedPattern = pattern;
147     }
148
149     public Class JavaDoc getTypeClass() {
150         return java.math.BigDecimal JavaDoc.class;
151     }
152 }
153
Popular Tags