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.core;
8
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 /**
18  * The list of built-in constraints.
19  * <p>
20  * We are not using the class directly to avoid loading the class if possible.
21  * <p>
22  * In the case of composing constraints, you need to define the dependencies manually.
23  * <p>
24  * When adding a new built-in constraint, please add a test in {@code MessagePropertiesTest} and
25  * {@code PredefinedScopeAllConstraintsTest}.
26  *
27  * @author Guillaume Smet
28  */

29 enum BuiltinConstraint {
30
31     // Specification constraints
32     JAVAX_VALIDATION_CONSTRAINTS_ASSERT_FALSE("javax.validation.constraints.AssertFalse"),
33     JAVAX_VALIDATION_CONSTRAINTS_ASSERT_TRUE("javax.validation.constraints.AssertTrue"),
34     JAVAX_VALIDATION_CONSTRAINTS_DECIMAL_MAX("javax.validation.constraints.DecimalMax"),
35     JAVAX_VALIDATION_CONSTRAINTS_DECIMAL_MIN("javax.validation.constraints.DecimalMin"),
36     JAVAX_VALIDATION_CONSTRAINTS_DIGITS("javax.validation.constraints.Digits"),
37     JAVAX_VALIDATION_CONSTRAINTS_EMAIL("javax.validation.constraints.Email"),
38     JAVAX_VALIDATION_CONSTRAINTS_FUTURE("javax.validation.constraints.Future"),
39     JAVAX_VALIDATION_CONSTRAINTS_FUTURE_OR_PRESENT("javax.validation.constraints.FutureOrPresent"),
40     JAVAX_VALIDATION_CONSTRAINTS_MIN("javax.validation.constraints.Min"),
41     JAVAX_VALIDATION_CONSTRAINTS_MAX("javax.validation.constraints.Max"),
42     JAVAX_VALIDATION_CONSTRAINTS_NEGATIVE("javax.validation.constraints.Negative"),
43     JAVAX_VALIDATION_CONSTRAINTS_NEGATIVE_OR_ZERO("javax.validation.constraints.NegativeOrZero"),
44     JAVAX_VALIDATION_CONSTRAINTS_NOT_BLANK("javax.validation.constraints.NotBlank"),
45     JAVAX_VALIDATION_CONSTRAINTS_NOT_EMPTY("javax.validation.constraints.NotEmpty"),
46     JAVAX_VALIDATION_CONSTRAINTS_NOT_NULL("javax.validation.constraints.NotNull"),
47     JAVAX_VALIDATION_CONSTRAINTS_NULL("javax.validation.constraints.Null"),
48     JAVAX_VALIDATION_CONSTRAINTS_PAST("javax.validation.constraints.Past"),
49     JAVAX_VALIDATION_CONSTRAINTS_PAST_OR_PRESENT("javax.validation.constraints.PastOrPresent"),
50     JAVAX_VALIDATION_CONSTRAINTS_PATTERN("javax.validation.constraints.Pattern"),
51     JAVAX_VALIDATION_CONSTRAINTS_POSITIVE("javax.validation.constraints.Positive"),
52     JAVAX_VALIDATION_CONSTRAINTS_POSITIVE_OR_ZERO("javax.validation.constraints.PositiveOrZero"),
53     JAVAX_VALIDATION_CONSTRAINTS_SIZE("javax.validation.constraints.Size"),
54
55     // Hibernate Validator specific constraints
56     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_CODE_POINT_LENGTH("org.hibernate.validator.constraints.CodePointLength"),
57     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_CURRENCY("org.hibernate.validator.constraints.Currency"),
58     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_EMAIL("org.hibernate.validator.constraints.Email", Arrays.asList( JAVAX_VALIDATION_CONSTRAINTS_PATTERN ) ),
59     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_ISBN("org.hibernate.validator.constraints.ISBN"),
60     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_LENGTH("org.hibernate.validator.constraints.Length"),
61     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_LUHN_CHECK("org.hibernate.validator.constraints.LuhnCheck"),
62     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_CREDIT_CARD_NUMBER("org.hibernate.validator.constraints.CreditCardNumber",
63             Arrays.asList( ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_LUHN_CHECK ) ),
64     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_MOD10_CHECK("org.hibernate.validator.constraints.Mod10Check"),
65     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_MOD11_CHECK("org.hibernate.validator.constraints.Mod11Check"),
66     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_MOD_CHECK("org.hibernate.validator.constraints.ModCheck"),
67     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_NORMALIZED("org.hibernate.validator.constraints.Normalized"),
68     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_EAN("org.hibernate.validator.constraints.EAN", Arrays.asList( ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_MOD10_CHECK ) ),
69     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_NOT_BLANK("org.hibernate.validator.constraints.NotBlank",
70             Arrays.asList( JAVAX_VALIDATION_CONSTRAINTS_NOT_NULL ) ),
71     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_NOT_EMPTY("org.hibernate.validator.constraints.NotEmpty",
72             Arrays.asList( JAVAX_VALIDATION_CONSTRAINTS_NOT_NULL, JAVAX_VALIDATION_CONSTRAINTS_SIZE )),
73     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_PARAMETER_SCRIPT_ASSERT("org.hibernate.validator.constraints.ParameterScriptAssert"),
74     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_RANGE("org.hibernate.validator.constraints.Range",
75             Arrays.asList( JAVAX_VALIDATION_CONSTRAINTS_MIN, JAVAX_VALIDATION_CONSTRAINTS_MAX )),
76     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_SAFE_HTML("org.hibernate.validator.constraints.SafeHtml"),
77     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_SCRIPT_ASSERT("org.hibernate.validator.constraints.ScriptAssert"),
78     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_URL("org.hibernate.validator.constraints.URL",
79             Arrays.asList( JAVAX_VALIDATION_CONSTRAINTS_PATTERN )),
80     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_UNIQUE_ELEMENTS("org.hibernate.validator.constraints.UniqueElements"),
81     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_BR_CNPJ("org.hibernate.validator.constraints.br.CNPJ",
82             Arrays.asList( JAVAX_VALIDATION_CONSTRAINTS_PATTERN )),
83     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_BR_CPF("org.hibernate.validator.constraints.br.CPF",
84             Arrays.asList( JAVAX_VALIDATION_CONSTRAINTS_PATTERN )),
85     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_BR_TITULO_ELEITORAL("org.hibernate.validator.constraints.br.TituloEleitoral",
86             Arrays.asList( JAVAX_VALIDATION_CONSTRAINTS_PATTERN, ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_MOD11_CHECK ) ),
87     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_PL_NIP("org.hibernate.validator.constraints.pl.NIP"),
88     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_PL_PESEL("org.hibernate.validator.constraints.pl.PESEL"),
89     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_PL_REGON("org.hibernate.validator.constraints.pl.REGON"),
90     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_TIME_DURATION_MAX("org.hibernate.validator.constraints.time.DurationMax"),
91     ORG_HIBERNATE_VALIDATOR_CONSTRAINTS_TIME_DURATION_MIN("org.hibernate.validator.constraints.time.DurationMin");
92
93     private static final Map<String, Set<BuiltinConstraint>> CONSTRAINT_MAPPING;
94
95     static {
96         CONSTRAINT_MAPPING = new HashMap<>();
97         for ( BuiltinConstraint constraint : values() ) {
98             if ( constraint.constraintDependencies.isEmpty() ) {
99                 CONSTRAINT_MAPPING.put( constraint.annotationClassName, Collections.singleton( constraint ) );
100             }
101             else {
102                 Set<BuiltinConstraint> constraints = new HashSet<>();
103                 constraints.add( constraint );
104                 constraints.addAll( constraint.constraintDependencies );
105                 CONSTRAINT_MAPPING.put( constraint.annotationClassName, constraints );
106             }
107         }
108     }
109
110     private String annotationClassName;
111     private List<BuiltinConstraint> constraintDependencies;
112
113     BuiltinConstraint(String constraint) {
114         this( constraint, Collections.emptyList() );
115     }
116
117     BuiltinConstraint(String constraint, List<BuiltinConstraint> composingConstraints) {
118         this.annotationClassName = constraint;
119         this.constraintDependencies = composingConstraints;
120     }
121
122     static Set<BuiltinConstraint> resolve(Set<String> constraints) {
123         Set<BuiltinConstraint> resolvedConstraints = new HashSet<>();
124         for ( String constraint : constraints ) {
125             Set<BuiltinConstraint> builtinConstraints = CONSTRAINT_MAPPING.get( constraint );
126             if ( builtinConstraints != null ) {
127                 resolvedConstraints.addAll( builtinConstraints );
128             }
129         }
130         return resolvedConstraints;
131     }
132
133     static boolean isBuiltin(String constraint) {
134         return CONSTRAINT_MAPPING.containsKey( constraint );
135     }
136
137     static Set<String> set() {
138         return CONSTRAINT_MAPPING.keySet();
139     }
140 }
141