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.lang.annotation.Documented;
16  import java.lang.annotation.ElementType;
17  import java.lang.annotation.Retention;
18  import java.lang.annotation.RetentionPolicy;
19  import java.lang.annotation.Target;
20  
21  import net.sf.oval.ConstraintTarget;
22  import net.sf.oval.ConstraintViolation;
23  import net.sf.oval.configuration.annotation.Constraint;
24  import net.sf.oval.configuration.annotation.Constraints;
25  
26  /**
27   * Check if the array, map, or collection has the given size. 
28   * 
29   * For objects of other types the length of their String representation will be checked.
30   * 
31   * <br><br>
32   * <b>Note:</b> This constraint is also satisfied when the value to validate is null, therefore you might also need to specified @NotNull
33   * 
34   * @author Sebastian Thomschke
35   */
36  @Documented
37  @Retention(RetentionPolicy.RUNTIME)
38  @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
39  @Constraint(checkWith = SizeCheck.class)
40  public @interface Size
41  {
42  	@Documented
43  	@Retention(RetentionPolicy.RUNTIME)
44  	@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
45  	@Constraints
46  	public @interface List
47  	{
48  		/**
49  		 * The Size constraints.
50  		 */
51  		Size[] value();
52  
53  		/**
54  		 * Formula returning <code>true</code> if this constraint shall be evaluated and
55  		 * <code>false</code> if it shall be ignored for the current validation.
56  		 * <p>
57  		 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
58  		 * E.g. <code>groovy:_this.amount > 10</code>
59  		 * <p>
60  		 * Available context variables are:<br>
61  		 * <b>_this</b> -&gt; the validated bean<br>
62  		 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
63  		 *    or the validated bean for object level constraints)
64  		 */
65  		String when() default "";
66  	}
67  
68  	/**
69  	 * <p>In case the constraint is declared for an array, collection or map this controls how the constraint is applied to it and it's child objects.
70  	 * 
71  	 * <p><b>Default:</b> ConstraintTarget.CONTAINER
72  	 * 
73  	 * <p><b>Note:</b> This setting is ignored for object types other than array, map and collection.
74  	 */
75  	ConstraintTarget[] appliesTo() default ConstraintTarget.CONTAINER;
76  
77  	/**
78  	 * error code passed to the ConstraintViolation object
79  	 */
80  	String errorCode() default "net.sf.oval.constraint.Size";
81  
82  	int max() default Integer.MAX_VALUE;
83  
84  	/**
85  	 * message to be used for the ContraintsViolatedException
86  	 * 
87  	 * @see ConstraintViolation
88  	 */
89  	String message() default "net.sf.oval.constraint.Size.violated";
90  
91  	int min() default 0;
92  
93  	/**
94  	 * The associated constraint profiles.
95  	 */
96  	String[] profiles() default {};
97  
98  	/**
99  	 * severity passed to the ConstraintViolation object
100 	 */
101 	int severity() default 0;
102 
103 	/**
104 	 * An expression to specify where in the object graph relative from this object the expression
105 	 * should be applied.
106 	 * <p>
107 	 * Examples:
108 	 * <li>"owner" would apply this constraint to the current object's property <code>owner</code>
109 	 * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code>
110 	 * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied.
111 	 */
112 	String target() default "";
113 
114 	/**
115 	 * Formula returning <code>true</code> if this constraint shall be evaluated and
116 	 * <code>false</code> if it shall be ignored for the current validation.
117 	 * <p>
118 	 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
119 	 * E.g. <code>groovy:_this.amount > 10</code>
120 	 * <p>
121 	 * Available context variables are:<br>
122 	 * <b>_this</b> -&gt; the validated bean<br>
123 	 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
124 	 *    or the validated bean for object level constraints)
125 	 */
126 	String when() default "";
127 }