01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.constraint;
13:
14: import java.text.DateFormat;
15: import java.text.ParseException;
16: import java.util.Calendar;
17: import java.util.Date;
18:
19: import net.sf.oval.Validator;
20: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
21: import net.sf.oval.context.OValContext;
22:
23: /**
24: * @author Sebastian Thomschke
25: */
26: public class PastCheck extends AbstractAnnotationCheck<Past> {
27: private static final long serialVersionUID = 1L;
28:
29: public boolean isSatisfied(final Object validatedObject,
30: final Object value, final OValContext context,
31: final Validator validator) {
32: if (value == null)
33: return true;
34:
35: // check if the value is a Date
36: if (value instanceof Date) {
37: // return ((Date) value).before(new Date());
38: return ((Date) value).getTime() < System
39: .currentTimeMillis();
40: }
41:
42: // check if the value is a Calendar
43: if (value instanceof Calendar) {
44: // return ((Calendar) value).getTime().before(new Date());
45: return ((Calendar) value).getTime().getTime() < System
46: .currentTimeMillis();
47: }
48:
49: // see if we can extract a date based on the object's String representation
50: final String stringValue = value.toString();
51: try {
52: // return DateFormat.getDateTimeInstance().parse(stringValue).before(new Date());
53: return DateFormat.getDateTimeInstance().parse(stringValue)
54: .getTime() < System.currentTimeMillis();
55: } catch (final ParseException ex) {
56: return false;
57: }
58: }
59: }
|