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