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 static net.sf.oval.Validator.getCollectionFactory;
16  
17  import java.util.Map;
18  
19  import net.sf.oval.ConstraintTarget;
20  import net.sf.oval.Validator;
21  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
22  import net.sf.oval.context.OValContext;
23  
24  /**
25   * @author Sebastian Thomschke
26   */
27  public class RangeCheck extends AbstractAnnotationCheck<Range>
28  {
29  	private static final long serialVersionUID = 1L;
30  
31  	private double min = Double.MIN_VALUE;
32  	private double max = Double.MAX_VALUE;
33  
34  	/**
35  	 * {@inheritDoc}
36  	 */
37  	@Override
38  	public void configure(final Range constraintAnnotation)
39  	{
40  		super.configure(constraintAnnotation);
41  		setMax(constraintAnnotation.max());
42  		setMin(constraintAnnotation.min());
43  	}
44  
45  	/**
46  	 * {@inheritDoc}
47  	 */
48  	@Override
49  	protected Map<String, String> createMessageVariables()
50  	{
51  		final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
52  		messageVariables.put("max", Double.toString(max));
53  		messageVariables.put("min", Double.toString(min));
54  		return messageVariables;
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	@Override
61  	protected ConstraintTarget[] getAppliesToDefault()
62  	{
63  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
64  	}
65  
66  	/**
67  	 * @return the max
68  	 */
69  	public double getMax()
70  	{
71  		return max;
72  	}
73  
74  	/**
75  	 * @return the min
76  	 */
77  	public double getMin()
78  	{
79  		return min;
80  	}
81  
82  	/**
83  	 * {@inheritDoc}
84  	 */
85  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
86  			final Validator validator)
87  	{
88  		if (valueToValidate == null) return true;
89  
90  		if (valueToValidate instanceof Number)
91  		{
92  			final double doubleValue = ((Number) valueToValidate).doubleValue();
93  			return doubleValue >= min && doubleValue <= max;
94  		}
95  
96  		final String stringValue = valueToValidate.toString();
97  		try
98  		{
99  			final double doubleValue = Double.parseDouble(stringValue);
100 			return doubleValue >= min && doubleValue <= max;
101 		}
102 		catch (final NumberFormatException e)
103 		{
104 			return false;
105 		}
106 	}
107 
108 	/**
109 	 * @param max the max to set
110 	 */
111 	public void setMax(final double max)
112 	{
113 		this.max = max;
114 		requireMessageVariablesRecreation();
115 	}
116 
117 	/**
118 	 * @param min the min to set
119 	 */
120 	public void setMin(final double min)
121 	{
122 		this.min = min;
123 		requireMessageVariablesRecreation();
124 	}
125 }