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