1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.seasar.cubby.converter.impl;
18
19 import java.math.BigDecimal;
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
28
29
30
31
32
33 public abstract class AbstractDecimalNumberConverter extends AbstractConverter {
34
35
36
37
38 public Object convertToObject(final Object value,
39 final Class<?> objectType, final ConversionHelper helper)
40 throws ConversionException {
41 if (value == null) {
42 return null;
43 }
44 return convert(value.toString());
45 }
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 protected Number convert(final String value) throws ConversionException {
61 if (value == null || value.length() == 0) {
62 return null;
63 }
64
65 final BigDecimal decimal;
66 try {
67 decimal = new BigDecimal(value);
68 } catch (final NumberFormatException e) {
69 final MessageInfo messageInfo = new MessageInfo();
70 messageInfo.setKey("valid.number");
71 throw new ConversionException(messageInfo);
72 }
73
74 final BigDecimal min = this.getMinValue();
75 final BigDecimal max = this.getMaxValue();
76 if ((min != null && min.compareTo(decimal) > 0)
77 || (max != null && max.compareTo(decimal) < 0)) {
78 final MessageInfo messageInfo = new MessageInfo();
79 messageInfo.setKey("valid.range");
80 messageInfo.setArguments(min.toPlainString(), max.toPlainString());
81 throw new ConversionException(messageInfo);
82 }
83
84 return convert(decimal);
85 }
86
87
88
89
90
91
92
93
94 protected abstract Number convert(BigDecimal decimal);
95
96
97
98
99
100
101
102
103
104 protected abstract BigDecimal getMinValue();
105
106
107
108
109
110
111
112
113
114 protected abstract BigDecimal getMaxValue();
115
116
117
118
119 public String convertToString(final Object value,
120 final ConversionHelper helper) {
121 if (value == null) {
122 return null;
123 }
124 return value.toString();
125 }
126
127 }