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.validator.ArrayFieldValidator;
20 import org.seasar.cubby.validator.ValidationContext;
21
22 /**
23 * 配列の最小サイズを検証します。
24 * <p>
25 * <table>
26 * <caption>検証エラー時に設定するエラーメッセージ</caption> <tbody>
27 * <tr>
28 * <th scope="row">デフォルトのキー</th>
29 * <td>valid.arrayMinSize</td>
30 * </tr>
31 * <tr>
32 * <th scope="row">置換文字列</th>
33 * <td>
34 * <ol start="0">
35 * <li>フィールド名</li>
36 * <li>このオブジェクトに設定された配列の最小サイズ</li>
37 * </ol></td>
38 * </tr>
39 * </tbody>
40 * </table>
41 * </p>
42 *
43 * @author agata
44 * @author baba
45 */
46 public class ArrayMinSizeValidator implements ArrayFieldValidator {
47
48 /**
49 * メッセージキー。
50 */
51 private final String messageKey;
52
53 /**
54 * 配列の最小サイズ
55 */
56 private final int min;
57
58 /**
59 * コンストラクタ
60 *
61 * @param min
62 * 配列の最小サイズ
63 */
64 public ArrayMinSizeValidator(final int min) {
65 this(min, "valid.minSize");
66 }
67
68 /**
69 * エラーメッセージキーを指定するコンストラクタ
70 *
71 * @param min
72 * 配列の最小サイズ
73 * @param messageKey
74 * エラーメッセージキー
75 */
76 public ArrayMinSizeValidator(final int min, final String messageKey) {
77 this.min = min;
78 this.messageKey = messageKey;
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 public void validate(final ValidationContext context, final Object[] values) {
85 if (values == null) {
86 return;
87 }
88 if (values.length >= min) {
89 return;
90 }
91
92 final MessageInfo messageInfo = new MessageInfo();
93 messageInfo.setKey(this.messageKey);
94 messageInfo.setArguments(min);
95 context.addMessageInfo(messageInfo);
96 }
97
98 }