KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > validator > DoubleRangeValidator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package javax.faces.validator;
17
18 import javax.faces.component.StateHolder;
19 import javax.faces.component.UIComponent;
20 import javax.faces.context.FacesContext;
21
22 /**
23  * @author Manfred Geiler (latest modification by $Author: mwessendorf $)
24  * @author Thomas Spiegl
25  * @version $Revision: 1.7 $ $Date: 2004/07/01 22:01:11 $
26  * $Log: DoubleRangeValidator.java,v $
27  * Revision 1.7 2004/07/01 22:01:11 mwessendorf
28  * ASF switch
29  *
30  * Revision 1.6 2004/06/14 12:55:22 manolito
31  * Added missing CVS Log comment
32  *
33  */

34 public class DoubleRangeValidator
35         implements Validator, StateHolder
36 {
37     // FIELDS
38
public static final String JavaDoc VALIDATOR_ID = "javax.faces.DoubleRange";
39     public static final String JavaDoc MAXIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MAXIMUM";
40     public static final String JavaDoc MINIMUM_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.MINIMUM";
41     public static final String JavaDoc TYPE_MESSAGE_ID = "javax.faces.validator.DoubleRangeValidator.TYPE";
42
43     private Double JavaDoc _minimum = null;
44     private Double JavaDoc _maximum = null;
45     private boolean _transient = false;
46
47     // CONSTRUCTORS
48
public DoubleRangeValidator()
49     {
50     }
51
52     public DoubleRangeValidator(double maximum)
53     {
54         _maximum = new Double JavaDoc(maximum);
55     }
56
57     public DoubleRangeValidator(double maximum,
58                                 double minimum)
59     {
60         _maximum = new Double JavaDoc(maximum);
61         _minimum = new Double JavaDoc(minimum);
62     }
63
64     // VALIDATE
65
public void validate(FacesContext facesContext,
66                          UIComponent uiComponent,
67                          Object JavaDoc value)
68             throws ValidatorException
69     {
70         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
71         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
72
73         if (value == null)
74         {
75             return;
76         }
77
78         double dvalue = parseDoubleValue(facesContext, uiComponent,value);
79         if (_minimum != null && _maximum != null)
80         {
81             if (dvalue < _minimum.doubleValue() ||
82                 dvalue > _maximum.doubleValue())
83             {
84                 Object JavaDoc[] args = {_minimum, _maximum,uiComponent.getId()};
85                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, NOT_IN_RANGE_MESSAGE_ID, args));
86             }
87         }
88         else if (_minimum != null)
89         {
90             if (dvalue < _minimum.doubleValue())
91             {
92                 Object JavaDoc[] args = {_minimum,uiComponent.getId()};
93                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
94             }
95         }
96         else if (_maximum != null)
97         {
98             if (dvalue > _maximum.doubleValue())
99             {
100                 Object JavaDoc[] args = {_maximum,uiComponent.getId()};
101                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
102             }
103         }
104     }
105
106     private double parseDoubleValue(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc value)
107         throws ValidatorException
108     {
109         if (value instanceof Number JavaDoc)
110         {
111             return ((Number JavaDoc)value).doubleValue();
112         }
113         else
114         {
115             try
116             {
117                 return Double.parseDouble(value.toString());
118             }
119             catch (NumberFormatException JavaDoc e)
120             {
121                 Object JavaDoc[] args = {uiComponent.getId()};
122                throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, TYPE_MESSAGE_ID, args));
123             }
124         }
125     }
126
127
128     // GETTER & SETTER
129
public double getMaximum()
130     {
131         return _maximum != null ? _maximum.doubleValue() : Double.MAX_VALUE;
132     }
133
134     public void setMaximum(double maximum)
135     {
136         _maximum = new Double JavaDoc(maximum);
137     }
138
139     public double getMinimum()
140     {
141         return _minimum != null ? _minimum.doubleValue() : Double.MIN_VALUE;
142     }
143
144     public void setMinimum(double minimum)
145     {
146         _minimum = new Double JavaDoc(minimum);
147     }
148
149
150     // RESTORE/SAVE STATE
151
public Object JavaDoc saveState(FacesContext context)
152     {
153         Object JavaDoc values[] = new Object JavaDoc[2];
154         values[0] = _maximum;
155         values[1] = _minimum;
156         return values;
157     }
158
159     public void restoreState(FacesContext context,
160                              Object JavaDoc state)
161     {
162         Object JavaDoc values[] = (Object JavaDoc[])state;
163         _maximum = (Double JavaDoc)values[0];
164         _minimum = (Double JavaDoc)values[1];
165     }
166
167     public boolean isTransient()
168     {
169         return _transient;
170     }
171
172     public void setTransient(boolean transientValue)
173     {
174         _transient = transientValue;
175     }
176
177     // MISC
178
public boolean equals(Object JavaDoc o)
179     {
180         if (this == o) return true;
181         if (!(o instanceof DoubleRangeValidator)) return false;
182
183         final DoubleRangeValidator doubleRangeValidator = (DoubleRangeValidator)o;
184
185         if (_maximum != null ? !_maximum.equals(doubleRangeValidator._maximum) : doubleRangeValidator._maximum != null) return false;
186         if (_minimum != null ? !_minimum.equals(doubleRangeValidator._minimum) : doubleRangeValidator._minimum != null) return false;
187
188         return true;
189     }
190
191 }
192
Popular Tags