KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > validator > SizeValidator


1 //$Id: SizeValidator.java,v 1.1 2005/05/27 08:58:54 epbernard Exp $
2
package org.hibernate.validator;
3
4 import java.lang.reflect.Array JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Map JavaDoc;
7
8 /**
9  * Check the size range according to the element
10  * @author Gavin King
11  */

12 public class SizeValidator implements Validator<Size> {
13     private int max;
14     private int min;
15     
16     public void initialize(Size parameters) {
17         max = parameters.max();
18         min = parameters.min();
19     }
20
21     public boolean isValid(Object JavaDoc value) {
22         if (value==null) return true;
23         int length;
24         if ( value.getClass().isArray() ) {
25             length = Array.getLength(value);
26         }
27         else if ( value instanceof Collection JavaDoc ) {
28             length = ( (Collection JavaDoc) value ).size();
29         }
30         else if ( value instanceof Map JavaDoc ) {
31             length = ( (Map JavaDoc) value ).size();
32         }
33         else {
34             return false;
35         }
36         return length>=min && length<=max;
37     }
38
39 }
40
Popular Tags