KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > format > support > NumberFormatter


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.binding.format.support;
17
18 import java.math.BigInteger JavaDoc;
19 import java.text.NumberFormat JavaDoc;
20
21 import org.springframework.binding.format.InvalidFormatException;
22 import org.springframework.util.NumberUtils;
23
24 /**
25  * Converts from various
26  * <code>Number<code> specializations to <code>String</code> and back.
27  * @author Keith Donald
28  */

29 public class NumberFormatter extends AbstractFormatter {
30
31     private NumberFormat JavaDoc numberFormat;
32
33     public NumberFormatter(NumberFormat JavaDoc numberFormat) {
34         this.numberFormat = numberFormat;
35     }
36
37     public NumberFormatter(NumberFormat JavaDoc numberFormat, boolean allowEmpty) {
38         super(allowEmpty);
39         this.numberFormat = numberFormat;
40     }
41
42     protected String JavaDoc doFormatValue(Object JavaDoc number) {
43         if (this.numberFormat != null) {
44             // use NumberFormat for rendering value
45
return this.numberFormat.format(number);
46         }
47         else {
48             // use toString method for rendering value
49
return number.toString();
50         }
51     }
52
53     protected Object JavaDoc doParseValue(String JavaDoc text, Class JavaDoc targetClass) throws IllegalArgumentException JavaDoc {
54         // use given NumberFormat for parsing text
55
if (this.numberFormat != null) {
56             return NumberUtils.parseNumber(text, targetClass, this.numberFormat);
57         }
58         // use default valueOf methods for parsing text
59
else {
60             return NumberUtils.parseNumber(text, targetClass);
61         }
62     }
63
64     public Short JavaDoc parseShort(String JavaDoc formattedString) throws InvalidFormatException {
65         return (Short JavaDoc)parseValue(formattedString, Short JavaDoc.class);
66     }
67
68     public Integer JavaDoc parseInteger(String JavaDoc formattedString) throws InvalidFormatException {
69         return (Integer JavaDoc)parseValue(formattedString, Integer JavaDoc.class);
70     }
71
72     public Long JavaDoc parseLong(String JavaDoc formattedString) throws InvalidFormatException {
73         return (Long JavaDoc)parseValue(formattedString, Long JavaDoc.class);
74     }
75
76     public Double JavaDoc parseDouble(String JavaDoc formattedString) throws InvalidFormatException {
77         return (Double JavaDoc)parseValue(formattedString, Double JavaDoc.class);
78     }
79
80     public Float JavaDoc parseFloat(String JavaDoc formattedString) throws InvalidFormatException {
81         return (Float JavaDoc)parseValue(formattedString, Float JavaDoc.class);
82     }
83
84     public BigInteger JavaDoc parseBigInteger(String JavaDoc formattedString) throws InvalidFormatException {
85         return (BigInteger JavaDoc)parseValue(formattedString, BigInteger JavaDoc.class);
86     }
87
88     public Byte JavaDoc parseByte(String JavaDoc formattedString) throws InvalidFormatException {
89         return (Byte JavaDoc)parseValue(formattedString, Byte JavaDoc.class);
90     }
91 }
Popular Tags