Skip to content

Commit d50a714

Browse files
authored
Return the same default instance if unchanged (#296)
Now the builder will return the exact same validator instance when no changes in the builder are detected
1 parent 813ecd9 commit d50a714

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

validator/src/main/java/io/avaje/validation/core/DValidator.java

+26-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static io.avaje.validation.core.Util.canonicalize;
44
import static io.avaje.validation.core.Util.canonicalizeClass;
55
import static io.avaje.validation.core.Util.removeSubtypeWildcard;
6+
import static java.time.Duration.ZERO;
67
import static java.util.Objects.requireNonNull;
78

89
import java.lang.annotation.Annotation;
@@ -179,14 +180,17 @@ String interpolate(Message msg, Locale requestLocale) {
179180
/** Implementation of Validator.Builder. */
180181
static final class DBuilder implements Validator.Builder {
181182

183+
private static final Validator DEFAULT = Validator.builder().build();
184+
private static final Supplier<Clock> DEFAULT_CLOCK = Clock::systemDefaultZone;
185+
182186
private final List<AdapterFactory> factories = new ArrayList<>();
183187
private final List<AnnotationFactory> afactories = new ArrayList<>();
184188
private final List<String> bundleNames = new ArrayList<>();
185189
private final List<ResourceBundle> bundles = new ArrayList<>();
186190
private final List<Locale> otherLocales = new ArrayList<>();
187191
private Locale defaultLocale = Locale.getDefault();
188-
private Supplier<Clock> clockSupplier = Clock::systemDefaultZone;
189-
private Duration temporalTolerance = Duration.ZERO;
192+
private Supplier<Clock> clockSupplier = DEFAULT_CLOCK;
193+
private Duration temporalTolerance = ZERO;
190194
private boolean failfast;
191195
private MessageInterpolator userInterpolator;
192196

@@ -287,7 +291,12 @@ private void registerComponents() {
287291
}
288292

289293
@Override
290-
public DValidator build() {
294+
public Validator build() {
295+
296+
if (!hasCustomizations()) {
297+
return DEFAULT;
298+
}
299+
291300
registerComponents();
292301

293302
final var localeResolver = new LocaleResolver(defaultLocale, otherLocales);
@@ -308,6 +317,20 @@ public DValidator build() {
308317
failfast);
309318
}
310319

320+
private boolean hasCustomizations() {
321+
return DEFAULT == null
322+
|| failfast
323+
|| userInterpolator != null
324+
|| !otherLocales.isEmpty()
325+
|| !Locale.getDefault().equals(defaultLocale)
326+
|| !bundleNames.isEmpty()
327+
|| !bundles.isEmpty()
328+
|| !DEFAULT_CLOCK.equals(clockSupplier)
329+
|| !ZERO.equals(temporalTolerance)
330+
|| !factories.isEmpty()
331+
|| !afactories.isEmpty();
332+
}
333+
311334
private static <T> AnnotationFactory newAnnotationAdapterFactory(
312335
Type type, ValidationAdapter<T> adapter) {
313336
requireNonNull(type);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.avaje.validation.core;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import io.avaje.validation.Validator;
8+
9+
class TestBuilder {
10+
11+
@Test
12+
void defaultBuilderReturnSameInstance() {
13+
14+
assertThat(Validator.builder().build()).isSameAs(Validator.builder().build());
15+
}
16+
17+
@Test
18+
void changedBuilderNotSame() {
19+
20+
assertThat(Validator.builder().failFast(true).build()).isNotSameAs(Validator.builder().build());
21+
}
22+
}

0 commit comments

Comments
 (0)