Skip to content

Commit

Permalink
Refactor MethodValidationAdapter constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Aug 23, 2023
1 parent ff069de commit 942800e
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ public class MethodValidationAdapter implements MethodValidator {
/**
* Create an instance using a default JSR-303 validator underneath.
*/
@SuppressWarnings("DataFlowIssue")
public MethodValidationAdapter() {
this.validator = SingletonSupplier.of(() -> Validation.buildDefaultValidatorFactory().getValidator());
this.validatorAdapter = SingletonSupplier.of(() -> new SpringValidatorAdapter(this.validator.get()));
this.validatorAdapter = initValidatorAdapter(this.validator);
}

/**
Expand All @@ -105,8 +104,14 @@ public MethodValidationAdapter() {
*/
@SuppressWarnings("DataFlowIssue")
public MethodValidationAdapter(ValidatorFactory validatorFactory) {
this.validator = SingletonSupplier.of(validatorFactory::getValidator);
this.validatorAdapter = SingletonSupplier.of(() -> new SpringValidatorAdapter(this.validator.get()));
if (validatorFactory instanceof SpringValidatorAdapter adapter) {
this.validator = () -> adapter;
this.validatorAdapter = () -> adapter;
}
else {
this.validator = SingletonSupplier.of(validatorFactory::getValidator);
this.validatorAdapter = SingletonSupplier.of(() -> new SpringValidatorAdapter(this.validator.get()));
}
}

/**
Expand All @@ -115,7 +120,7 @@ public MethodValidationAdapter(ValidatorFactory validatorFactory) {
*/
public MethodValidationAdapter(Validator validator) {
this.validator = () -> validator;
this.validatorAdapter = () -> new SpringValidatorAdapter(validator);
this.validatorAdapter = initValidatorAdapter(this.validator);
}

/**
Expand All @@ -124,7 +129,13 @@ public MethodValidationAdapter(Validator validator) {
*/
public MethodValidationAdapter(Supplier<Validator> validator) {
this.validator = validator;
this.validatorAdapter = () -> new SpringValidatorAdapter(this.validator.get());
this.validatorAdapter = initValidatorAdapter(validator);
}

private static Supplier<SpringValidatorAdapter> initValidatorAdapter(Supplier<Validator> validatorSupplier) {
Validator validator = validatorSupplier.get();
return (validator instanceof SpringValidatorAdapter validatorAdapter ?
(() -> validatorAdapter) : SingletonSupplier.of(() -> new SpringValidatorAdapter(validator)));
}


Expand Down

0 comments on commit 942800e

Please sign in to comment.