1 package org.hibernate.validator; 3 4 import org.hibernate.mapping.Column; 5 import org.hibernate.mapping.Property; 6 7 13 public class MaxValidator implements Validator<Max>, PropertyConstraint { 14 15 private int max; 16 17 public void initialize(Max parameters) { 18 max = 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<=max; 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<=max; 35 } 36 else if (value instanceof Number ) { 37 long lv = ( (Number ) value ).longValue(); 38 return lv<=max; 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() + "<=" + max); 48 } 49 } 50 | Popular Tags |