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 DoubleValidator 42 extends NumberValidator 43 { 44 45 private double minValue = Double.NEGATIVE_INFINITY; 46 47 48 private double maxValue = Double.POSITIVE_INFINITY; 49 50 56 public DoubleValidator(Map paramMap) 57 throws InvalidMaskException 58 { 59 invalidNumberMessage = "Entry was not a valid Double"; 60 init(paramMap); 61 } 62 63 66 public DoubleValidator() 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 = Double.parseDouble(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 = Double.parseDouble(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 double d = 0.0D; 112 113 if (required || StringUtils.isNotEmpty(testValue)) 114 { 115 try 116 { 117 d = Double.parseDouble(testValue); 118 } 119 catch (RuntimeException e) 120 { 121 errorMessage = invalidNumberMessage; 122 throw new ValidationException(invalidNumberMessage); 123 } 124 125 if (d < minValue) 126 { 127 errorMessage = minValueMessage; 128 throw new ValidationException(minValueMessage); 129 } 130 if (d > maxValue) 131 { 132 errorMessage = maxValueMessage; 133 throw new ValidationException(maxValueMessage); 134 } 135 } 136 } 137 138 139 143 148 public double getMinValue() 149 { 150 return minValue; 151 } 152 153 158 public void setMinValue(double value) 159 { 160 this.minValue = value; 161 } 162 163 168 public double getMaxValue() 169 { 170 return maxValue; 171 } 172 173 178 public void setMaxValue(double value) 179 { 180 this.maxValue = value; 181 } 182 } 183 | Popular Tags |