View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import java.text.DateFormat;
16  import java.text.ParseException;
17  import java.util.Calendar;
18  import java.util.Date;
19  
20  import net.sf.oval.ConstraintTarget;
21  import net.sf.oval.Validator;
22  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
23  import net.sf.oval.context.OValContext;
24  
25  /**
26   * @author Sebastian Thomschke
27   */
28  public class FutureCheck extends AbstractAnnotationCheck<Future>
29  {
30  	private static final long serialVersionUID = 1L;
31  
32  	private long tolerance;
33  
34  	@Override
35  	public void configure(final Future constraintAnnotation)
36  	{
37  		super.configure(constraintAnnotation);
38  		setTolerance(constraintAnnotation.tolerance());
39  	}
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	@Override
45  	protected ConstraintTarget[] getAppliesToDefault()
46  	{
47  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
48  	}
49  
50  	/**
51  	 * @return the tolerance
52  	 */
53  	public long getTolerance()
54  	{
55  		return tolerance;
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
62  			final Validator validator)
63  	{
64  		if (valueToValidate == null) return true;
65  
66  		final long now = System.currentTimeMillis() - tolerance;
67  
68  		// check if the value is a Date
69  		if (valueToValidate instanceof Date) // return ((Date) value).after(new Date());
70  			return ((Date) valueToValidate).getTime() > now;
71  
72  		// check if the value is a Calendar
73  		if (valueToValidate instanceof Calendar) return ((Calendar) valueToValidate).getTime().getTime() > now;
74  
75  		// see if we can extract a date based on the object's String representation
76  		final String stringValue = valueToValidate.toString();
77  		try
78  		{
79  			// return DateFormat.getDateTimeInstance().parse(stringValue).after(new Date());
80  			return DateFormat.getDateTimeInstance().parse(stringValue).getTime() > now;
81  		}
82  		catch (final ParseException ex)
83  		{
84  			return false;
85  		}
86  	}
87  
88  	/**
89  	 * @param tolerance the tolerance to set
90  	 */
91  	public void setTolerance(final long tolerance)
92  	{
93  		this.tolerance = tolerance;
94  	}
95  }