1 /*
2  * DateParam.java
3  *
4  * jcmdline Rel. @VERSION@ $Id: DateParam.java,v 1.4 2009/08/07 16:13:28 lglawrence Exp $
5  *
6  * Classes:
7  *   public   DateParam
8  *
9  * ***** BEGIN LICENSE BLOCK *****
10  * Version: MPL 1.1
11  *
12  * The contents of this file are subject to the Mozilla Public License Version
13  * 1.1 (the "License"); you may not use this file except in compliance with
14  * the License. You may obtain a copy of the License at
15  * http://www.mozilla.org/MPL/
16  *
17  * Software distributed under the License is distributed on an "AS IS" basis,
18  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
19  * for the specific language governing rights and limitations under the
20  * License.
21  *
22  * The Original Code is the Java jcmdline (command line management) package.
23  *
24  * The Initial Developer of the Original Code is Lynne Lawrence.
25  *
26  * Portions created by the Initial Developer are Copyright (C) 2002
27  * the Initial Developer. All Rights Reserved.
28  *
29  * Contributor(s):  Lynne Lawrence <lgl@visuallink.com>
30  *
31  * ***** END LICENSE BLOCK *****
32  */
33 
34 package jcmdline;
35 
36 import java.text.ParseException;
37 import java.text.SimpleDateFormat;
38 import java.util.Date;
39 
40 /**
41  * A parameter that accepts a date as its value.
42  * <p>
43  * The format for the date is taken from the <code>strings</code>
44  * ResourceBundle.
45  * <p>
46  * Sample Usage:
47  *
48  * <pre>
49  *     DateParam startDateParam =
50  *         new DateParam(&quot;startDate&quot;,
51  *                       &quot;start date of report&quot;,
52  *                       DateParam.REQUIRED);
53  *     DateParam endDateParam =
54  *         new DateParam(&quot;endDate&quot;,
55  *                       &quot;end date of report&quot;,
56  *                       DateParam.REQUIRED);
57  *
58  *     // Time for startDate will be the beginning of the day by default.
59  *     // Set the time for the end of the report to be the end of the day.
60  *     endDateParam.setDefaultTime(23, 59, 58, 999);
61  *
62  *     CmdLineHandler cl = new DefaultCmdLineHandler(
63  *         &quot;myreport&quot;, &quot;report of activity over days&quot;,
64  *         new Parameter[] {},
65  *         new Parameter[] { startDateParam, endDateParam });
66  *
67  *     cl.parse();
68  *
69  *     // Don't need to check isSet() because params are REQUIRED
70  *     Date stDate = startDateParam.getValue();
71  *     Date enDate = endDateParam.getValue();
72  *     .
73  *     .
74  * </pre>
75  *
76  * @author Lynne Lawrence
77  * @version jcmdline Rel. @VERSION@ $Id: DateParam.java,v 1.2 2002/12/07
78  *          14:22:06 lglawrence Exp $
79  * @see DateTimeParam
80  * @see TimeParam
81  */
82 public class DateParam extends AbstractParameter<Date> {
83 
84 	private static final String sDateFmt = Strings.get("DateParam.dateFormat");
85 	private static final String sTimeFmt = "HH:mm:ss:SSS";
86 	private static final SimpleDateFormat dateFmt = new SimpleDateFormat(
87 			sDateFmt);
88 	private static final SimpleDateFormat dateFmtWTime = new SimpleDateFormat(
89 			sDateFmt + " " + sTimeFmt);
90 
91 	/**
92 	 * The default hours to be added to the date - defaults to 0
93 	 *
94 	 * @see #setDefaultTime(int,int,int,int) setDefaultTime()
95 	 * @see #getDefaultTime()
96 	 */
97 	private int defaultHours = 0;
98 
99 	/**
100 	 * The default milliseconds to be added to the date - defaults to 0
101 	 *
102 	 * @see #setDefaultTime(int,int,int,int) setDefaultTime()
103 	 * @see #getDefaultTime()
104 	 */
105 	private int defaultMilliSeconds = 0;
106 
107 	/**
108 	 * The default minutes to be added to the date - defaults to 0
109 	 *
110 	 * @see #setDefaultTime(int,int,int,int) setDefaultTime()
111 	 * @see #getDefaultTime()
112 	 */
113 	private int defaultMinutes = 0;
114 
115 	/**
116 	 * The default seconds to be added to the date - defaults to 0
117 	 *
118 	 * @see #setDefaultTime(int,int,int,int) setDefaultTime()
119 	 * @see #getDefaultTime()
120 	 */
121 	private int defaultSeconds = 0;
122 
123 	/**
124 	 * constructor - creates single-valued, optional, public parameter
125 	 *
126 	 * @param tag
127 	 *            a unique identifier for this parameter
128 	 * @param desc
129 	 *            a description of the parameter, suitable for display in a
130 	 *            usage statement
131 	 * @throws IllegalArgumentException
132 	 *             if <code>tag</code> or <desc> are invalid.
133 	 * @see AbstractParameter#setTag(String) setTag()
134 	 * @see AbstractParameter#setDesc(String) setDesc()
135 	 */
DateParam(String tag, String desc)136 	public DateParam(String tag, String desc) {
137 		this(tag, desc, OPTIONAL, SINGLE_VALUED, PUBLIC);
138 	}
139 
140 	/**
141 	 * constructor - creates single-valued, public parameter which will will be
142 	 * either optional or required, as specified.
143 	 *
144 	 * @param tag
145 	 *            a unique identifier for this parameter
146 	 * @param desc
147 	 *            a description of the parameter, suitable for display in a
148 	 *            usage statement
149 	 * @param optional
150 	 *            {@link Parameter#OPTIONAL OPTIONAL} if optional,
151 	 *            {@link Parameter#REQUIRED REQUIRED} if required
152 	 * @throws IllegalArgumentException
153 	 *             if any of the specified parameters are invalid.
154 	 * @see AbstractParameter#setTag(String) setTag()
155 	 * @see AbstractParameter#setDesc(String) setDesc()
156 	 */
DateParam(String tag, String desc, boolean optional)157 	public DateParam(String tag, String desc, boolean optional) {
158 		this(tag, desc, optional, SINGLE_VALUED, PUBLIC);
159 	}
160 
161 	/**
162 	 * constructor - creates a public parameter which will will be either
163 	 * optional or required, and/or multi-valued, as specified.
164 	 *
165 	 * @param tag
166 	 *            a unique identifier for this parameter
167 	 * @param desc
168 	 *            a description of the parameter, suitable for display in a
169 	 *            usage statement
170 	 * @param optional
171 	 *            {@link Parameter#OPTIONAL OPTIONAL} if optional,
172 	 *            {@link Parameter#REQUIRED REQUIRED} if required
173 	 * @param multiValued
174 	 *            {@link Parameter#MULTI_VALUED MULTI_VALUED} if the parameter
175 	 *            can accept multiple values, {@link Parameter#SINGLE_VALUED
176 	 *            SINGLE_VALUED} if the parameter can contain only a single
177 	 *            value
178 	 * @throws IllegalArgumentException
179 	 *             if any of the specified parameters are invalid.
180 	 * @see AbstractParameter#setTag(String) setTag()
181 	 * @see AbstractParameter#setDesc(String) setDesc()
182 	 * @see Parameter#SINGLE_VALUED SINGLE_VALUED
183 	 * @see Parameter#MULTI_VALUED MULTI_VALUED
184 	 */
DateParam(String tag, String desc, boolean optional, boolean multiValued)185 	public DateParam(String tag, String desc, boolean optional,
186 			boolean multiValued) {
187 		this(tag, desc, optional, multiValued, PUBLIC);
188 	}
189 
190 	/**
191 	 * constructor - creates a parameter which will will be either optional or
192 	 * required, single or multi-valued, and hidden or public as specified.
193 	 *
194 	 * @param tag
195 	 *            a unique identifier for this parameter
196 	 * @param desc
197 	 *            a description of the parameter, suitable for display in a
198 	 *            usage statement
199 	 * @param optional
200 	 *            {@link Parameter#OPTIONAL OPTIONAL} if optional,
201 	 *            {@link Parameter#REQUIRED REQUIRED} if required
202 	 * @param multiValued
203 	 *            {@link Parameter#MULTI_VALUED MULTI_VALUED} if the parameter
204 	 *            can accept multiple values, {@link Parameter#SINGLE_VALUED
205 	 *            SINGLE_VALUED} if the parameter can contain only a single
206 	 *            value
207 	 * @param hidden
208 	 *            {@link Parameter#HIDDEN HIDDEN} if parameter is not to be
209 	 *            listed in the usage, {@link Parameter#PUBLIC PUBLIC}
210 	 *            otherwise.
211 	 * @throws IllegalArgumentException
212 	 *             if any of the specified parameters are invalid.
213 	 * @see AbstractParameter#setTag(String) setTag()
214 	 * @see AbstractParameter#setDesc(String) setDesc()
215 	 * @see Parameter#SINGLE_VALUED SINGLE_VALUED
216 	 * @see Parameter#MULTI_VALUED MULTI_VALUED
217 	 * @see Parameter#HIDDEN HIDDEN
218 	 * @see Parameter#PUBLIC PUBLIC
219 	 */
DateParam(String tag, String desc, boolean optional, boolean multiValued, boolean hidden)220 	public DateParam(String tag, String desc, boolean optional,
221 			boolean multiValued, boolean hidden) {
222 
223 		this.setTag(tag);
224 		this.setDesc(desc);
225 		this.setOptional(optional);
226 		this.setMultiValued(multiValued);
227 		this.setHidden(hidden);
228 		this.setOptionLabel(sDateFmt);
229 	}
230 
231 	/**
232 	 * constructor - creates a single-valued, optional, public, number parameter
233 	 * whose value must be one of the specified values.
234 	 *
235 	 * @param tag
236 	 *            the tag associated with this parameter
237 	 * @param desc
238 	 *            a description of the parameter, suitable for display in a
239 	 *            usage statement
240 	 * @param acceptableValues
241 	 *            the acceptable values for the parameter
242 	 * @throws IllegalArgumentException
243 	 *             if any parameter is invalid.
244 	 * @see AbstractParameter#setTag(String) setTag()
245 	 * @see AbstractParameter#setDesc(String) setDesc()
246 	 * @see AbstractParameter#setAcceptableValues(Object[]) setAcceptableValues()
247 	 */
DateParam(String tag, String desc, Date[] acceptableValues)248 	public DateParam(String tag, String desc, Date[] acceptableValues) {
249 		this(tag, desc, acceptableValues, OPTIONAL, SINGLE_VALUED, PUBLIC);
250 	}
251 
252 	/**
253 	 * constructor - creates a single-valued, public, number parameter whose
254 	 * value must be one of the specified values, and which is required or
255 	 * optional, as specified.
256 	 *
257 	 * @param tag
258 	 *            the tag associated with this parameter
259 	 * @param desc
260 	 *            a description of the parameter, suitable for display in a
261 	 *            usage statement
262 	 * @param acceptableValues
263 	 *            the acceptable values for the parameter
264 	 * @param optional
265 	 *            {@link Parameter#OPTIONAL OPTIONAL} if optional,
266 	 *            {@link Parameter#REQUIRED REQUIRED} if required
267 	 * @throws IllegalArgumentException
268 	 *             if any parameter is invalid.
269 	 * @see AbstractParameter#setTag(String) setTag()
270 	 * @see AbstractParameter#setDesc(String) setDesc()
271 	 * @see AbstractParameter#setAcceptableValues(Object[]) setAcceptableValues()
272 	 * @see Parameter#OPTIONAL OPTIONAL
273 	 * @see Parameter#REQUIRED REQUIRED
274 	 */
DateParam(String tag, String desc, Date[] acceptableValues, boolean optional)275 	public DateParam(String tag, String desc, Date[] acceptableValues,
276 			boolean optional) {
277 		this(tag, desc, acceptableValues, optional, SINGLE_VALUED, PUBLIC);
278 	}
279 
280 	/**
281 	 * constructor - creates a public number parameter whose value must be one
282 	 * of the specified values, and which is required or optional and/or
283 	 * multi-valued, as specified.
284 	 *
285 	 * @param tag
286 	 *            the tag associated with this parameter
287 	 * @param desc
288 	 *            a description of the parameter, suitable for display in a
289 	 *            usage statement
290 	 * @param acceptableValues
291 	 *            the acceptable values for the parameter
292 	 * @param optional
293 	 *            {@link Parameter#OPTIONAL OPTIONAL} if optional,
294 	 *            {@link Parameter#REQUIRED REQUIRED} if required
295 	 * @param multiValued
296 	 *            {@link Parameter#MULTI_VALUED MULTI_VALUED} if the parameter
297 	 *            can accept multiple values, {@link Parameter#SINGLE_VALUED
298 	 *            SINGLE_VALUED} if the parameter can contain only a single
299 	 *            value
300 	 * @throws IllegalArgumentException
301 	 *             if any parameter is invalid.
302 	 * @see AbstractParameter#setTag(String) setTag()
303 	 * @see AbstractParameter#setDesc(String) setDesc()
304 	 * @see AbstractParameter#setAcceptableValues(Object[]) setAcceptableValues()
305 	 * @see Parameter#OPTIONAL OPTIONAL
306 	 * @see Parameter#REQUIRED REQUIRED
307 	 * @see Parameter#SINGLE_VALUED SINGLE_VALUED
308 	 * @see Parameter#MULTI_VALUED MULTI_VALUED
309 	 */
DateParam(String tag, String desc, Date[] acceptableValues, boolean optional, boolean multiValued)310 	public DateParam(String tag, String desc, Date[] acceptableValues,
311 			boolean optional, boolean multiValued) {
312 		this(tag, desc, acceptableValues, optional, multiValued, PUBLIC);
313 	}
314 
315 	/**
316 	 * constructor - creates a Parameter, all of whose options are specified.
317 	 *
318 	 * @param tag
319 	 *            the tag associated with this parameter
320 	 * @param desc
321 	 *            a description of the parameter, suitable for display in a
322 	 *            usage statement
323 	 * @param acceptableValues
324 	 *            the acceptable values for the parameter
325 	 * @param optional
326 	 *            {@link Parameter#OPTIONAL OPTIONAL} if optional,
327 	 *            {@link Parameter#REQUIRED REQUIRED} if required
328 	 * @param multiValued
329 	 *            {@link Parameter#MULTI_VALUED MULTI_VALUED} if the parameter
330 	 *            can accept multiple values, {@link Parameter#SINGLE_VALUED
331 	 *            SINGLE_VALUED} if the parameter can contain only a single
332 	 *            value
333 	 * @param hidden
334 	 *            {@link Parameter#HIDDEN HIDDEN} if parameter is not to be
335 	 *            listed in the usage, {@link Parameter#PUBLIC PUBLIC}
336 	 *            otherwise.
337 	 * @throws IllegalArgumentException
338 	 *             if any parameter is invalid.
339 	 * @see AbstractParameter#setTag(String) setTag()
340 	 * @see AbstractParameter#setDesc(String) setDesc()
341 	 * @see AbstractParameter#setAcceptableValues(Object[]) setAcceptableValues()
342 	 * @see Parameter#OPTIONAL OPTIONAL
343 	 * @see Parameter#REQUIRED REQUIRED
344 	 * @see Parameter#SINGLE_VALUED SINGLE_VALUED
345 	 * @see Parameter#MULTI_VALUED MULTI_VALUED
346 	 * @see Parameter#HIDDEN HIDDEN
347 	 * @see Parameter#PUBLIC PUBLIC
348 	 */
DateParam(String tag, String desc, Date[] acceptableValues, boolean optional, boolean multiValued, boolean hidden)349 	public DateParam(String tag, String desc, Date[] acceptableValues,
350 			boolean optional, boolean multiValued, boolean hidden) {
351 		this.setTag(tag);
352 		this.setAcceptableValues(acceptableValues);
353 		this.setDesc(desc);
354 		this.setOptional(optional);
355 		this.setMultiValued(multiValued);
356 		this.setHidden(hidden);
357 		this.setOptionLabel(sDateFmt);
358 	}
359 
360 	/**
361 	 * Gets the format used to parse the date/time values.
362 	 *
363 	 * @return the format used to parse the date/time values
364 	 */
getParseFormat()365 	static String getParseFormat() {
366 		return dateFmt.toLocalizedPattern();
367 	}
368 
369 	/**
370 	 * @see jcmdline.AbstractParameter#convertValue(java.lang.String)
371 	 */
convertValue(String val)372 	public Date convertValue(String val) throws CmdLineException {
373 		String sTime = String.format("%02d:%02d:%02d:%03d", defaultHours,
374 				defaultMinutes, defaultSeconds, defaultMilliSeconds);
375 		try {
376 			return dateFmtWTime.parse(val + " " + sTime);
377 		} catch (ParseException e) {
378 			throw new CmdLineException(Strings.get("DateParam.invalidDate",
379 					new Object[] { getTag(), sDateFmt }));
380 		}
381 	}
382 
383 	/**
384 	 * Gets default values for the time component used to generate the Date
385 	 * value.
386 	 *
387 	 * @return a 4 element <code>int</code> array, where the elements are the
388 	 *         default hours, minutes, seconds, and milliseconds, in that order
389 	 */
getDefaultTime()390 	public int[] getDefaultTime() {
391 		return new int[] { defaultHours, defaultMinutes, defaultSeconds,
392 				defaultMilliSeconds };
393 	}
394 
395 	/**
396 	 * Sets default values for the time component used to generate the Date
397 	 * value.
398 	 *
399 	 * @param h
400 	 *            the hours - 0-23 - defaults to 0
401 	 * @param m
402 	 *            the minutes - 0-59 - defaults to 0
403 	 * @param s
404 	 *            the seconds - 0-59 - defaults to 0
405 	 * @param ms
406 	 *            the milliseconds - 0-999 - defaults to 0
407 	 * @throws IllegalArgumentException
408 	 *             if any of the parameters are in error.
409 	 */
setDefaultTime(int h, int m, int s, int ms)410 	public void setDefaultTime(int h, int m, int s, int ms) {
411 		if (h < 0 || h > 23) {
412 			throw new IllegalArgumentException(Strings.get(
413 					"DateParam.invalidHours", new Object[] { new Integer(h) }));
414 		}
415 		if (m < 0 || m > 59) {
416 			throw new IllegalArgumentException(Strings
417 					.get("DateParam.invalidMinutes",
418 							new Object[] { new Integer(m) }));
419 		}
420 		if (s < 0 || s > 59) {
421 			throw new IllegalArgumentException(Strings
422 					.get("DateParam.invalidSeconds",
423 							new Object[] { new Integer(s) }));
424 		}
425 		if (ms < 0 || ms > 999) {
426 			throw new IllegalArgumentException(Strings.get(
427 					"DateParam.invalidMilliSeconds",
428 					new Object[] { new Integer(ms) }));
429 		}
430 		defaultHours = h;
431 		defaultMinutes = m;
432 		defaultSeconds = s;
433 		defaultMilliSeconds = ms;
434 	}
435 }
436