KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typevalidator > NumberTypeValidator


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typevalidator;
8
9
10 import java.util.Locale JavaDoc;
11 import java.util.Map JavaDoc;
12
13
14 /**
15  * This class validates a number which could be a wrapper
16  * class or a String. This also handles min and max params
17  * in the HashMap of parameters passed in.
18  *
19  * @author Brian Pontarelli
20  */

21 public class NumberTypeValidator extends BaseTypeValidator {
22
23     /**
24      * The default error message for the number validator
25      */

26     public static final String JavaDoc DEFAULT_MESSAGE = "Invalid number value";
27
28
29     /**
30      * Validates a number and also optionally validates minimum and maximum based
31      * on java.lang.Number or String values passed into the method in the params
32      * parameter. This parameter must be a Map and the values must be stored under
33      * parameters named <code>min</code> and <code>max</code>
34      *
35      * @param value The value to validate
36      * @param params (Optional) A Map of parameters
37      * @param message (Optional) The error message to return
38      * @param locale Not used
39      * @param mesgParams Parameters to the message (if any are needed)
40      * @return The error message
41      * @throws NumberFormatException If the params min and/or max are not numbers
42      * or String representations of numbers
43      */

44     protected String JavaDoc internalValidate(Object JavaDoc value, Object JavaDoc params, String JavaDoc message,
45             Locale JavaDoc locale, Object JavaDoc[] mesgParams) {
46
47         Number JavaDoc number = null;
48         String JavaDoc error = null;
49         boolean valid = true;
50         if (value == null) {
51             valid = false;
52         } else if (value instanceof Number JavaDoc) {
53             number = (Number JavaDoc) value;
54         } else {
55             try {
56                 number = Double.valueOf(value.toString());
57             } catch (NumberFormatException JavaDoc nfe) {
58                 valid = false;
59             }
60         }
61
62         if (valid && number != null && params != null) {
63             Object JavaDoc min = ((Map JavaDoc) params).get("min");
64             Object JavaDoc max = ((Map JavaDoc) params).get("max");
65             if (min != null) {
66                 if (!(min instanceof Number JavaDoc)) {
67                     try {
68                         min = Double.valueOf(min.toString());
69                     } catch (NumberFormatException JavaDoc nfe) {
70                         throw new NumberFormatException JavaDoc("Invalid min value: " +
71                             min);
72                     }
73                 }
74
75                 if (min != null) {
76                     if (number.doubleValue() < (((Number JavaDoc) min).doubleValue())) {
77                         valid = false;
78                     }
79                 }
80             }
81
82             if (max != null) {
83                 if (!(max instanceof Number JavaDoc)) {
84                     try {
85                         max = Double.valueOf(max.toString());
86                     } catch (NumberFormatException JavaDoc nfe) {
87                         throw new NumberFormatException JavaDoc("Invalid max value: " +
88                             max);
89                     }
90                 }
91
92                 if (max != null) {
93                     if (number.doubleValue() > (((Number JavaDoc) 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