View Javadoc

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   * @since 1.0.0
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  }