1 package org.apache.turbine.services.intake.validator; 2 3 18 19 import java.util.Map ; 20 21 import org.apache.commons.lang.StringUtils; 22 23 41 public class ShortValidator 42 extends NumberValidator 43 { 44 45 private short minValue = Short.MIN_VALUE; 46 47 48 private short maxValue = Short.MAX_VALUE; 49 50 56 public ShortValidator(Map paramMap) 57 throws InvalidMaskException 58 { 59 invalidNumberMessage = "Entry was not a valid Short"; 60 init(paramMap); 61 } 62 63 66 public ShortValidator() 67 { 68 } 69 70 76 public void init(Map paramMap) 77 throws InvalidMaskException 78 { 79 super.init(paramMap); 80 81 Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME); 82 if (constraint != null) 83 { 84 String param = constraint.getValue(); 85 minValue = Short.parseShort(param); 86 minValueMessage = constraint.getMessage(); 87 } 88 89 constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME); 90 if (constraint != null) 91 { 92 String param = constraint.getValue(); 93 maxValue = Short.parseShort(param); 94 maxValueMessage = constraint.getMessage(); 95 } 96 } 97 98 106 public void assertValidity(String testValue) 107 throws ValidationException 108 { 109 super.assertValidity(testValue); 110 111 if (required || StringUtils.isNotEmpty(testValue)) 112 { 113 short s = 0; 114 try 115 { 116 s = Short.parseShort(testValue); 117 } 118 catch (RuntimeException e) 119 { 120 errorMessage = invalidNumberMessage; 121 throw new ValidationException(invalidNumberMessage); 122 } 123 124 if (s < minValue) 125 { 126 errorMessage = minValueMessage; 127 throw new ValidationException(minValueMessage); 128 } 129 if (s > maxValue) 130 { 131 errorMessage = maxValueMessage; 132 throw new ValidationException(maxValueMessage); 133 } 134 } 135 } 136 137 138 142 147 public short getMinValue() 148 { 149 return minValue; 150 } 151 152 157 public void setMinValue(short minValue) 158 { 159 this.minValue = minValue; 160 } 161 162 167 public short getMaxValue() 168 { 169 return maxValue; 170 } 171 172 177 public void setMaxValue(short maxValue) 178 { 179 this.maxValue = maxValue; 180 } 181 } 182 | Popular Tags |