diff options
22 files changed, 253 insertions, 122 deletions
diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index e139bcd5c8..f8ddace8a6 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -623,13 +623,30 @@ public class Binder<BEAN> implements Serializable { * Returns the field value run through all converters and validators, * but doesn't pass the {@link ValidationStatus} to any status handler. * + * @return the conversation result + */ + private Result<TARGET> doConversation() { + FIELDVALUE fieldValue = field.getValue(); + return converterValidatorChain.convertToModel(fieldValue, + createValueContext()); + } + + private ValidationStatus<TARGET> toValidationStatus( + Result<TARGET> result) { + return new ValidationStatus<>(this, + result.isError() + ? ValidationResult.error(result.getMessage().get()) + : ValidationResult.ok()); + } + + /** + * Returns the field value run through all converters and validators, + * but doesn't pass the {@link ValidationStatus} to any status handler. + * * @return the validation status */ private ValidationStatus<TARGET> doValidation() { - FIELDVALUE fieldValue = field.getValue(); - Result<TARGET> dataValue = converterValidatorChain - .convertToModel(fieldValue, createValueContext()); - return new ValidationStatus<>(this, dataValue); + return toValidationStatus(doConversation()); } /** @@ -677,7 +694,7 @@ public class Binder<BEAN> implements Serializable { // store field value if valid ValidationStatus<TARGET> fieldValidationStatus = writeFieldValue( bean); - List<Result<?>> binderValidationResults; + List<ValidationResult> binderValidationResults; // if all field level validations pass, run bean level validation if (!getBinder().bindings.stream().map(BindingImpl::doValidation) .anyMatch(ValidationStatus::isError)) { @@ -702,12 +719,11 @@ public class Binder<BEAN> implements Serializable { private ValidationStatus<TARGET> writeFieldValue(BEAN bean) { assert bean != null; - ValidationStatus<TARGET> validationStatus = doValidation(); + Result<TARGET> result = doConversation(); if (setter != null) { - validationStatus.getResult().ifPresent(result -> result - .ifOk(value -> setter.accept(bean, value))); + result.ifOk(value -> setter.accept(bean, value)); } - return validationStatus; + return toValidationStatus(result); } private void notifyStatusHandler(ValidationStatus<?> status) { @@ -740,10 +756,9 @@ public class Binder<BEAN> implements Serializable { @Override public Result<T> convertToModel(T value, ValueContext context) { - Result<? super T> validationResult = validator.apply(value, - context); + ValidationResult validationResult = validator.apply(value, context); if (validationResult.isError()) { - return Result.error(validationResult.getMessage().get()); + return Result.error(validationResult.getErrorMessage()); } else { return Result.ok(value); } @@ -1069,9 +1084,9 @@ public class Binder<BEAN> implements Serializable { bindings.forEach(binding -> binding.writeFieldValue(bean)); // Now run bean level validation against the updated bean - List<Result<?>> binderResults = validateBean(bean); - boolean hasErrors = binderResults.stream().filter(Result::isError) - .findAny().isPresent(); + List<ValidationResult> binderResults = validateBean(bean); + boolean hasErrors = binderResults.stream() + .filter(ValidationResult::isError).findAny().isPresent(); if (hasErrors) { // Bean validator failed, revert values bindings.forEach((BindingImpl binding) -> binding.setter @@ -1216,9 +1231,9 @@ public class Binder<BEAN> implements Serializable { * @return a list of validation errors or an empty list if validation * succeeded */ - private List<Result<?>> validateBean(BEAN bean) { + private List<ValidationResult> validateBean(BEAN bean) { Objects.requireNonNull(bean, "bean cannot be null"); - List<Result<?>> results = Collections.unmodifiableList(validators + List<ValidationResult> results = Collections.unmodifiableList(validators .stream() .map(validator -> validator.apply(bean, new ValueContext())) .collect(Collectors.toList())); @@ -1444,7 +1459,7 @@ public class Binder<BEAN> implements Serializable { // show first possible error or OK status in the label if set if (getStatusLabel().isPresent()) { String statusMessage = binderStatus.getBeanValidationErrors() - .stream().findFirst().flatMap(Result::getMessage) + .stream().findFirst().map(ValidationResult::getErrorMessage) .orElse(""); getStatusLabel().get().setValue(statusMessage); } diff --git a/server/src/main/java/com/vaadin/data/BinderValidationStatus.java b/server/src/main/java/com/vaadin/data/BinderValidationStatus.java index 9b7e40f516..41a181dacf 100644 --- a/server/src/main/java/com/vaadin/data/BinderValidationStatus.java +++ b/server/src/main/java/com/vaadin/data/BinderValidationStatus.java @@ -53,7 +53,7 @@ public class BinderValidationStatus<BEAN> implements Serializable { private final Binder<BEAN> binder; private final List<ValidationStatus<?>> bindingStatuses; - private final List<Result<?>> binderStatuses; + private final List<ValidationResult> binderStatuses; /** * Convenience method for creating a unresolved validation status for the @@ -90,7 +90,7 @@ public class BinderValidationStatus<BEAN> implements Serializable { */ public BinderValidationStatus(Binder<BEAN> source, List<ValidationStatus<?>> bindingStatuses, - List<Result<?>> binderStatuses) { + List<ValidationResult> binderStatuses) { Objects.requireNonNull(binderStatuses, "binding statuses cannot be null"); Objects.requireNonNull(binderStatuses, @@ -116,8 +116,8 @@ public class BinderValidationStatus<BEAN> implements Serializable { * passed */ public boolean hasErrors() { - return binderStatuses.stream().filter(Result::isError).findAny() - .isPresent() + return binderStatuses.stream().filter(ValidationResult::isError) + .findAny().isPresent() || bindingStatuses.stream().filter(ValidationStatus::isError) .findAny().isPresent(); } @@ -136,10 +136,11 @@ public class BinderValidationStatus<BEAN> implements Serializable { * * @return a list of all validation errors */ - public List<Result<?>> getValidationErrors() { - ArrayList<Result<?>> errors = new ArrayList<>(getFieldValidationErrors() - .stream().map(s -> s.getResult().get()) - .collect(Collectors.toList())); + public List<ValidationResult> getValidationErrors() { + ArrayList<ValidationResult> errors = new ArrayList<>( + getFieldValidationErrors().stream() + .map(s -> s.getResult().get()) + .collect(Collectors.toList())); errors.addAll(getBeanValidationErrors()); return errors; } @@ -166,7 +167,7 @@ public class BinderValidationStatus<BEAN> implements Serializable { * * @return the bean level validation results */ - public List<Result<?>> getBeanValidationResults() { + public List<ValidationResult> getBeanValidationResults() { return binderStatuses; } @@ -193,8 +194,8 @@ public class BinderValidationStatus<BEAN> implements Serializable { * * @return a list of failed bean level validation results */ - public List<Result<?>> getBeanValidationErrors() { - return binderStatuses.stream().filter(Result::isError) + public List<ValidationResult> getBeanValidationErrors() { + return binderStatuses.stream().filter(ValidationResult::isError) .collect(Collectors.toList()); } } diff --git a/server/src/main/java/com/vaadin/data/Result.java b/server/src/main/java/com/vaadin/data/Result.java index 7f63efa6a6..e2e7c61e84 100644 --- a/server/src/main/java/com/vaadin/data/Result.java +++ b/server/src/main/java/com/vaadin/data/Result.java @@ -24,9 +24,9 @@ import java.util.function.Function; import java.util.function.Supplier; /** - * Represents the result of an operation that might fail, such as input - * validation or type conversion. A result may contain either a value, - * signifying a successful operation, or an error message in case of a failure. + * Represents the result of an operation that might fail, such as type + * conversion. A result may contain either a value, signifying a successful + * operation, or an error message in case of a failure. * <p> * Result instances are created using the factory methods {@link #ok(R)} and * {@link #error(String)}, denoting success and failure respectively. diff --git a/server/src/main/java/com/vaadin/data/ValidationException.java b/server/src/main/java/com/vaadin/data/ValidationException.java index 093d88283a..0f86dd5186 100644 --- a/server/src/main/java/com/vaadin/data/ValidationException.java +++ b/server/src/main/java/com/vaadin/data/ValidationException.java @@ -34,7 +34,7 @@ import java.util.stream.Collectors; public class ValidationException extends Exception { private final List<ValidationStatus<?>> fieldValidationErrors; - private final List<Result<?>> beanValidationErrors; + private final List<ValidationResult> beanValidationErrors; /** * Constructs a new exception with validation {@code errors} list. @@ -45,7 +45,7 @@ public class ValidationException extends Exception { * binder validation errors list */ public ValidationException(List<ValidationStatus<?>> fieldValidationErrors, - List<Result<?>> beanValidationErrors) { + List<ValidationResult> beanValidationErrors) { super("Validation has failed for some fields"); this.fieldValidationErrors = Collections .unmodifiableList(fieldValidationErrors); @@ -58,10 +58,11 @@ public class ValidationException extends Exception { * * @return a list of all validation errors */ - public List<Result<?>> getValidationErrors() { - ArrayList<Result<?>> errors = new ArrayList<>(getFieldValidationErrors() - .stream().map(s -> s.getResult().get()) - .collect(Collectors.toList())); + public List<ValidationResult> getValidationErrors() { + ArrayList<ValidationResult> errors = new ArrayList<>( + getFieldValidationErrors().stream() + .map(s -> s.getResult().get()) + .collect(Collectors.toList())); errors.addAll(getBeanValidationErrors()); return errors; } @@ -84,7 +85,7 @@ public class ValidationException extends Exception { * * @return binder validation errors list */ - public List<Result<?>> getBeanValidationErrors() { + public List<ValidationResult> getBeanValidationErrors() { return beanValidationErrors; } } diff --git a/server/src/main/java/com/vaadin/data/ValidationResult.java b/server/src/main/java/com/vaadin/data/ValidationResult.java new file mode 100644 index 0000000000..489631005f --- /dev/null +++ b/server/src/main/java/com/vaadin/data/ValidationResult.java @@ -0,0 +1,103 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data; + +import java.io.Serializable; +import java.util.Objects; + +/** + * Represents the result of a validation. A result may be either successful or + * contain an error message in case of a failure. + * <p> + * ValidationResult instances are created using the factory methods + * {@link #ok()} and {@link #error(String)}, denoting success and failure + * respectively. + * + * @author Vaadin Ltd + * @since 8.0 + * + */ +public interface ValidationResult extends Serializable { + + class SimpleValidationResult implements ValidationResult { + + private final String error; + + SimpleValidationResult(String error) { + this.error = error; + } + + @Override + public String getErrorMessage() { + if (error == null) { + throw new IllegalStateException("The result is not an error. " + + "It cannot contain error message"); + } else { + return error; + } + } + + @Override + public boolean isError() { + return error != null; + } + + } + + /** + * Returns the result message. + * <p> + * Throws an {@link IllegalStateException} if the result represents success. + * + * @return the error message + * @throws IllegalStateException + * if the result represents success + */ + String getErrorMessage(); + + /** + * Checks if the result denotes an error. + * + * @return <code>true</code> if the result denotes an error, + * <code>false</code> otherwise + */ + boolean isError(); + + /** + * Returns a successful result. + * + * @return the successful result + */ + public static ValidationResult ok() { + return new SimpleValidationResult(null); + } + + /** + * Creates the validation result which represent an error with the given + * {@code errorMessage}. + * + * @param errorMessage + * error message, not {@code null} + * @return validation result which represent an error with the given + * {@code errorMessage} + * @throws NullPointerException + * if {@code errorMessage} is null + */ + public static ValidationResult error(String errorMessage) { + Objects.requireNonNull(errorMessage); + return new SimpleValidationResult(errorMessage); + } +} diff --git a/server/src/main/java/com/vaadin/data/ValidationStatus.java b/server/src/main/java/com/vaadin/data/ValidationStatus.java index f1f3da2d09..66e34eb6dd 100644 --- a/server/src/main/java/com/vaadin/data/ValidationStatus.java +++ b/server/src/main/java/com/vaadin/data/ValidationStatus.java @@ -22,8 +22,11 @@ import java.util.Optional; import com.vaadin.data.Binder.Binding; /** - * Represents the outcome of field level validation. Use - * {@link Binding#withValidationStatusHandler(ValidationStatusHandler)} to + * Represents the status of field validation. Status can be {@code Status.OK}, + * {@code Status.ERROR} or {@code Status.RESET}. Status OK and ERROR are always + * associated with a ValidationResult {@link #getResult}. + * <p> + * Use {@link Binding#withValidationStatusHandler(ValidationStatusHandler)} to * register a handler for field level validation status changes. * * @author Vaadin Ltd @@ -65,7 +68,7 @@ public class ValidationStatus<TARGET> implements Serializable { } private final Status status; - private final Result<TARGET> result; + private final ValidationResult result; private final Binding<?, ?, TARGET> binding; /** @@ -94,7 +97,7 @@ public class ValidationStatus<TARGET> implements Serializable { * the result of the validation */ public ValidationStatus(Binding<?, ?, TARGET> source, - Result<TARGET> result) { + ValidationResult result) { this(source, result.isError() ? Status.ERROR : Status.OK, result); } @@ -112,7 +115,7 @@ public class ValidationStatus<TARGET> implements Serializable { * the related result, may be {@code null} */ public ValidationStatus(Binding<?, ?, TARGET> source, Status status, - Result<TARGET> result) { + ValidationResult result) { Objects.requireNonNull(source, "Event source may not be null"); Objects.requireNonNull(status, "Status may not be null"); if (Objects.equals(status, Status.OK) && result.isError() @@ -154,7 +157,8 @@ public class ValidationStatus<TARGET> implements Serializable { * status is not an error */ public Optional<String> getMessage() { - return Optional.ofNullable(result).flatMap(Result::getMessage); + return Optional.ofNullable(result).filter(ValidationResult::isError) + .map(ValidationResult::getErrorMessage); } /** @@ -164,7 +168,7 @@ public class ValidationStatus<TARGET> implements Serializable { * * @return the validation result */ - public Optional<Result<TARGET>> getResult() { + public Optional<ValidationResult> getResult() { return Optional.ofNullable(result); } diff --git a/server/src/main/java/com/vaadin/data/Validator.java b/server/src/main/java/com/vaadin/data/Validator.java index 45a88bc7f3..33da20b945 100644 --- a/server/src/main/java/com/vaadin/data/Validator.java +++ b/server/src/main/java/com/vaadin/data/Validator.java @@ -34,9 +34,9 @@ import com.vaadin.server.SerializablePredicate; * <pre> * Validator<Integer> v = num -> { * if (num >= 0) - * return Result.ok(num); + * return ValidationResult.ok(); * else - * return Result.error("number must be positive"); + * return ValidationResult.error("number must be positive"); * }; * </pre> * @@ -45,15 +45,15 @@ import com.vaadin.server.SerializablePredicate; * @param <T> * the type of the value to validate * - * @see Result + * @see ValidationResult */ @FunctionalInterface public interface Validator<T> - extends BiFunction<T, ValueContext, Result<T>>, Serializable { + extends BiFunction<T, ValueContext, ValidationResult>, Serializable { /** - * Validates the given value. Returns a {@code Result} instance representing - * the outcome of the validation. + * Validates the given value. Returns a {@code ValidationResult} instance + * representing the outcome of the validation. * * @param value * the input value to validate @@ -62,7 +62,7 @@ public interface Validator<T> * @return the validation result */ @Override - public Result<T> apply(T value, ValueContext context); + public ValidationResult apply(T value, ValueContext context); /** * Returns a validator that passes any value. @@ -72,14 +72,15 @@ public interface Validator<T> * @return an always-passing validator */ public static <T> Validator<T> alwaysPass() { - return (v, ctx) -> Result.ok(v); + return (value, context) -> ValidationResult.ok(); } /** * Builds a validator out of a conditional function and an error message. If * the function returns true, the validator returns {@code Result.ok()}; if - * it returns false or throws an exception, {@code Result.error()} is - * returned with the given message. + * it returns false or throws an exception, + * {@link ValidationResult#error(String)} is returned with the given + * message. * <p> * For instance, the following validator checks if a number is between 0 and * 10, inclusive: @@ -126,12 +127,14 @@ public interface Validator<T> return (value, context) -> { try { if (guard.test(value)) { - return Result.ok(value); + return ValidationResult.ok(); } else { - return Result.error(errorMessageProvider.apply(context)); + return ValidationResult + .error(errorMessageProvider.apply(context)); } } catch (Exception e) { - return Result.error(errorMessageProvider.apply(context)); + return ValidationResult + .error(errorMessageProvider.apply(context)); } }; } 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()); } diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java index bb90ce53be..6e59d9bbe0 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java @@ -30,6 +30,7 @@ import java.util.logging.Logger; import org.jsoup.nodes.Element; import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; import com.vaadin.data.validator.DateRangeValidator; import com.vaadin.event.FieldEvents.BlurEvent; @@ -678,7 +679,7 @@ public abstract class AbstractDateField extends AbstractField<LocalDate> getDateOutOfRangeMessage(), getDate(getRangeStart(), getResolution()), getDate(getRangeEnd(), getResolution())); - Result<LocalDate> result = validator.apply(value, + ValidationResult result = validator.apply(value, new ValueContext(this)); if (result.isError()) { setComponentError(new UserError(getDateOutOfRangeMessage())); diff --git a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java index 9c0ffd31e7..8875cf3821 100644 --- a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java +++ b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java @@ -610,7 +610,7 @@ public class BinderBookOfVaadinTest { Assert.assertEquals(0, status.getFieldValidationErrors().size()); Assert.assertEquals(1, status.getBeanValidationErrors().size()); Assert.assertEquals( - status.getBeanValidationErrors().get(0).getMessage().get(), + status.getBeanValidationErrors().get(0).getErrorMessage(), message); Assert.assertEquals(message, formStatusLabel.getValue()); @@ -644,9 +644,10 @@ public class BinderBookOfVaadinTest { binder.setValidationStatusHandler(status -> { // create an error message on failed bean level validations - List<Result<?>> errors = status.getBeanValidationErrors(); - String errorMessage = errors.stream().map(Result::getMessage) - .map(o -> o.get()).collect(Collectors.joining("\n")); + List<ValidationResult> errors = status.getBeanValidationErrors(); + String errorMessage = errors.stream() + .map(ValidationResult::getErrorMessage) + .collect(Collectors.joining("\n")); // show error in a label formStatusLabel.setValue(errorMessage); formStatusLabel.setVisible(!errorMessage.isEmpty()); @@ -685,7 +686,7 @@ public class BinderBookOfVaadinTest { Assert.assertEquals(0, status.getFieldValidationErrors().size()); Assert.assertEquals(1, status.getBeanValidationErrors().size()); Assert.assertEquals(message, - status.getBeanValidationErrors().get(0).getMessage().get()); + status.getBeanValidationErrors().get(0).getErrorMessage()); Assert.assertEquals(message, formStatusLabel.getValue()); diff --git a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java index a0fc5cc6fa..0711ae24bf 100644 --- a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java @@ -93,8 +93,8 @@ public class BinderConverterValidatorTest String msg2 = "bar"; binding.withValidator(new Validator<String>() { @Override - public Result<String> apply(String value, ValueContext context) { - return new SimpleResult<>(null, msg1); + public ValidationResult apply(String value, ValueContext context) { + return ValidationResult.error(msg1); } }); binding.withValidator(value -> false, msg2); @@ -128,9 +128,9 @@ public class BinderConverterValidatorTest TextField salaryField = new TextField(); Validator<Number> positiveNumberValidator = (value, context) -> { if (value.doubleValue() >= 0) { - return Result.ok(value); + return ValidationResult.ok(); } else { - return Result.error(NEGATIVE_ERROR_MESSAGE); + return ValidationResult.error(NEGATIVE_ERROR_MESSAGE); } }; binder.forField(salaryField) diff --git a/server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java b/server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java index 9066bf4dbe..b884d85990 100644 --- a/server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java +++ b/server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java @@ -386,7 +386,7 @@ public class BinderValidationStatusTest Assert.assertEquals(1, status.getBeanValidationErrors().size()); Assert.assertEquals("Need first name and age", - status.getBeanValidationErrors().get(0).getMessage().get()); + status.getBeanValidationErrors().get(0).getErrorMessage()); } @Test diff --git a/server/src/test/java/com/vaadin/data/ValidatorTest.java b/server/src/test/java/com/vaadin/data/ValidatorTest.java index fa533c5010..9640550d95 100644 --- a/server/src/test/java/com/vaadin/data/ValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/ValidatorTest.java @@ -33,17 +33,15 @@ public class ValidatorTest extends ValidatorTestBase { @Test public void alwaysPass() { Validator<String> alwaysPass = Validator.alwaysPass(); - Result<String> result = alwaysPass.apply("foo", new ValueContext()); - Assert.assertTrue(result instanceof SimpleResult); - SimpleResult<String> implRes = (SimpleResult<String>) result; - Assert.assertFalse(implRes.getMessage().isPresent()); + ValidationResult result = alwaysPass.apply("foo", new ValueContext()); + Assert.assertFalse(result.isError()); } @Test public void from() { Validator<String> validator = Validator.from(Objects::nonNull, "Cannot be null"); - Result<String> result = validator.apply(null, new ValueContext()); + ValidationResult result = validator.apply(null, new ValueContext()); Assert.assertTrue(result.isError()); result = validator.apply("", new ValueContext()); diff --git a/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java b/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java index 85e43d2c65..ae1bb4fe17 100644 --- a/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import org.junit.Assert; import org.junit.Test; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; /** @@ -30,26 +30,25 @@ public class NotEmptyValidatorTest { @Test public void nullValueIsDisallowed() { NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo"); - Result<String> result = validator.apply(null, new ValueContext()); + ValidationResult result = validator.apply(null, new ValueContext()); Assert.assertTrue(result.isError()); - Assert.assertEquals("foo", result.getMessage().get()); + Assert.assertEquals("foo", result.getErrorMessage()); } @Test public void emptyValueIsDisallowed() { NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo"); - Result<String> result = validator.apply("", new ValueContext()); + ValidationResult result = validator.apply("", new ValueContext()); Assert.assertTrue(result.isError()); - Assert.assertEquals("foo", result.getMessage().get()); + Assert.assertEquals("foo", result.getErrorMessage()); } @Test public void nonNullValueIsAllowed() { NotEmptyValidator<Object> validator = new NotEmptyValidator<>("foo"); Object value = new Object(); - Result<Object> result = validator.apply(value, new ValueContext()); + ValidationResult result = validator.apply(value, new ValueContext()); + Assert.assertFalse(result.isError()); Assert.assertFalse(result.isError()); - result.ifOk(val -> Assert.assertEquals(value, val)); - result.ifError(msg -> Assert.fail()); } } diff --git a/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java b/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java index e3e76fbf53..24b95bf58a 100644 --- a/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java @@ -18,7 +18,7 @@ package com.vaadin.data.validator; import org.junit.Assert; import org.junit.Test; -import com.vaadin.data.Result; +import com.vaadin.data.ValidationResult; import com.vaadin.data.util.converter.ValueContext; public class NotNullValidatorTest { @@ -26,18 +26,17 @@ public class NotNullValidatorTest { @Test public void nullValueIsDisallowed() { NotNullValidator validator = new NotNullValidator("foo"); - Result<String> result = validator.apply(null, new ValueContext()); + ValidationResult result = validator.apply(null, new ValueContext()); Assert.assertTrue(result.isError()); - Assert.assertEquals("foo", result.getMessage().get()); + Assert.assertEquals("foo", result.getErrorMessage()); } @Test public void nonNullValueIsAllowed() { NotNullValidator validator = new NotNullValidator("foo"); - Result<String> result = validator.apply("bar", new ValueContext()); + ValidationResult result = validator.apply("bar", new ValueContext()); + Assert.assertFalse(result.isError()); Assert.assertFalse(result.isError()); - result.ifOk(value -> Assert.assertEquals("bar", value)); - result.ifError(msg -> Assert.fail()); } } diff --git a/server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java b/server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java index 5a7198f33c..c8432324ad 100644 --- a/server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java +++ b/server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java @@ -5,6 +5,7 @@ import java.util.Locale; import org.junit.Assert; import org.junit.Before; +import com.vaadin.data.ValidationResult; import com.vaadin.data.Validator; import com.vaadin.data.util.converter.ValueContext; import com.vaadin.ui.Label; @@ -18,17 +19,20 @@ public class ValidatorTestBase { localeContext = new Label(); } - protected <T> void assertPasses(T value, Validator<? super T> v) { - v.apply(value, new ValueContext()) - .handle(val -> Assert.assertEquals(value, val), err -> Assert - .fail(value + " should pass " + v + " but got " + err)); + protected <T> void assertPasses(T value, Validator<? super T> validator) { + ValidationResult result = validator.apply(value, new ValueContext()); + if (result.isError()) { + Assert.fail(value + " should pass " + validator + " but got " + + result.getErrorMessage()); + } } protected <T> void assertFails(T value, String errorMessage, - Validator<? super T> v) { - v.apply(value, new ValueContext(localeContext)).handle( - val -> Assert.fail(value + " should fail " + v), - err -> Assert.assertEquals(errorMessage, err)); + Validator<? super T> validator) { + ValidationResult result = validator.apply(value, + new ValueContext(localeContext)); + Assert.assertTrue(result.isError()); + Assert.assertEquals(errorMessage, result.getErrorMessage()); } protected <T> void assertFails(T value, AbstractValidator<? super T> v) { |