1 package org.hibernate.validator; 3 4 import java.lang.reflect.Array ; 5 import java.util.Collection ; 6 import java.util.Map ; 7 8 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 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 ) { 28 length = ( (Collection ) value ).size(); 29 } 30 else if ( value instanceof Map ) { 31 length = ( (Map ) value ).size(); 32 } 33 else { 34 return false; 35 } 36 return length>=min && length<=max; 37 } 38 39 } 40 | Popular Tags |