3
3
import static io .avaje .validation .core .Util .canonicalize ;
4
4
import static io .avaje .validation .core .Util .canonicalizeClass ;
5
5
import static io .avaje .validation .core .Util .removeSubtypeWildcard ;
6
+ import static java .time .Duration .ZERO ;
6
7
import static java .util .Objects .requireNonNull ;
7
8
8
9
import java .lang .annotation .Annotation ;
@@ -179,14 +180,17 @@ String interpolate(Message msg, Locale requestLocale) {
179
180
/** Implementation of Validator.Builder. */
180
181
static final class DBuilder implements Validator .Builder {
181
182
183
+ private static final Validator DEFAULT = Validator .builder ().build ();
184
+ private static final Supplier <Clock > DEFAULT_CLOCK = Clock ::systemDefaultZone ;
185
+
182
186
private final List <AdapterFactory > factories = new ArrayList <>();
183
187
private final List <AnnotationFactory > afactories = new ArrayList <>();
184
188
private final List <String > bundleNames = new ArrayList <>();
185
189
private final List <ResourceBundle > bundles = new ArrayList <>();
186
190
private final List <Locale > otherLocales = new ArrayList <>();
187
191
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 ;
190
194
private boolean failfast ;
191
195
private MessageInterpolator userInterpolator ;
192
196
@@ -287,7 +291,12 @@ private void registerComponents() {
287
291
}
288
292
289
293
@ Override
290
- public DValidator build () {
294
+ public Validator build () {
295
+
296
+ if (!hasCustomizations ()) {
297
+ return DEFAULT ;
298
+ }
299
+
291
300
registerComponents ();
292
301
293
302
final var localeResolver = new LocaleResolver (defaultLocale , otherLocales );
@@ -308,6 +317,20 @@ public DValidator build() {
308
317
failfast );
309
318
}
310
319
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
+
311
334
private static <T > AnnotationFactory newAnnotationAdapterFactory (
312
335
Type type , ValidationAdapter <T > adapter ) {
313
336
requireNonNull (type );
0 commit comments