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 public class RangeValidator implements ScalarFieldValidator {
33
34
35
36
37 private final MessageHelper messageHelper;
38
39
40
41
42 private final long min;
43
44
45
46
47 private final long max;
48
49
50
51
52
53
54
55
56
57 public RangeValidator(final long min, final long max) {
58 this(min, max, "valid.range");
59 }
60
61
62
63
64
65
66
67
68
69
70
71 public RangeValidator(final long min, final long max,
72 final String messageKey) {
73 this.min = min;
74 this.max = max;
75 this.messageHelper = new MessageHelper(messageKey);
76 }
77
78 public void validate(final ValidationContext context, final Object value) {
79 if (value instanceof String) {
80 final String str = (String) value;
81 if (StringUtil.isEmpty(str)) {
82 return;
83 }
84 try {
85 final long longValue = Long.parseLong(str);
86 if (longValue >= min && longValue <= max) {
87 return;
88 }
89 } catch (final NumberFormatException e) {
90 }
91 } else if (value == null) {
92 return;
93 }
94 context.addMessageInfo(this.messageHelper.createMessageInfo(min, max));
95 }
96 }