1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.util.annotation;
8
9 import java.lang.annotation.Annotation;
10 import java.util.Map;
11
12 import javax.validation.ConstraintTarget;
13 import javax.validation.Payload;
14
15 import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
16
17 /**
18  * @author Marko Bekhta
19  */

20 public class ConstraintAnnotationDescriptor<A extends Annotation> extends AnnotationDescriptor<A> {
21
22     public ConstraintAnnotationDescriptor(A annotation) {
23         super( annotation );
24     }
25
26     public ConstraintAnnotationDescriptor(AnnotationDescriptor<A> descriptor) {
27         super( descriptor );
28     }
29
30     public String getMessage() {
31         return getMandatoryAttribute( ConstraintHelper.MESSAGE, String.class );
32     }
33
34     public Class<?>[] getGroups() {
35         return getMandatoryAttribute( ConstraintHelper.GROUPS, Class[].class );
36     }
37
38     @SuppressWarnings("unchecked")
39     public Class<? extends Payload>[] getPayload() {
40         return getMandatoryAttribute( ConstraintHelper.PAYLOAD, Class[].class );
41     }
42
43     public ConstraintTarget getValidationAppliesTo() {
44         return getAttribute( ConstraintHelper.VALIDATION_APPLIES_TO, ConstraintTarget.class );
45     }
46
47     public static class Builder<S extends Annotation> extends AnnotationDescriptor.Builder<S> {
48
49         public Builder(Class<S> type) {
50             super( type );
51         }
52
53         public Builder(Class<S> type, Map<String, Object> attributes) {
54             super( type, attributes );
55         }
56
57         public Builder(S annotation) {
58             super( annotation );
59         }
60
61         public Builder<S> setMessage(String message) {
62             setAttribute( ConstraintHelper.MESSAGE, message );
63             return this;
64         }
65
66         public Builder<S> setGroups(Class<?>[] groups) {
67             setAttribute( ConstraintHelper.GROUPS, groups );
68             return this;
69         }
70
71         public Builder<S> setPayload(Class<?>[] payload) {
72             setAttribute( ConstraintHelper.PAYLOAD, payload );
73             return this;
74         }
75
76         public Builder<S> setValidationAppliesTo(ConstraintTarget validationAppliesTo) {
77             setAttribute( ConstraintHelper.VALIDATION_APPLIES_TO, validationAppliesTo );
78             return this;
79         }
80
81         @Override
82         public ConstraintAnnotationDescriptor<S> build() {
83             return new ConstraintAnnotationDescriptor<>( super.build() );
84         }
85     }
86 }
87