1 package org.hibernate.validator; 3 4 import org.hibernate.mapping.Column; 5 import org.hibernate.mapping.Property; 6 7 13 public class MinValidator implements Validator<Min>, PropertyConstraint { 14 15 private int min; 16 17 public void initialize(Min parameters) { 18 min = parameters.value(); 19 } 20 21 public boolean isValid(Object value) { 22 if (value==null) return true; 23 if (value instanceof String ) { 24 try { 25 double dv = Double.parseDouble( (String ) value ); 26 return dv>=min; 27 } 28 catch (NumberFormatException nfe) { 29 return false; 30 } 31 } 32 else if ( (value instanceof Double ) || (value instanceof Float ) ) { 33 double dv = ( (Number ) value ).doubleValue(); 34 return dv>=min; 35 } 36 else if (value instanceof Number ) { 37 long lv = ( (Number ) value ).longValue(); 38 return lv>=min; 39 } 40 else { 41 return false; 42 } 43 } 44 45 public void apply(Property property) { 46 Column col = (Column) property.getColumnIterator().next(); 47 col.setCheckConstraint( col.getName() + ">=" + min ); 48 } 49 } 50 | Popular Tags |