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.metadata.raw;
8
9 import java.util.Set;
10
11 import javax.validation.Valid;
12 import javax.validation.groups.ConvertGroup;
13
14 import org.hibernate.validator.internal.metadata.aggregated.CascadingMetaDataBuilder;
15 import org.hibernate.validator.internal.metadata.core.MetaConstraint;
16
17 /**
18  * Represents a (potentially) constrained Java element such as a type, field or
19  * method. Such an element has a set of {@link org.hibernate.validator.internal.metadata.core.MetaConstraint}s and can be
20  * marked for a cascaded validation. Furthermore each constrained element has a
21  * {@link ConfigurationSource configuration source} which determines its origin.
22  * <p>
23  * The hierarchy of constrained elements resembles the physical structure of the
24  * represented Java types. In particular it doesn't provide the notion of
25  * properties and it doesn't aggregate meta data for overridden elements in an
26  * inheritance hierarchy.
27  * </p>
28  * <p>
29  * Identity of implementations is based on the element location and constraint
30  * source. That means that for instance in a set there can be two configurations
31  * for one and the same Java field created by two different configuration
32  * sources (e.g. via annotation and XML) but not two configurations for the same
33  * field originating from one configuration source.
34  * </p>
35  * <p>
36  * Implementations are strictly read-only.
37  * </p>
38  *
39  * @author Gunnar Morling
40  */

41 public interface ConstrainedElement extends Iterable<MetaConstraint<?>> {
42
43     /**
44      * Returns the kind of this constrained element.
45      *
46      * @return The kind of this constrained element.
47      */

48     ConstrainedElementKind getKind();
49
50     /**
51      * Returns a set containing the constraints specified for this constrained
52      * element.
53      *
54      * @return A set with this constrained element's constraints. May be empty,
55      *         but never {@code null}.
56      */

57     Set<MetaConstraint<?>> getConstraints();
58
59     /**
60      * Returns the type argument constraints of this element, if any.
61      */

62     Set<MetaConstraint<?>> getTypeArgumentConstraints();
63
64     /**
65      * Returns the cascading metadata (e.g. {@link Valid} and {@link ConvertGroup}) for the element and the potential
66      * container elements.
67      */

68     CascadingMetaDataBuilder getCascadingMetaDataBuilder();
69
70     /**
71      * Whether this element is constrained or not. This is the caseif this
72      * element has at least one constraint or a cascaded validation shall be
73      * performed for it.
74      *
75      * @return {@code True}, if this element is constrained,
76      *         {@code false} otherwise.
77      */

78     boolean isConstrained();
79
80     /**
81      * Returns the configuration source contributing this constrained element.
82      */

83     ConfigurationSource getSource();
84
85     /**
86      * The kind of a {@link ConstrainedElement}. Can be used to determine an
87      * element's type when traversing over a collection of constrained elements.
88      *
89      * @author Gunnar Morling
90      */

91     enum ConstrainedElementKind {
92
93         TYPE,
94         FIELD,
95         CONSTRUCTOR,
96         METHOD,
97         PARAMETER,
98         GETTER;
99
100         public boolean isExecutable() {
101             return this == CONSTRUCTOR || isMethod();
102         }
103
104         public boolean isMethod() {
105             return this == METHOD || this == GETTER;
106         }
107     }
108 }
109