1 7 package com.inversoft.util.typevalidator; 8 9 10 import java.util.Locale ; 11 import java.util.Map ; 12 13 14 21 public class NumberTypeValidator extends BaseTypeValidator { 22 23 26 public static final String DEFAULT_MESSAGE = "Invalid number value"; 27 28 29 44 protected String internalValidate(Object value, Object params, String message, 45 Locale locale, Object [] mesgParams) { 46 47 Number number = null; 48 String error = null; 49 boolean valid = true; 50 if (value == null) { 51 valid = false; 52 } else if (value instanceof Number ) { 53 number = (Number ) value; 54 } else { 55 try { 56 number = Double.valueOf(value.toString()); 57 } catch (NumberFormatException nfe) { 58 valid = false; 59 } 60 } 61 62 if (valid && number != null && params != null) { 63 Object min = ((Map ) params).get("min"); 64 Object max = ((Map ) params).get("max"); 65 if (min != null) { 66 if (!(min instanceof Number )) { 67 try { 68 min = Double.valueOf(min.toString()); 69 } catch (NumberFormatException nfe) { 70 throw new NumberFormatException ("Invalid min value: " + 71 min); 72 } 73 } 74 75 if (min != null) { 76 if (number.doubleValue() < (((Number ) min).doubleValue())) { 77 valid = false; 78 } 79 } 80 } 81 82 if (max != null) { 83 if (!(max instanceof Number )) { 84 try { 85 max = Double.valueOf(max.toString()); 86 } catch (NumberFormatException nfe) { 87 throw new NumberFormatException ("Invalid max value: " + 88 max); 89 } 90 } 91 92 if (max != null) { 93 if (number.doubleValue() > (((Number ) max).doubleValue())) { 94 valid = false; 95 } 96 } 97 } 98 } 99 100 if (!valid) { 101 error = getErrorMessage(message, DEFAULT_MESSAGE, mesgParams); 102 } 103 104 return error; 105 } 106 } 107 | Popular Tags |