Skip to content

Return the same default instance if unchanged #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions validator/src/main/java/io/avaje/validation/core/DValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.avaje.validation.core.Util.canonicalize;
import static io.avaje.validation.core.Util.canonicalizeClass;
import static io.avaje.validation.core.Util.removeSubtypeWildcard;
import static java.time.Duration.ZERO;
import static java.util.Objects.requireNonNull;

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

private static final Validator DEFAULT = Validator.builder().build();
private static final Supplier<Clock> DEFAULT_CLOCK = Clock::systemDefaultZone;

private final List<AdapterFactory> factories = new ArrayList<>();
private final List<AnnotationFactory> afactories = new ArrayList<>();
private final List<String> bundleNames = new ArrayList<>();
private final List<ResourceBundle> bundles = new ArrayList<>();
private final List<Locale> otherLocales = new ArrayList<>();
private Locale defaultLocale = Locale.getDefault();
private Supplier<Clock> clockSupplier = Clock::systemDefaultZone;
private Duration temporalTolerance = Duration.ZERO;
private Supplier<Clock> clockSupplier = DEFAULT_CLOCK;
private Duration temporalTolerance = ZERO;
private boolean failfast;
private MessageInterpolator userInterpolator;

Expand Down Expand Up @@ -287,7 +291,12 @@ private void registerComponents() {
}

@Override
public DValidator build() {
public Validator build() {

if (!hasCustomizations()) {
return DEFAULT;
}

registerComponents();

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

private boolean hasCustomizations() {
return DEFAULT == null
|| failfast
|| userInterpolator != null
|| !otherLocales.isEmpty()
|| !Locale.getDefault().equals(defaultLocale)
|| !bundleNames.isEmpty()
|| !bundles.isEmpty()
|| !DEFAULT_CLOCK.equals(clockSupplier)
|| !ZERO.equals(temporalTolerance)
|| !factories.isEmpty()
|| !afactories.isEmpty();
}

private static <T> AnnotationFactory newAnnotationAdapterFactory(
Type type, ValidationAdapter<T> adapter) {
requireNonNull(type);
Expand Down
22 changes: 22 additions & 0 deletions validator/src/test/java/io/avaje/validation/core/TestBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.avaje.validation.core;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import io.avaje.validation.Validator;

class TestBuilder {

@Test
void defaultBuilderReturnSameInstance() {

assertThat(Validator.builder().build()).isSameAs(Validator.builder().build());
}

@Test
void changedBuilderNotSame() {

assertThat(Validator.builder().failFast(true).build()).isNotSameAs(Validator.builder().build());
}
}