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.lang.reflect.Array;
18  import java.util.Collection;
19  import java.util.Map;
20  
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 SizeCheck extends AbstractAnnotationCheck<Size>
29  {
30  	private static final long serialVersionUID = 1L;
31  
32  	private int min;
33  	private int max;
34  
35  	/**
36  	 * {@inheritDoc}
37  	 */
38  	@Override
39  	public void configure(final Size constraintAnnotation)
40  	{
41  		super.configure(constraintAnnotation);
42  		setMax(constraintAnnotation.max());
43  		setMin(constraintAnnotation.min());
44  	}
45  
46  	/**
47  	 * {@inheritDoc}
48  	 */
49  	@Override
50  	protected Map<String, String> createMessageVariables()
51  	{
52  		final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
53  		messageVariables.put("max", Integer.toString(max));
54  		messageVariables.put("min", Integer.toString(min));
55  		return messageVariables;
56  	}
57  
58  	/**
59  	 * @return the max
60  	 */
61  	public int getMax()
62  	{
63  		return max;
64  	}
65  
66  	/**
67  	 * @return the min
68  	 */
69  	public int getMin()
70  	{
71  		return min;
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 */
77  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
78  			final Validator validator)
79  	{
80  		if (valueToValidate == null) return true;
81  
82  		if (valueToValidate instanceof Collection< ? >)
83  		{
84  			final int size = ((Collection< ? >) valueToValidate).size();
85  			return size >= min && size <= max;
86  		}
87  		if (valueToValidate instanceof Map< ? , ? >)
88  		{
89  			final int size = ((Map< ? , ? >) valueToValidate).size();
90  			return size >= min && size <= max;
91  		}
92  		if (valueToValidate.getClass().isArray())
93  		{
94  			final int size = Array.getLength(valueToValidate);
95  			return size >= min && size <= max;
96  		}
97  		final String str = valueToValidate.toString();
98  		final int size = str.length();
99  		return size >= min && size <= max;
100 	}
101 
102 	/**
103 	 * @param max the max to set
104 	 */
105 	public void setMax(final int max)
106 	{
107 		this.max = max;
108 		requireMessageVariablesRecreation();
109 	}
110 
111 	/**
112 	 * @param min the min to set
113 	 */
114 	public void setMin(final int min)
115 	{
116 		this.min = min;
117 		requireMessageVariablesRecreation();
118 	}
119 }