Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RangeValidator |
|
| 4.0;4 |
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.validator.validators; | |
17 | ||
18 | import org.seasar.cubby.action.MessageInfo; | |
19 | import org.seasar.cubby.internal.util.StringUtils; | |
20 | import org.seasar.cubby.validator.ScalarFieldValidator; | |
21 | import org.seasar.cubby.validator.ValidationContext; | |
22 | ||
23 | /** | |
24 | * 数値の範囲を検証します。 | |
25 | * <p> | |
26 | * <table> | |
27 | * <caption>検証エラー時に設定するエラーメッセージ</caption> <tbody> | |
28 | * <tr> | |
29 | * <th scope="row">デフォルトのキー</th> | |
30 | * <td>valid.range</td> | |
31 | * </tr> | |
32 | * <tr> | |
33 | * <th scope="row">置換文字列</th> | |
34 | * <td> | |
35 | * <ol start="0"> | |
36 | * <li>フィールド名</li> | |
37 | * <li>このオブジェクトに設定された数値の最小値</li> | |
38 | * <li>このオブジェクトに設定された数値の最大値</li> | |
39 | * </ol></td> | |
40 | * </tr> | |
41 | * </tbody> | |
42 | * </table> | |
43 | * </p> | |
44 | * | |
45 | * @author agata | |
46 | * @author baba | |
47 | */ | |
48 | public class RangeValidator implements ScalarFieldValidator { | |
49 | ||
50 | /** | |
51 | * メッセージキー。 | |
52 | */ | |
53 | private final String messageKey; | |
54 | ||
55 | /** | |
56 | * 最小値 | |
57 | */ | |
58 | private final long min; | |
59 | ||
60 | /** | |
61 | * 最大値 | |
62 | */ | |
63 | private final long max; | |
64 | ||
65 | /** | |
66 | * コンストラクタ | |
67 | * | |
68 | * @param min | |
69 | * 最小値 | |
70 | * @param max | |
71 | * 最大値 | |
72 | */ | |
73 | public RangeValidator(final long min, final long max) { | |
74 | 5 | this(min, max, "valid.range"); |
75 | 5 | } |
76 | ||
77 | /** | |
78 | * エラーメッセージキーを指定するコンストラクタ | |
79 | * | |
80 | * @param min | |
81 | * 最小値 | |
82 | * @param max | |
83 | * 最大値 | |
84 | * @param messageKey | |
85 | * エラーメッセージキー | |
86 | */ | |
87 | public RangeValidator(final long min, final long max, | |
88 | 5 | final String messageKey) { |
89 | 5 | this.min = min; |
90 | 5 | this.max = max; |
91 | 5 | this.messageKey = messageKey; |
92 | 5 | } |
93 | ||
94 | /** | |
95 | * {@inheritDoc} | |
96 | */ | |
97 | public void validate(final ValidationContext context, final Object value) { | |
98 | 0 | if (value instanceof String) { |
99 | 0 | final String str = (String) value; |
100 | 0 | if (StringUtils.isEmpty(str)) { |
101 | 0 | return; |
102 | } | |
103 | try { | |
104 | 0 | final long longValue = Long.parseLong(str); |
105 | 0 | if (longValue >= min && longValue <= max) { |
106 | 0 | return; |
107 | } | |
108 | 0 | } catch (final NumberFormatException e) { |
109 | 0 | } |
110 | 0 | } else if (value == null) { |
111 | 0 | return; |
112 | } | |
113 | ||
114 | 0 | final MessageInfo messageInfo = new MessageInfo(); |
115 | 0 | messageInfo.setKey(this.messageKey); |
116 | 0 | messageInfo.setArguments(min, max); |
117 | 0 | context.addMessageInfo(messageInfo); |
118 | 0 | } |
119 | } |