01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
19: import java.math.BigInteger;
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: *
29: * @author Keith Donald
30: */
31: public class TextToNumber extends AbstractFormattingConverter {
32:
33: /**
34: * Default constructor that uses a {@link SimpleFormatterFactory}.
35: */
36: public TextToNumber() {
37: super (new SimpleFormatterFactory());
38: }
39:
40: /**
41: * Create a string to number converter using given formatter factory.
42: * @param formatterFactory the factory to use
43: */
44: public TextToNumber(FormatterFactory formatterFactory) {
45: super (formatterFactory);
46: }
47:
48: public Class[] getSourceClasses() {
49: return new Class[] { String.class };
50: }
51:
52: public Class[] getTargetClasses() {
53: return new Class[] { Integer.class, Short.class, Byte.class,
54: Long.class, Float.class, Double.class,
55: BigInteger.class, BigDecimal.class };
56: }
57:
58: protected Object doConvert(Object source, Class targetClass,
59: ConversionContext context) throws Exception {
60: return getFormatterFactory().getNumberFormatter(targetClass)
61: .parseValue((String) source, targetClass);
62: }
63: }
|