KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: LengthValidator.java,v 1.1 2005/05/27 08:58:53 epbernard Exp $
2
package org.hibernate.validator;
3
4 import org.hibernate.mapping.Column;
5 import org.hibernate.mapping.Property;
6
7 /**
8  * Do check a length restriction on a string, and apply expected contraints on hibernate metadata.
9  *
10  * @author Gavin King
11  */

12 public class LengthValidator implements Validator<Length>, PropertyConstraint {
13     private int max;
14     private int min;
15     
16     public void initialize(Length 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         if ( !(value instanceof String JavaDoc) ) return false;
24         String JavaDoc string = (String JavaDoc) value;
25         int length = string.length();
26         return length>=min && length<=max;
27     }
28
29     public void apply(Property property) {
30         Column col = (Column) property.getColumnIterator().next();
31         col.setLength(max);
32     }
33
34 }
35
Popular Tags