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.engine.validationcontext;
8
9 import java.time.Duration;
10
11 import javax.validation.ClockProvider;
12 import javax.validation.MessageInterpolator;
13 import javax.validation.Validator;
14
15 import org.hibernate.validator.internal.engine.ValidatorFactoryScopedContext;
16 import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
17 import org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory;
18
19 /**
20  * Context object storing the {@link Validator} level helper and configuration properties.
21  * <p>
22  * There should be only one per {@code Validator} instance.
23  */

24 public class ValidatorScopedContext {
25
26     /**
27      * The message interpolator.
28      */

29     private final MessageInterpolator messageInterpolator;
30
31     /**
32      * The parameter name provider.
33      */

34     private final ExecutableParameterNameProvider parameterNameProvider;
35
36     /**
37      * Provider for the current time when validating {@code @Future} or {@code @Past}
38      */

39     private final ClockProvider clockProvider;
40
41     /**
42      * Defines the temporal validation tolerance i.e. the allowed margin of error when comparing date/time in temporal
43      * constraints.
44      */

45     private final Duration temporalValidationTolerance;
46
47     /**
48      * Used to get the {@code ScriptEvaluatorFactory} when validating {@code @ScriptAssert} and
49      * {@code @ParameterScriptAssert} constraints.
50      */

51     private final ScriptEvaluatorFactory scriptEvaluatorFactory;
52
53     /**
54      * Hibernate Validator specific flag to abort validation on first constraint violation.
55      */

56     private final boolean failFast;
57
58     /**
59      * Hibernate Validator specific flag to disable the {@code TraversableResolver} result cache.
60      */

61     private final boolean traversableResolverResultCacheEnabled;
62
63     /**
64      * Hibernate Validator specific payload passed to the constraint validators.
65      */

66     private final Object constraintValidatorPayload;
67
68     public ValidatorScopedContext(ValidatorFactoryScopedContext validatorFactoryScopedContext) {
69         this.messageInterpolator = validatorFactoryScopedContext.getMessageInterpolator();
70         this.parameterNameProvider = validatorFactoryScopedContext.getParameterNameProvider();
71         this.clockProvider = validatorFactoryScopedContext.getClockProvider();
72         this.temporalValidationTolerance = validatorFactoryScopedContext.getTemporalValidationTolerance();
73         this.scriptEvaluatorFactory = validatorFactoryScopedContext.getScriptEvaluatorFactory();
74         this.failFast = validatorFactoryScopedContext.isFailFast();
75         this.traversableResolverResultCacheEnabled = validatorFactoryScopedContext.isTraversableResolverResultCacheEnabled();
76         this.constraintValidatorPayload = validatorFactoryScopedContext.getConstraintValidatorPayload();
77     }
78
79     public MessageInterpolator getMessageInterpolator() {
80         return this.messageInterpolator;
81     }
82
83     public ExecutableParameterNameProvider getParameterNameProvider() {
84         return this.parameterNameProvider;
85     }
86
87     public ClockProvider getClockProvider() {
88         return this.clockProvider;
89     }
90
91     public Duration getTemporalValidationTolerance() {
92         return this.temporalValidationTolerance;
93     }
94
95     public ScriptEvaluatorFactory getScriptEvaluatorFactory() {
96         return this.scriptEvaluatorFactory;
97     }
98
99     public boolean isFailFast() {
100         return this.failFast;
101     }
102
103     public boolean isTraversableResolverResultCacheEnabled() {
104         return this.traversableResolverResultCacheEnabled;
105     }
106
107     public Object getConstraintValidatorPayload() {
108         return this.constraintValidatorPayload;
109     }
110 }
111