1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.validator.validators;
17
18 import org.seasar.cubby.validator.MessageHelper;
19 import org.seasar.cubby.validator.ScalarFieldValidator;
20 import org.seasar.cubby.validator.ValidationContext;
21 import org.seasar.framework.util.StringUtil;
22
23
24
25
26
27
28
29
30
31
32
33 public class RangeValidator implements ScalarFieldValidator {
34
35
36
37
38 private final MessageHelper messageHelper;
39
40
41
42
43 private final long min;
44
45
46
47
48 private final long max;
49
50
51
52
53
54
55
56
57
58 public RangeValidator(final long min, final long max) {
59 this(min, max, "valid.range");
60 }
61
62
63
64
65
66
67
68
69
70
71
72 public RangeValidator(final long min, final long max,
73 final String messageKey) {
74 this.min = min;
75 this.max = max;
76 this.messageHelper = new MessageHelper(messageKey);
77 }
78
79
80
81
82 public void validate(final ValidationContext context, final Object value) {
83 if (value instanceof String) {
84 final String str = (String) value;
85 if (StringUtil.isEmpty(str)) {
86 return;
87 }
88 try {
89 final long longValue = Long.parseLong(str);
90 if (longValue >= min && longValue <= max) {
91 return;
92 }
93 } catch (final NumberFormatException e) {
94 }
95 } else if (value == null) {
96 return;
97 }
98 context.addMessageInfo(this.messageHelper.createMessageInfo(min, max));
99 }
100 }