KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > convert > support > TextToNumber


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.convert.support;
17
18 import java.math.BigDecimal JavaDoc;
19 import java.math.BigInteger JavaDoc;
20
21 import org.springframework.binding.convert.ConversionContext;
22 import org.springframework.binding.format.FormatterFactory;
23 import org.springframework.binding.format.support.SimpleFormatterFactory;
24
25 /**
26  * Converts textual representations of numbers to a <code>Number</code>
27  * specialization. Delegates to a synchronized formatter to parse text strings.
28  * @author Keith Donald
29  */

30 public class TextToNumber extends AbstractFormattingConverter {
31
32     public TextToNumber() {
33         super(new SimpleFormatterFactory());
34     }
35
36     public TextToNumber(FormatterFactory formatterFactory) {
37         super(formatterFactory);
38     }
39
40     public Class JavaDoc[] getSourceClasses() {
41         return new Class JavaDoc[] { String JavaDoc.class };
42     }
43
44     public Class JavaDoc[] getTargetClasses() {
45         return new Class JavaDoc[] { Integer JavaDoc.class, Short JavaDoc.class, Byte JavaDoc.class, Long JavaDoc.class, Float JavaDoc.class, Double JavaDoc.class,
46                 BigInteger JavaDoc.class, BigDecimal JavaDoc.class };
47     }
48
49     protected Object JavaDoc doConvert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws Exception JavaDoc {
50         return getFormatterFactory().getNumberFormatter(targetClass).parseValue((String JavaDoc)source, targetClass);
51     }
52 }
Popular Tags