Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractIntegerNumberConverter |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2004-2009 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 | import java.math.BigInteger; | |
20 | ||
21 | import org.seasar.cubby.converter.ConversionException; | |
22 | import org.seasar.cubby.converter.ConversionHelper; | |
23 | import org.seasar.cubby.validator.MessageInfo; | |
24 | ||
25 | /** | |
26 | * 整数への変換を行うコンバータの抽象クラスです。 | |
27 | * <p> | |
28 | * 変換元のオブジェクトの文字列表現を値とする{@link BigInteger}からサブクラスが変換した結果を変換先とします。 | |
29 | * </p> | |
30 | * | |
31 | * @author baba | |
32 | * @since 1.1.0 | |
33 | */ | |
34 | 76 | public abstract class AbstractIntegerNumberConverter extends AbstractConverter { |
35 | ||
36 | /** | |
37 | * {@inheritDoc} | |
38 | */ | |
39 | public Object convertToObject(final Object value, | |
40 | final Class<?> objectType, final ConversionHelper helper) | |
41 | throws ConversionException { | |
42 | 55 | if (value == null) { |
43 | 0 | return null; |
44 | } | |
45 | 55 | return convert(value.toString()); |
46 | } | |
47 | ||
48 | /** | |
49 | * 数を表す文字列から数値に変換して返します。 | |
50 | * <p> | |
51 | * 型変換に失敗した場合はメッセージのキーを <code>valid.number</code> とした | |
52 | * {@link ConversionException} をスローします。 | |
53 | * </p> | |
54 | * | |
55 | * @param value | |
56 | * 数を表す文字列 | |
57 | * @return 変換結果の数値 | |
58 | * @throws ConversionException | |
59 | * 型変換に失敗した場合 | |
60 | */ | |
61 | protected Number convert(final String value) throws ConversionException { | |
62 | 55 | if (value == null || value.length() == 0) { |
63 | 1 | return null; |
64 | } | |
65 | ||
66 | final BigInteger integer; | |
67 | try { | |
68 | 54 | integer = new BigInteger(value); |
69 | 15 | } catch (final NumberFormatException e) { |
70 | try { | |
71 | 15 | new BigDecimal(value); |
72 | 1 | final MessageInfo messageInfo = new MessageInfo(); |
73 | 1 | messageInfo.setKey("valid.integer"); |
74 | 1 | throw new ConversionException(messageInfo); |
75 | 14 | } catch (final NumberFormatException e1) { |
76 | 14 | final MessageInfo messageInfo = new MessageInfo(); |
77 | 14 | messageInfo.setKey("valid.number"); |
78 | 14 | throw new ConversionException(messageInfo); |
79 | } | |
80 | 39 | } |
81 | ||
82 | 39 | final BigInteger min = this.getMinValue(); |
83 | 39 | final BigInteger max = this.getMaxValue(); |
84 | 39 | if ((min != null && min.compareTo(integer) > 0) |
85 | || (max != null && max.compareTo(integer) < 0)) { | |
86 | 1 | final MessageInfo messageInfo = new MessageInfo(); |
87 | 1 | messageInfo.setKey("valid.range"); |
88 | 1 | messageInfo.setArguments(min, max); |
89 | 1 | throw new ConversionException(messageInfo); |
90 | } | |
91 | ||
92 | 38 | return convert(integer); |
93 | } | |
94 | ||
95 | /** | |
96 | * 数値を変換して返します。 | |
97 | * | |
98 | * @param integer | |
99 | * 変換元の数値 | |
100 | * @return 変換結果の数値 | |
101 | */ | |
102 | protected abstract Number convert(BigInteger integer); | |
103 | ||
104 | /** | |
105 | * 最小値を取得します。 | |
106 | * <p> | |
107 | * 最小値をチェックしない場合は <code>null</code> を返します。 | |
108 | * </p> | |
109 | * | |
110 | * @return 最小値 | |
111 | */ | |
112 | protected abstract BigInteger getMinValue(); | |
113 | ||
114 | /** | |
115 | * 最大値を取得します。 | |
116 | * <p> | |
117 | * 最大値をチェックしない場合は <code>null</code> を返します。 | |
118 | * </p> | |
119 | * | |
120 | * @return 最大値 | |
121 | */ | |
122 | protected abstract BigInteger getMaxValue(); | |
123 | ||
124 | /** | |
125 | * {@inheritDoc} | |
126 | */ | |
127 | public String convertToString(final Object value, | |
128 | final ConversionHelper helper) { | |
129 | 4 | if (value == null) { |
130 | 0 | return null; |
131 | } | |
132 | 4 | return value.toString(); |
133 | } | |
134 | ||
135 | } |