]> source.dussan.org Git - vaadin-framework.git/commitdiff
Introduces ValidationResult interface to replace Result in validators.
authorDenis Anisimov <denis@vaadin.com>
Tue, 1 Nov 2016 09:38:17 +0000 (11:38 +0200)
committerDenis Anisimov <denis@vaadin.com>
Tue, 1 Nov 2016 15:14:19 +0000 (17:14 +0200)
Fixes vaadin/framework8-issues#414

Change-Id: I2d4e101f29542fadfdd30ce63455a0e078e0e735

22 files changed:
server/src/main/java/com/vaadin/data/Binder.java
server/src/main/java/com/vaadin/data/BinderValidationStatus.java
server/src/main/java/com/vaadin/data/Result.java
server/src/main/java/com/vaadin/data/ValidationException.java
server/src/main/java/com/vaadin/data/ValidationResult.java [new file with mode: 0644]
server/src/main/java/com/vaadin/data/ValidationStatus.java
server/src/main/java/com/vaadin/data/Validator.java
server/src/main/java/com/vaadin/data/validator/AbstractValidator.java
server/src/main/java/com/vaadin/data/validator/BeanValidator.java
server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java
server/src/main/java/com/vaadin/data/validator/NotNullValidator.java
server/src/main/java/com/vaadin/data/validator/RangeValidator.java
server/src/main/java/com/vaadin/data/validator/RegexpValidator.java
server/src/main/java/com/vaadin/data/validator/StringLengthValidator.java
server/src/main/java/com/vaadin/ui/AbstractDateField.java
server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java
server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java
server/src/test/java/com/vaadin/data/BinderValidationStatusTest.java
server/src/test/java/com/vaadin/data/ValidatorTest.java
server/src/test/java/com/vaadin/data/validator/NotEmptyValidatorTest.java
server/src/test/java/com/vaadin/data/validator/NotNullValidatorTest.java
server/src/test/java/com/vaadin/data/validator/ValidatorTestBase.java

index e139bcd5c8af31fa31e2158ae65767db4922e32f..f8ddace8a636b2e7dbada9e817294d9d2255f77a 100644 (file)
@@ -619,6 +619,26 @@ public class Binder<BEAN> implements Serializable {
             return status;
         }
 
+        /**
+         * 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.
@@ -626,10 +646,7 @@ public class Binder<BEAN> implements Serializable {
          * @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);
         }
index 9b7e40f516c101243c1930cebec7227ab2d6e63c..41a181dacf2b2735bb01a3dcb82546d8824aa45c 100644 (file)
@@ -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());
     }
 }
index 7f63efa6a6ef32237d997d2dcb1e7bd9ec09d93e..e2e7c61e8439344f41a72a62c13492b6b89b1560 100644 (file)
@@ -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.
index 093d88283a3fd9fa740d5f59b27b470c49bbee49..0f86dd5186221449c6af68e4b060b486cc5bdd00 100644 (file)
@@ -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 (file)
index 0000000..4896310
--- /dev/null
@@ -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);
+    }
+}
index f1f3da2d09aa5714c4b79dd9ab3a67cabc3b7db2..66e34eb6dd6f7661135c4c3c5ffb46dbd5d5b393 100644 (file)
@@ -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);
     }
 
index 45a88bc7f3d106472db788798ba4755d7bf2d7ec..33da20b945e19a43a8ad49e48a17d70027d685dc 100644 (file)
@@ -34,9 +34,9 @@ import com.vaadin.server.SerializablePredicate;
  * <pre>
  * Validator&lt;Integer&gt; 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));
             }
         };
     }
index b798d2bf73fd1bd892e3632a26394dc3d44ec8cc..793797ee3f1fce33cb5396cedb4be3609ca078d0 100644 (file)
@@ -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));
     }
 }
index 53886c98407830a969ad9769a2e6680703634aec..a3f9b86a6eaef1dcbd854b9a5c9e421637bffb02 100644 (file)
@@ -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
index b03353894936925fc9202f8fab9c8cecc9c02ea6..2223c333cb9c18bca647ceee138586ad05fd6500 100644 (file)
@@ -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();
         }
     }
 
index 349e000c68d5359993b5c01f20853bf51c41c810..c92aefac3b9f4ac66133c25daf57747138c644d6 100644 (file)
@@ -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();
     }
 
 }
index 32effdd8116a6650f938d122df2533ee2d581c45..b8ad38730807313fc48a671de075f4ab96c8b781 100644 (file)
@@ -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));
     }
 
index b9763e5199ebdde4a905126eb2306281af8ff482..89e4d81a534feb46d04c63237fc9bc1427f1b5d9 100644 (file)
@@ -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));
     }
 
index 421cf13a2993b9f6f79001cf15ad45e141f8d569..b3e1da18eb51098406069d51223d8aa4eb556b1e 100644 (file)
@@ -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());
     }
 
index bb90ce53be58f7a327be08dccd04e30cc659b3aa..6e59d9bbe0cf5652b30fa2268221c95bf845f866 100644 (file)
@@ -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()));
index 9c0ffd31e7e224592fa88875871f26cfc58d873a..8875cf38210c11eaca56e9dfabcfd683b972fd8a 100644 (file)
@@ -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());
 
index a0fc5cc6fa5d8dcf1d3f0411d7bb9bed9ced19a0..0711ae24bf5a2bc14576f85de159546935805b68 100644 (file)
@@ -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)
index 9066bf4dbeacb679c145ebf53fe051f7ef0a0c3e..b884d859908421e2875064566649bb31a21fc5e9 100644 (file)
@@ -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
index fa533c5010696fcefb12255c932214533b26b4a8..9640550d9543367e75d5ffb1c7456442a577396f 100644 (file)
@@ -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());
index 85e43d2c65a9e35679c0f04ba4c1ea26134be6cd..ae1bb4fe17c9fab446727c3f728d12eb9d94735c 100644 (file)
@@ -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());
     }
 }
index e3e76fbf536945314de6a19d5410d4aa80779044..24b95bf58a8d0e49db9b1088aa7e0c0177e27196 100644 (file)
@@ -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());
     }
 
 }
index 5a7198f33c4c132038d361236a0aef70f84e1ed1..c8432324ad180dff3b1685f5b0500a63d13fb044 100644 (file)
@@ -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) {