Skip to content

Commit 780af71

Browse files
gsmetgunnarmorling
authored andcommitted
HV-1480 Avoid creating a list in the common case when we only have the
default violation Also optimize a bit the concatenation of the default violation and the custom ones to avoid creating a list too small and resize it each time.
1 parent 5398159 commit 780af71

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

engine/src/main/java/org/hibernate/validator/internal/engine/constraintvalidation/ConstraintValidatorContextImpl.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
*/
77
package org.hibernate.validator.internal.engine.constraintvalidation;
88

9-
import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
10-
119
import java.util.ArrayList;
1210
import java.util.Collections;
1311
import java.util.HashMap;
@@ -30,6 +28,7 @@
3028

3129
import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext;
3230
import org.hibernate.validator.internal.engine.path.PathImpl;
31+
import org.hibernate.validator.internal.util.CollectionHelper;
3332
import org.hibernate.validator.internal.util.Contracts;
3433
import org.hibernate.validator.internal.util.logging.Log;
3534
import org.hibernate.validator.internal.util.logging.LoggerFactory;
@@ -47,9 +46,9 @@ public class ConstraintValidatorContextImpl implements HibernateConstraintValida
4746
private Map<String, Object> expressionVariables;
4847
private final List<String> methodParameterNames;
4948
private final ClockProvider clockProvider;
50-
private final List<ConstraintViolationCreationContext> constraintViolationCreationContexts = newArrayList( 3 );
5149
private final PathImpl basePath;
5250
private final ConstraintDescriptor<?> constraintDescriptor;
51+
private List<ConstraintViolationCreationContext> constraintViolationCreationContexts;
5352
private boolean defaultDisabled;
5453
private Object dynamicPayload;
5554

@@ -129,25 +128,34 @@ public final ConstraintDescriptor<?> getConstraintDescriptor() {
129128
}
130129

131130
public final List<ConstraintViolationCreationContext> getConstraintViolationCreationContexts() {
132-
if ( defaultDisabled && constraintViolationCreationContexts.size() == 0 ) {
133-
throw log.getAtLeastOneCustomMessageMustBeCreatedException();
131+
if ( defaultDisabled ) {
132+
if ( constraintViolationCreationContexts == null || constraintViolationCreationContexts.size() == 0 ) {
133+
throw log.getAtLeastOneCustomMessageMustBeCreatedException();
134+
}
135+
136+
return CollectionHelper.toImmutableList( constraintViolationCreationContexts );
134137
}
135138

136-
List<ConstraintViolationCreationContext> returnedConstraintViolationCreationContexts = new ArrayList<>(
137-
constraintViolationCreationContexts
138-
);
139-
if ( !defaultDisabled ) {
140-
returnedConstraintViolationCreationContexts.add(
141-
new ConstraintViolationCreationContext(
142-
getDefaultConstraintMessageTemplate(),
143-
basePath,
144-
messageParameters != null ? new HashMap<>( messageParameters ) : Collections.emptyMap(),
145-
expressionVariables != null ? new HashMap<>( expressionVariables ) : Collections.emptyMap(),
146-
dynamicPayload
147-
)
148-
);
139+
if ( constraintViolationCreationContexts == null || constraintViolationCreationContexts.size() == 0 ) {
140+
return Collections.singletonList( getDefaultConstraintViolationCreationContext() );
149141
}
150-
return returnedConstraintViolationCreationContexts;
142+
143+
List<ConstraintViolationCreationContext> returnedConstraintViolationCreationContexts =
144+
new ArrayList<>( constraintViolationCreationContexts.size() + 1 );
145+
returnedConstraintViolationCreationContexts.addAll( constraintViolationCreationContexts );
146+
returnedConstraintViolationCreationContexts.add( getDefaultConstraintViolationCreationContext() );
147+
148+
return CollectionHelper.toImmutableList( returnedConstraintViolationCreationContexts );
149+
}
150+
151+
private ConstraintViolationCreationContext getDefaultConstraintViolationCreationContext() {
152+
return new ConstraintViolationCreationContext(
153+
getDefaultConstraintMessageTemplate(),
154+
basePath,
155+
messageParameters != null ? new HashMap<>( messageParameters ) : Collections.emptyMap(),
156+
expressionVariables != null ? new HashMap<>( expressionVariables ) : Collections.emptyMap(),
157+
dynamicPayload
158+
);
151159
}
152160

153161
public List<String> getMethodParameterNames() {
@@ -164,6 +172,9 @@ protected NodeBuilderBase(String template, PathImpl path) {
164172
}
165173

166174
public ConstraintValidatorContext addConstraintViolation() {
175+
if ( constraintViolationCreationContexts == null ) {
176+
constraintViolationCreationContexts = CollectionHelper.newArrayList( 3 );
177+
}
167178
constraintViolationCreationContexts.add(
168179
new ConstraintViolationCreationContext(
169180
messageTemplate,

0 commit comments

Comments
 (0)