1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.validator;
20
21 import javax.faces.component.StateHolder;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class LongRangeValidator
45 implements Validator, StateHolder
46 {
47
48 public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MAXIMUM";
49 public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.MINIMUM";
50 public static final String TYPE_MESSAGE_ID = "javax.faces.validator.LongRangeValidator.TYPE";
51 public static final String VALIDATOR_ID = "javax.faces.LongRange";
52
53 private Long _minimum = null;
54 private Long _maximum = null;
55 private boolean _transient = false;
56
57
58 public LongRangeValidator()
59 {
60 }
61
62 public LongRangeValidator(long maximum)
63 {
64 _maximum = new Long(maximum);
65 }
66
67 public LongRangeValidator(long maximum,
68 long minimum)
69 {
70 _maximum = new Long(maximum);
71 _minimum = new Long(minimum);
72 }
73
74
75 public void validate(FacesContext facesContext,
76 UIComponent uiComponent,
77 Object value)
78 throws ValidatorException
79 {
80 if (facesContext == null) throw new NullPointerException("facesContext");
81 if (uiComponent == null) throw new NullPointerException("uiComponent");
82
83 if (value == null)
84 {
85 return;
86 }
87
88 double dvalue = parseLongValue(facesContext, uiComponent,value);
89 if (_minimum != null && _maximum != null)
90 {
91 if (dvalue < _minimum.longValue() ||
92 dvalue > _maximum.longValue())
93 {
94 Object[] args = {_minimum, _maximum,uiComponent.getId()};
95 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, NOT_IN_RANGE_MESSAGE_ID, args));
96 }
97 }
98 else if (_minimum != null)
99 {
100 if (dvalue < _minimum.longValue())
101 {
102 Object[] args = {_minimum,uiComponent.getId()};
103 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
104 }
105 }
106 else if (_maximum != null)
107 {
108 if (dvalue > _maximum.longValue())
109 {
110 Object[] args = {_maximum,uiComponent.getId()};
111 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
112 }
113 }
114 }
115
116 private long parseLongValue(FacesContext facesContext, UIComponent uiComponent, Object value)
117 throws ValidatorException
118 {
119 if (value instanceof Number)
120 {
121 return ((Number)value).longValue();
122 }
123 else
124 {
125 try
126 {
127 return Long.parseLong(value.toString());
128 }
129 catch (NumberFormatException e)
130 {
131 Object[] args = {uiComponent.getId()};
132 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, TYPE_MESSAGE_ID, args));
133 }
134 }
135 }
136
137
138
139
140
141
142
143
144
145 public long getMaximum()
146 {
147 return _maximum != null ? _maximum.longValue() : Long.MAX_VALUE;
148 }
149
150 public void setMaximum(long maximum)
151 {
152 _maximum = new Long(maximum);
153 }
154
155
156
157
158
159
160 public long getMinimum()
161 {
162 return _minimum != null ? _minimum.longValue() : Long.MIN_VALUE;
163 }
164
165 public void setMinimum(long minimum)
166 {
167 _minimum = new Long(minimum);
168 }
169
170 public boolean isTransient()
171 {
172 return _transient;
173 }
174
175 public void setTransient(boolean transientValue)
176 {
177 _transient = transientValue;
178 }
179
180
181 public Object saveState(FacesContext context)
182 {
183 Object values[] = new Object[2];
184 values[0] = _maximum;
185 values[1] = _minimum;
186 return values;
187 }
188
189 public void restoreState(FacesContext context,
190 Object state)
191 {
192 Object values[] = (Object[])state;
193 _maximum = (Long)values[0];
194 _minimum = (Long)values[1];
195 }
196
197
198 public boolean equals(Object o)
199 {
200 if (this == o) return true;
201 if (!(o instanceof LongRangeValidator)) return false;
202
203 final LongRangeValidator longRangeValidator = (LongRangeValidator)o;
204
205 if (_maximum != null ? !_maximum.equals(longRangeValidator._maximum) : longRangeValidator._maximum != null) return false;
206 if (_minimum != null ? !_minimum.equals(longRangeValidator._minimum) : longRangeValidator._minimum != null) return false;
207
208 return true;
209 }
210
211 }