View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.converter.impl;
17  
18  import java.math.BigDecimal;
19  
20  import org.seasar.cubby.converter.ConversionHelper;
21  
22  /**
23   * {@link Number 数}への変換を行うコンバータの抽象クラスです。
24   * <p>
25   * 変換元のオブジェクトの文字列表現を値とする{@link BigDecimal}からサブクラスが変換した結果を変換先とします。
26   * </p>
27   * 
28   * @author baba
29   * @since 1.1.0
30   */
31  public abstract class AbstractNumberConverter extends AbstractConverter {
32  
33  	/**
34  	 * {@inheritDoc}
35  	 */
36  	public Object convertToObject(final Object value, final Class<?> objectType, ConversionHelper helper) {
37  		if (value == null) {
38  			return null;
39  		}
40  		return convert(value.toString());
41  	}
42  
43  	/**
44  	 * 数を表す文字列から数値に変換して返します。
45  	 * 
46  	 * @param number
47  	 *            数を表す文字列
48  	 * @return 変換結果の数値
49  	 */
50  	protected Number convert(final String number) {
51  		if (number == null || number.length() == 0) {
52  			return null;
53  		}
54  		final BigDecimal decimal = new BigDecimal(number);
55  		return convert(decimal);
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	public String convertToString(final Object value, ConversionHelper helper) {
62  		if (value == null) {
63  			return null;
64  		}
65  		return value.toString();
66  	}
67  
68  	/**
69  	 * 数値を変換して返します。
70  	 * 
71  	 * @param number
72  	 *            変換元の数値
73  	 * @return 変換結果の数値
74  	 */
75  	protected abstract Number convert(Number number);
76  
77  }