From e4d132f4cb84a7aafdf70b5e5ca2121b66c87bfa Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Tue, 1 Nov 2016 11:38:17 +0200 Subject: Introduces ValidationResult interface to replace Result in validators. Fixes vaadin/framework8-issues#414 Change-Id: I2d4e101f29542fadfdd30ce63455a0e078e0e735 --- .../java/com/vaadin/data/validator/AbstractValidator.java | 7 ++++--- .../java/com/vaadin/data/validator/BeanValidator.java | 15 ++++++++------- .../java/com/vaadin/data/validator/NotEmptyValidator.java | 8 ++++---- .../java/com/vaadin/data/validator/NotNullValidator.java | 8 ++++---- .../java/com/vaadin/data/validator/RangeValidator.java | 4 ++-- .../java/com/vaadin/data/validator/RegexpValidator.java | 4 ++-- .../com/vaadin/data/validator/StringLengthValidator.java | 6 +++--- 7 files changed, 27 insertions(+), 25 deletions(-) (limited to 'server/src/main/java/com/vaadin/data/validator') 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 implements Validator { * whether the value is valid or not * @return the validation result */ - protected Result 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 { * @return the validation result */ @Override - public Result apply(final Object value, ValueContext context) { + public ValidationResult apply(final Object value, ValueContext context) { Set> violations = getJavaxBeanValidator() .validateValue(beanType, propertyName, value); - BinaryOperator> 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 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 implements Validator { } @Override - public Result 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 { } @Override - public Result 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 extends AbstractValidator { * behavior depends on the used comparator. */ @Override - public Result 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 { } @Override - public Result 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 { } @Override - public Result 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()); } -- cgit v1.2.3