KickJava   Java API By Example, From Geeks To Geeks.

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


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: LongRangeValidator.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 LongRangeValidator
35         implements Validator, StateHolder
36 {
37     // FIELDS
38
public static final String JavaDoc MAXIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MAXIMUM";
39     public static final String JavaDoc MINIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MINIMUM";
40     public static final String JavaDoc TYPE_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.TYPE";
41     public static final String JavaDoc VALIDATOR_ID = "javax.faces.LongRange";
42
43     private Long JavaDoc _minimum = null;
44     private Long JavaDoc _maximum = null;
45     private boolean _transient = false;
46
47     // CONSTRUCTORS
48
public LongRangeValidator()
49     {
50     }
51
52     public LongRangeValidator(long maximum)
53     {
54         _maximum = new Long JavaDoc(maximum);
55     }
56
57     public LongRangeValidator(long maximum,
58                               long minimum)
59     {
60         _maximum = new Long JavaDoc(maximum);
61         _minimum = new Long 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 = parseLongValue(facesContext, uiComponent,value);
79         if (_minimum != null && _maximum != null)
80         {
81             if (dvalue < _minimum.longValue() ||
82                 dvalue > _maximum.longValue())
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.longValue())
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.longValue())
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 long parseLongValue(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc value)
107         throws ValidatorException
108     {
109         if (value instanceof Number JavaDoc)
110         {
111             return ((Number JavaDoc)value).longValue();
112         }
113         else
114         {
115             try
116             {
117                 return Long.parseLong(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 long getMaximum()
130     {
131         return _maximum != null ? _maximum.longValue() : Long.MAX_VALUE;
132     }
133
134     public void setMaximum(long maximum)
135     {
136         _maximum = new Long JavaDoc(maximum);
137     }
138
139     public long getMinimum()
140     {
141         return _minimum != null ? _minimum.longValue() : Long.MIN_VALUE;
142     }
143
144     public void setMinimum(long minimum)
145     {
146         _minimum = new Long JavaDoc(minimum);
147     }
148
149     public boolean isTransient()
150     {
151         return _transient;
152     }
153
154     public void setTransient(boolean transientValue)
155     {
156         _transient = transientValue;
157     }
158
159     // RESTORE & SAVE STATE
160
public Object JavaDoc saveState(FacesContext context)
161     {
162         Object JavaDoc values[] = new Object JavaDoc[2];
163         values[0] = _maximum;
164         values[1] = _minimum;
165         return values;
166     }
167
168     public void restoreState(FacesContext context,
169                              Object JavaDoc state)
170     {
171         Object JavaDoc values[] = (Object JavaDoc[])state;
172         _maximum = (Long JavaDoc)values[0];
173         _minimum = (Long JavaDoc)values[1];
174     }
175
176     // MISC
177
public boolean equals(Object JavaDoc o)
178     {
179         if (this == o) return true;
180         if (!(o instanceof LongRangeValidator)) return false;
181
182         final LongRangeValidator longRangeValidator = (LongRangeValidator)o;
183
184         if (_maximum != null ? !_maximum.equals(longRangeValidator._maximum) : longRangeValidator._maximum != null) return false;
185         if (_minimum != null ? !_minimum.equals(longRangeValidator._minimum) : longRangeValidator._minimum != null) return false;
186
187         return true;
188     }
189
190 }
191
Popular Tags