Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TimeValidator |
|
| 1.0;1 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.validator.routines; | |
18 | ||
19 | import java.text.DateFormat; | |
20 | import java.text.Format; | |
21 | import java.util.Calendar; | |
22 | import java.util.Locale; | |
23 | import java.util.TimeZone; | |
24 | ||
25 | /** | |
26 | * <p><b>Time Validation</b> and Conversion routines (<code>java.util.Calendar</code>).</p> | |
27 | * | |
28 | * <p>This validator provides a number of methods for validating/converting | |
29 | * a <code>String</code> time value to a <code>java.util.Calendar</code> using | |
30 | * <code>java.text.DateFormat</code> to parse either:</p> | |
31 | * <ul> | |
32 | * <li>using the default format for the default <code>Locale</code></li> | |
33 | * <li>using a specified pattern with the default <code>Locale</code></li> | |
34 | * <li>using the default format for a specified <code>Locale</code></li> | |
35 | * <li>using a specified pattern with a specified <code>Locale</code></li> | |
36 | * </ul> | |
37 | * | |
38 | * <p>For each of the above mechanisms, conversion method (i.e the | |
39 | * <code>validate</code> methods) implementations are provided which | |
40 | * either use the default <code>TimeZone</code> or allow the | |
41 | * <code>TimeZone</code> to be specified.</p> | |
42 | * | |
43 | * <p>Use one of the <code>isValid()</code> methods to just validate or | |
44 | * one of the <code>validate()</code> methods to validate and receive a | |
45 | * <i>converted</i> <code>Calendar</code> value for the time.</p> | |
46 | * | |
47 | * <p>Implementations of the <code>validate()</code> method are provided | |
48 | * to create <code>Calendar</code> objects for different <i>time zones</i> | |
49 | * if the system default is not appropriate.</p> | |
50 | * | |
51 | * <p>Alternatively the CalendarValidator's <code>adjustToTimeZone()</code> method | |
52 | * can be used to adjust the <code>TimeZone</code> of the <code>Calendar</code> | |
53 | * object afterwards.</p> | |
54 | * | |
55 | * <p>Once a value has been successfully converted the following | |
56 | * methods can be used to perform various time comparison checks:</p> | |
57 | * <ul> | |
58 | * <li><code>compareTime()</code> compares the hours, minutes, seconds | |
59 | * and milliseconds of two calendars, returning 0, -1 or +1 indicating | |
60 | * whether the first time is equal, before or after the second.</li> | |
61 | * <li><code>compareSeconds()</code> compares the hours, minutes and | |
62 | * seconds of two times, returning 0, -1 or +1 indicating | |
63 | * whether the first is equal to, before or after the second.</li> | |
64 | * <li><code>compareMinutes()</code> compares the hours and minutes | |
65 | * two times, returning 0, -1 or +1 indicating | |
66 | * whether the first is equal to, before or after the second.</li> | |
67 | * <li><code>compareHours()</code> compares the hours | |
68 | * of two times, returning 0, -1 or +1 indicating | |
69 | * whether the first is equal to, before or after the second.</li> | |
70 | * </ul> | |
71 | * | |
72 | * <p>So that the same mechanism used for parsing an <i>input</i> value | |
73 | * for validation can be used to format <i>output</i>, corresponding | |
74 | * <code>format()</code> methods are also provided. That is you can | |
75 | * format either:</p> | |
76 | * <ul> | |
77 | * <li>using a specified pattern</li> | |
78 | * <li>using the format for a specified <code>Locale</code></li> | |
79 | * <li>using the format for the <i>default</i> <code>Locale</code></li> | |
80 | * </ul> | |
81 | * | |
82 | * @version $Revision: 1739356 $ | |
83 | * @since Validator 1.3.0 | |
84 | */ | |
85 | public class TimeValidator extends AbstractCalendarValidator { | |
86 | ||
87 | private static final long serialVersionUID = 3494007492269691581L; | |
88 | ||
89 | 1 | private static final TimeValidator VALIDATOR = new TimeValidator(); |
90 | ||
91 | /** | |
92 | * Return a singleton instance of this validator. | |
93 | * @return A singleton instance of the TimeValidator. | |
94 | */ | |
95 | public static TimeValidator getInstance() { | |
96 | 1 | return VALIDATOR; |
97 | } | |
98 | ||
99 | /** | |
100 | * Construct a <i>strict</i> instance with <i>short</i> | |
101 | * time style. | |
102 | */ | |
103 | public TimeValidator() { | |
104 | 8 | this(true, DateFormat.SHORT); |
105 | 8 | } |
106 | ||
107 | /** | |
108 | * Construct an instance with the specified <i>strict</i> | |
109 | * and <i>time style</i> parameters. | |
110 | * | |
111 | * @param strict <code>true</code> if strict | |
112 | * <code>Format</code> parsing should be used. | |
113 | * @param timeStyle the time style to use for Locale validation. | |
114 | */ | |
115 | public TimeValidator(boolean strict, int timeStyle) { | |
116 | 8 | super(strict, -1, timeStyle); |
117 | 8 | } |
118 | ||
119 | /** | |
120 | * <p>Validate/convert a time using the default <code>Locale</code> | |
121 | * and <code>TimeZone</code>. | |
122 | * | |
123 | * @param value The value validation is being performed on. | |
124 | * @return The parsed <code>Calendar</code> if valid or <code>null</code> | |
125 | * if invalid. | |
126 | */ | |
127 | public Calendar validate(String value) { | |
128 | 1 | return (Calendar)parse(value, (String)null, (Locale)null, (TimeZone)null); |
129 | } | |
130 | ||
131 | /** | |
132 | * <p>Validate/convert a time using the specified <code>TimeZone</code> | |
133 | * and default <code>Locale</code>. | |
134 | * | |
135 | * @param value The value validation is being performed on. | |
136 | * @param timeZone The Time Zone used to parse the time, system default if null. | |
137 | * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. | |
138 | */ | |
139 | public Calendar validate(String value, TimeZone timeZone) { | |
140 | 1 | return (Calendar)parse(value, (String)null, (Locale)null, timeZone); |
141 | } | |
142 | ||
143 | /** | |
144 | * <p>Validate/convert a time using the specified <i>pattern</i> and | |
145 | * default <code>TimeZone</code>. | |
146 | * | |
147 | * @param value The value validation is being performed on. | |
148 | * @param pattern The pattern used to validate the value against. | |
149 | * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. | |
150 | */ | |
151 | public Calendar validate(String value, String pattern) { | |
152 | 21 | return (Calendar)parse(value, pattern, (Locale)null, (TimeZone)null); |
153 | } | |
154 | ||
155 | /** | |
156 | * <p>Validate/convert a time using the specified <i>pattern</i> | |
157 | * and <code>TimeZone</code>. | |
158 | * | |
159 | * @param value The value validation is being performed on. | |
160 | * @param pattern The pattern used to validate the value against. | |
161 | * @param timeZone The Time Zone used to parse the time, system default if null. | |
162 | * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. | |
163 | */ | |
164 | public Calendar validate(String value, String pattern, TimeZone timeZone) { | |
165 | 1 | return (Calendar)parse(value, pattern, (Locale)null, timeZone); |
166 | } | |
167 | ||
168 | /** | |
169 | * <p>Validate/convert a time using the specified <code>Locale</code> | |
170 | * default <code>TimeZone</code>. | |
171 | * | |
172 | * @param value The value validation is being performed on. | |
173 | * @param locale The locale to use for the time format, system default if null. | |
174 | * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. | |
175 | */ | |
176 | public Calendar validate(String value, Locale locale) { | |
177 | 18 | return (Calendar)parse(value, (String)null, locale, (TimeZone)null); |
178 | } | |
179 | ||
180 | /** | |
181 | * <p>Validate/convert a time using the specified specified <code>Locale</code> | |
182 | * and <code>TimeZone</code>. | |
183 | * | |
184 | * @param value The value validation is being performed on. | |
185 | * @param locale The locale to use for the time format, system default if null. | |
186 | * @param timeZone The Time Zone used to parse the time, system default if null. | |
187 | * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. | |
188 | */ | |
189 | public Calendar validate(String value, Locale locale, TimeZone timeZone) { | |
190 | 1 | return (Calendar)parse(value, (String)null, locale, timeZone); |
191 | } | |
192 | ||
193 | /** | |
194 | * <p>Validate/convert a time using the specified pattern and <code>Locale</code> | |
195 | * and the default <code>TimeZone</code>. | |
196 | * | |
197 | * @param value The value validation is being performed on. | |
198 | * @param pattern The pattern used to validate the value against, or the | |
199 | * default for the <code>Locale</code> if <code>null</code>. | |
200 | * @param locale The locale to use for the date format, system default if null. | |
201 | * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. | |
202 | */ | |
203 | public Calendar validate(String value, String pattern, Locale locale) { | |
204 | 1 | return (Calendar)parse(value, pattern, locale, (TimeZone)null); |
205 | } | |
206 | ||
207 | /** | |
208 | * <p>Validate/convert a time using the specified pattern, <code>Locale</code> | |
209 | * and <code>TimeZone</code>. | |
210 | * | |
211 | * @param value The value validation is being performed on. | |
212 | * @param pattern The pattern used to validate the value against, or the | |
213 | * default for the <code>Locale</code> if <code>null</code>. | |
214 | * @param locale The locale to use for the date format, system default if null. | |
215 | * @param timeZone The Time Zone used to parse the date, system default if null. | |
216 | * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid. | |
217 | */ | |
218 | public Calendar validate(String value, String pattern, Locale locale, TimeZone timeZone) { | |
219 | 1 | return (Calendar)parse(value, pattern, locale, timeZone); |
220 | } | |
221 | ||
222 | /** | |
223 | * <p>Compare Times (hour, minute, second and millisecond - not date).</p> | |
224 | * | |
225 | * @param value The <code>Calendar</code> value to check. | |
226 | * @param compare The <code>Calendar</code> to compare the value to. | |
227 | * @return Zero if the hours are equal, -1 if first | |
228 | * time is less than the seconds and +1 if the first | |
229 | * time is greater than. | |
230 | */ | |
231 | public int compareTime(Calendar value, Calendar compare) { | |
232 | 3 | return compareTime(value, compare, Calendar.MILLISECOND); |
233 | } | |
234 | ||
235 | /** | |
236 | * <p>Compare Seconds (hours, minutes and seconds).</p> | |
237 | * | |
238 | * @param value The <code>Calendar</code> value to check. | |
239 | * @param compare The <code>Calendar</code> to compare the value to. | |
240 | * @return Zero if the hours are equal, -1 if first | |
241 | * parameter's seconds are less than the seconds and +1 if the first | |
242 | * parameter's seconds are greater than. | |
243 | */ | |
244 | public int compareSeconds(Calendar value, Calendar compare) { | |
245 | 5 | return compareTime(value, compare, Calendar.SECOND); |
246 | } | |
247 | ||
248 | /** | |
249 | * <p>Compare Minutes (hours and minutes).</p> | |
250 | * | |
251 | * @param value The <code>Calendar</code> value to check. | |
252 | * @param compare The <code>Calendar</code> to compare the value to. | |
253 | * @return Zero if the hours are equal, -1 if first | |
254 | * parameter's minutes are less than the seconds and +1 if the first | |
255 | * parameter's minutes are greater than. | |
256 | */ | |
257 | public int compareMinutes(Calendar value, Calendar compare) { | |
258 | 5 | return compareTime(value, compare, Calendar.MINUTE); |
259 | } | |
260 | ||
261 | /** | |
262 | * <p>Compare Hours.</p> | |
263 | * | |
264 | * @param value The <code>Calendar</code> value to check. | |
265 | * @param compare The <code>Calendar</code> to compare the value to. | |
266 | * @return Zero if the hours are equal, -1 if first | |
267 | * parameter's hour is less than the seconds and +1 if the first | |
268 | * parameter's hour is greater than. | |
269 | */ | |
270 | public int compareHours(Calendar value, Calendar compare) { | |
271 | 5 | return compareTime(value, compare, Calendar.HOUR_OF_DAY); |
272 | } | |
273 | ||
274 | /** | |
275 | * <p>Convert the parsed <code>Date</code> to a <code>Calendar</code>.</p> | |
276 | * | |
277 | * @param value The parsed <code>Date</code> object created. | |
278 | * @param formatter The Format used to parse the value with. | |
279 | * @return The parsed value converted to a <code>Calendar</code>. | |
280 | */ | |
281 | @Override | |
282 | protected Object processParsedValue(Object value, Format formatter) { | |
283 | 35 | return ((DateFormat)formatter).getCalendar(); |
284 | } | |
285 | } |