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