1 23 24 package org.infoglue.cms.util.validators; 25 26 import org.infoglue.cms.exception.ConstraintException; 27 import org.infoglue.cms.util.ConstraintExceptionBuffer; 28 29 42 public class IntegerValidator extends AbstractValidator 43 { 44 46 private static final String ILLEGAL_VALUE_ERROR_CODE = "306"; 48 49 50 51 53 private Range valueSpace; 55 56 57 58 61 64 IntegerValidator(String fieldName, boolean isRequired, int lowerLimit, int upperLimit) { 65 super(fieldName, isRequired); 66 this.valueSpace = new Range(lowerLimit, upperLimit); 67 } 68 69 70 71 73 76 public void validate(Integer value) throws ConstraintException { 77 validateIsRequired(value); 78 if(value == null) { return; 80 } 81 validateValueSpace(value); 82 failIfAnyExceptionsFound(); 83 } 84 85 87 public void validate(Integer value, ConstraintExceptionBuffer ceb) { 88 try { 89 validate(value); 90 } catch(ConstraintException e) { 91 ceb.add(e); 92 } 93 } 94 95 100 103 private void validateValueSpace(Integer value) { 104 if(!this.valueSpace.isWithinLimits(value.intValue())) { 105 addConstraintException(ILLEGAL_VALUE_ERROR_CODE); 106 } 107 } 108 109 110 111 } | Popular Tags |