diff options
author | Denis Anisimov <denis@vaadin.com> | 2016-11-01 11:38:17 +0200 |
---|---|---|
committer | Denis Anisimov <denis@vaadin.com> | 2016-11-01 17:14:19 +0200 |
commit | e4d132f4cb84a7aafdf70b5e5ca2121b66c87bfa (patch) | |
tree | f4a1a5160cafb94482e12d8f0dda8ce9ba82146c /server/src/main/java/com/vaadin/data/validator | |
parent | 855ec0f67951da7f4392ae704796340e1d1a3ff3 (diff) | |
download | vaadin-framework-e4d132f4cb84a7aafdf70b5e5ca2121b66c87bfa.tar.gz vaadin-framework-e4d132f4cb84a7aafdf70b5e5ca2121b66c87bfa.zip |
Introduces ValidationResult interface to replace Result in validators.
Fixes vaadin/framework8-issues#414
Change-Id: I2d4e101f29542fadfdd30ce63455a0e078e0e735
Diffstat (limited to 'server/src/main/java/com/vaadin/data/validator')
7 files changed, 27 insertions, 25 deletions
diff --git a/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java b/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java index b798d2bf73..793797ee3f 100644 --- a/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/AbstractValidator.java @@ -17,7 +17,7 @@ package com.vaadin.data.validator; import java.util.Objects; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; import com.vaadin.server.SerializableFunction; @@ -79,7 +79,8 @@ public abstract class AbstractValidator<T> implements Validator<T> { * whether the value is valid or not * @return the validation result */ - protected Result<T> toResult(T value, boolean isValid) { - return isValid ? Result.ok(value) : Result.error(getMessage(value)); + protected ValidationResult toResult(T value, boolean isValid) { + return isValid ? ValidationResult.ok() + : ValidationResult.error(getMessage(value)); } } diff --git a/server/src/main/java/com/vaadin/data/validator/BeanValidator.java b/server/src/main/java/com/vaadin/data/validator/BeanValidator.java index 53886c9840..a3f9b86a6e 100644 --- a/server/src/main/java/com/vaadin/data/validator/BeanValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/BeanValidator.java @@ -19,8 +19,8 @@ package com.vaadin.data.validator; import java.io.Serializable; import java.util.Locale; import java.util.Objects; +import java.util.Optional; import java.util.Set; -import java.util.function.BinaryOperator; import javax.validation.ConstraintViolation; import javax.validation.MessageInterpolator.Context; @@ -28,7 +28,7 @@ import javax.validation.Validation; import javax.validation.ValidatorFactory; import javax.validation.metadata.ConstraintDescriptor; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; import com.vaadin.data.util.BeanUtil; import com.vaadin.data.util.converter.ValueContext; @@ -116,16 +116,17 @@ public class BeanValidator implements Validator<Object> { * @return the validation result */ @Override - public Result<Object> apply(final Object value, ValueContext context) { + public ValidationResult apply(final Object value, ValueContext context) { Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator() .validateValue(beanType, propertyName, value); - BinaryOperator<Result<Object>> accumulator = (result1, - result2) -> result1.flatMap(val -> result2); Locale locale = context.getLocale().orElse(Locale.getDefault()); - return violations.stream().map(v -> Result.error(getMessage(v, locale))) - .reduce(Result.ok(value), accumulator); + Optional<ValidationResult> result = violations.stream() + .map(violation -> ValidationResult + .error(getMessage(violation, locale))) + .findFirst(); + return result.orElse(ValidationResult.ok()); } @Override diff --git a/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java b/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java index b033538949..2223c333cb 100644 --- a/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import java.util.Objects; import com.vaadin.data.HasValue; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; import com.vaadin.data.util.converter.ValueContext; @@ -67,11 +67,11 @@ public class NotEmptyValidator<T> implements Validator<T> { } @Override - public Result<T> apply(T value, ValueContext context) { + public ValidationResult apply(T value, ValueContext context) { if (Objects.isNull(value) || Objects.equals(value, "")) { - return Result.error(message); + return ValidationResult.error(message); } else { - return Result.ok(value); + return ValidationResult.ok(); } } diff --git a/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java b/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java index 349e000c68..c92aefac3b 100644 --- a/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/NotNullValidator.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import java.util.Objects; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -42,9 +42,9 @@ public class NotNullValidator extends AbstractValidator<String> { } @Override - public Result<String> apply(String value, ValueContext context) { - return Objects.isNull(value) ? Result.error(getMessage(value)) - : Result.ok(value); + public ValidationResult apply(String value, ValueContext context) { + return Objects.isNull(value) ? ValidationResult.error(getMessage(value)) + : ValidationResult.ok(); } } diff --git a/server/src/main/java/com/vaadin/data/validator/RangeValidator.java b/server/src/main/java/com/vaadin/data/validator/RangeValidator.java index 32effdd811..b8ad387308 100644 --- a/server/src/main/java/com/vaadin/data/validator/RangeValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/RangeValidator.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import java.util.Comparator; import java.util.Objects; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -98,7 +98,7 @@ public class RangeValidator<T> extends AbstractValidator<T> { * behavior depends on the used comparator. */ @Override - public Result<T> apply(T value, ValueContext context) { + public ValidationResult apply(T value, ValueContext context) { return toResult(value, isValid(value)); } diff --git a/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java b/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java index b9763e5199..89e4d81a53 100644 --- a/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/RegexpValidator.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -71,7 +71,7 @@ public class RegexpValidator extends AbstractValidator<String> { } @Override - public Result<String> apply(String value, ValueContext context) { + public ValidationResult apply(String value, ValueContext context) { return toResult(value, isValid(value)); } diff --git a/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java b/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java index 421cf13a29..b3e1da18eb 100644 --- a/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java +++ b/server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java @@ -16,7 +16,7 @@ package com.vaadin.data.validator; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -50,11 +50,11 @@ public class StringLengthValidator extends AbstractValidator<String> { } @Override - public Result<String> apply(String value, ValueContext context) { + public ValidationResult apply(String value, ValueContext context) { if (value == null) { return toResult(value, true); } - Result<?> lengthCheck = validator.apply(value.length(), context); + ValidationResult lengthCheck = validator.apply(value.length(), context); return toResult(value, !lengthCheck.isError()); } |