1 /*
2  * Jakarta Bean Validation API
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 javax.validation.constraints;
8
9 import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
10 import static java.lang.annotation.ElementType.CONSTRUCTOR;
11 import static java.lang.annotation.ElementType.FIELD;
12 import static java.lang.annotation.ElementType.METHOD;
13 import static java.lang.annotation.ElementType.PARAMETER;
14 import static java.lang.annotation.ElementType.TYPE_USE;
15 import static java.lang.annotation.RetentionPolicy.RUNTIME;
16
17 import java.lang.annotation.Documented;
18 import java.lang.annotation.Repeatable;
19 import java.lang.annotation.Retention;
20 import java.lang.annotation.Target;
21
22 import javax.validation.Constraint;
23 import javax.validation.Payload;
24 import javax.validation.constraints.Pattern.List;
25
26 /**
27  * The annotated {@code CharSequence} must match the specified regular expression.
28  * The regular expression follows the Java regular expression conventions
29  * see {@link java.util.regex.Pattern}.
30  * <p>
31  * Accepts {@code CharSequence}. {@code null} elements are considered valid.
32  *
33  * @author Emmanuel Bernard
34  */

35 @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
36 @Retention(RUNTIME)
37 @Repeatable(List.class)
38 @Documented
39 @Constraint(validatedBy = { })
40 public @interface Pattern {
41
42     /**
43      * @return the regular expression to match
44      */

45     String regexp();
46
47     /**
48      * @return array of {@code Flag}s considered when resolving the regular expression
49      */

50     Flag[] flags() default { };
51
52     /**
53      * @return the error message template
54      */

55     String message() default "{javax.validation.constraints.Pattern.message}";
56
57     /**
58      * @return the groups the constraint belongs to
59      */

60     Class<?>[] groups() default { };
61
62     /**
63      * @return the payload associated to the constraint
64      */

65     Class<? extends Payload>[] payload() default { };
66
67     /**
68      * Possible Regexp flags.
69      */

70     public static enum Flag {
71
72         /**
73          * Enables Unix lines mode.
74          *
75          * @see java.util.regex.Pattern#UNIX_LINES
76          */

77         UNIX_LINES( java.util.regex.Pattern.UNIX_LINES ),
78
79         /**
80          * Enables case-insensitive matching.
81          *
82          * @see java.util.regex.Pattern#CASE_INSENSITIVE
83          */

84         CASE_INSENSITIVE( java.util.regex.Pattern.CASE_INSENSITIVE ),
85
86         /**
87          * Permits whitespace and comments in pattern.
88          *
89          * @see java.util.regex.Pattern#COMMENTS
90          */

91         COMMENTS( java.util.regex.Pattern.COMMENTS ),
92
93         /**
94          * Enables multiline mode.
95          *
96          * @see java.util.regex.Pattern#MULTILINE
97          */

98         MULTILINE( java.util.regex.Pattern.MULTILINE ),
99
100         /**
101          * Enables dotall mode.
102          *
103          * @see java.util.regex.Pattern#DOTALL
104          */

105         DOTALL( java.util.regex.Pattern.DOTALL ),
106
107         /**
108          * Enables Unicode-aware case folding.
109          *
110          * @see java.util.regex.Pattern#UNICODE_CASE
111          */

112         UNICODE_CASE( java.util.regex.Pattern.UNICODE_CASE ),
113
114         /**
115          * Enables canonical equivalence.
116          *
117          * @see java.util.regex.Pattern#CANON_EQ
118          */

119         CANON_EQ( java.util.regex.Pattern.CANON_EQ );
120
121         //JDK flag value
122         private final int value;
123
124         private Flag(int value) {
125             this.value = value;
126         }
127
128         /**
129          * @return flag value as defined in {@link java.util.regex.Pattern}
130          */

131         public int getValue() {
132             return value;
133         }
134     }
135
136     /**
137      * Defines several {@link Pattern} annotations on the same element.
138      *
139      * @see Pattern
140      */

141     @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
142     @Retention(RUNTIME)
143     @Documented
144     @interface List {
145
146         Pattern[] value();
147     }
148 }
149