View Javadoc

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