From 7ded9823fb54b81cbd73a133fd1e1fe036f31b04 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 13 Apr 2019 21:24:40 +0200 Subject: refactor validation --- .../org/pacien/lemonad/validation/Validation.java | 133 ++++++++++++++++----- .../lemonad/validation/ValidationContainer.java | 6 + .../org/pacien/lemonad/validation/Validator.java | 80 ------------- 3 files changed, 110 insertions(+), 109 deletions(-) delete mode 100644 src/main/java/org/pacien/lemonad/validation/Validator.java (limited to 'src/main/java/org/pacien') diff --git a/src/main/java/org/pacien/lemonad/validation/Validation.java b/src/main/java/org/pacien/lemonad/validation/Validation.java index 04c1ed0..98a4496 100644 --- a/src/main/java/org/pacien/lemonad/validation/Validation.java +++ b/src/main/java/org/pacien/lemonad/validation/Validation.java @@ -20,17 +20,17 @@ package org.pacien.lemonad.validation; import org.pacien.lemonad.attempt.Attempt; -import java.util.Arrays; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; -import java.util.Objects; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; -import java.util.stream.Stream; +import java.util.function.Predicate; import lombok.NonNull; -import static java.util.stream.Collectors.toUnmodifiableList; +import static java.util.function.Function.identity; import static org.pacien.lemonad.attempt.Attempt.failure; import static org.pacien.lemonad.attempt.Attempt.success; @@ -72,58 +72,133 @@ public interface Validation { } /** - * @param consumer the consumer called with the validation subject and reported errors if the validation is failed. + * @param consumer the consumer called with the validation subject and reported errors if the validation has failed. * @return the current object. */ default Validation ifInvalid(@NonNull BiConsumer> consumer) { - if (!isValid()) consumer.accept(getSubject(), getErrors()); + if (isInvalid()) consumer.accept(getSubject(), getErrors()); return this; } /** - * @return an {@link Attempt} with a state corresponding to the one of the validation. + * @param predicate the validation predicate testing the validity of a subject. + * @param error the error to return if the subject does not pass the test. + * @return an updated {@link Validation}. */ - default Attempt> toAttempt() { - return isValid() ? success(getSubject()) : failure(getErrors()); + default Validation validate(@NonNull Predicate predicate, @NonNull E error) { + return validate(identity(), predicate, error); + } + + /** + * @param mapper the field getter mapping the validation subject. + * @param predicate the validation predicate testing the validity of a subject. + * @param error the error to return if the subject does not pass the test. + * @return an updated {@link Validation}. + */ + default Validation validate( + @NonNull Function mapper, + @NonNull Predicate predicate, + E error + ) { + return validate(mapper, field -> predicate.test(field) ? List.of() : List.of(error)); + } + + /** + * @param validator the validating function to use, returning a potentially empty list of errors. + * @return an updated {@link Validation}. + */ + default Validation validate(@NonNull Function> validator) { + var errors = validator.apply(getSubject()); + return errors.isEmpty() ? this : merge(errors); + } + + /** + * @param mapper the field getter mapping the validation subject. + * @param validator the validating function to use, returning a potentially empty list of errors. + * @return an updated {@link Validation}. + */ + default Validation validate( + @NonNull Function mapper, + @NonNull Function> validator + ) { + return validate(validator.compose(mapper)); + } + + /** + * @param validator a subject validating function returning a {@link Validation}. + * @return an updated {@link Validation}. + */ + default Validation merge(@NonNull Function> validator) { + return merge(validator.apply(getSubject())); + } + + /** + * @param mapper the field getter mapping the validation subject. + * @param validator a subject validating function returning a {@link Validation}. + * @return an updated {@link Validation}. + */ + default Validation merge( + @NonNull Function mapper, + @NonNull Function> validator + ) { + return merge(validator.compose(mapper)); + } + + /** + * @param validation another validation to merge into the current one. + * @return an updated {@link Validation}. + */ + @SuppressWarnings("unchecked") + default Validation merge(@NonNull Validation validation) { + if (validation.isValid()) return this; + if (this.isValid()) return Validation.of(this.getSubject(), (List) validation.getErrors()); + return merge(validation.getErrors()); + } + + /** + * @param errors a potentially empty list of additional errors to take into account. + * @return an updated {@link Validation}. + */ + default Validation merge(@NonNull Collection errors) { + var combinedErrors = new ArrayList(getErrors().size() + errors.size()); + combinedErrors.addAll(getErrors()); + combinedErrors.addAll(errors); + return new ValidationContainer<>(getSubject(), combinedErrors); } /** * @param mapper a function transforming a {@link Validation}. * @return the transformed {@link Validation}. */ - default Validation flatMap(@NonNull Function, ? extends Validation> mapper) { + default Validation flatMap( + @NonNull Function, ? extends Validation> mapper + ) { //noinspection unchecked return (Validation) mapper.apply(this); } /** - * @param subject an overriding subject. - * @param validationResults a {@link Stream} of {@link Validation}s to merge. - * @return the merged {@link Validation} containing all errors from the supplied ones. + * @return an {@link Attempt} with a state corresponding to the one of the validation. */ - static Validation merge(S subject, @NonNull Stream> validationResults) { - return new ValidationContainer<>( - subject, - validationResults.flatMap(res -> res.getErrors().stream()).collect(toUnmodifiableList())); + default Attempt> toAttempt() { + return isValid() ? success(getSubject()) : failure(getErrors()); } /** - * @param subject the suject of the validation. - * @return a successful {@link Validation}. + * @param subject the subject of the validation. + * @param errors some optional validation errors. + * @return a {@link Validation}. */ - static Validation valid(S subject) { - return new ValidationContainer<>(subject, List.of()); + @SafeVarargs static Validation of(S subject, E... errors) { + return Validation.of(subject, List.of(errors)); } /** - * @param subject the suject of the validation. - * @param error a validation error. - * @param errors additional validation errors. - * @return a failed {@link Validation} for the supplied subject. + * @param subject the subject of the validation. + * @param errors some optional validation errors. + * @return a {@link Validation}. */ - @SafeVarargs static Validation invalid(S subject, E error, E... errors) { - return new ValidationContainer<>( - subject, - Stream.concat(Stream.of(error), Arrays.stream(errors)).map(Objects::requireNonNull).collect(toUnmodifiableList())); + static Validation of(S subject, @NonNull List errors) { + return new ValidationContainer<>(subject, errors); } } diff --git a/src/main/java/org/pacien/lemonad/validation/ValidationContainer.java b/src/main/java/org/pacien/lemonad/validation/ValidationContainer.java index 03f77be..a9a1614 100644 --- a/src/main/java/org/pacien/lemonad/validation/ValidationContainer.java +++ b/src/main/java/org/pacien/lemonad/validation/ValidationContainer.java @@ -23,6 +23,8 @@ import java.util.List; import lombok.NonNull; import lombok.Value; +import static java.util.Collections.unmodifiableList; + /** * @author pacien */ @@ -37,4 +39,8 @@ import lombok.Value; @Override public boolean isInvalid() { return !isValid(); } + + @Override public List getErrors() { + return unmodifiableList(errors); + } } diff --git a/src/main/java/org/pacien/lemonad/validation/Validator.java b/src/main/java/org/pacien/lemonad/validation/Validator.java deleted file mode 100644 index e04cde8..0000000 --- a/src/main/java/org/pacien/lemonad/validation/Validator.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * lemonad - Some functional sweetness for Java - * Copyright (C) 2019 Pacien TRAN-GIRARD - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package org.pacien.lemonad.validation; - -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.function.Function; -import java.util.function.Predicate; - -import lombok.NonNull; -import lombok.val; - -import static java.util.stream.Collectors.toUnmodifiableList; -import static org.pacien.lemonad.validation.Validation.invalid; -import static org.pacien.lemonad.validation.Validation.valid; - -/** - * A function which applies validation rules on a subject and reports possible errors. - * - * @param the subject type - * @param the error type - * @author pacien - */ -@FunctionalInterface public interface Validator { - /** - * @param subject the subject to validate, which can potentially be null. - * @return the non-null result of the validation of the supplied subject. - */ - Validation validate(S subject); - - /** - * @param predicate the validation predicate testing the validity of a subject. - * @param negativeError an error to return if the subject does not pass the test. - * @return a {@link Validator} based on the supplied predicate and error. - */ - static Validator ensuringPredicate(@NonNull Predicate predicate, @NonNull E negativeError) { - return subject -> predicate.test(subject) ? valid(subject) : invalid(subject, negativeError); - } - - /** - * @param validators the {@link Validator}s to combine, to be evaluated in order of listing. - * @return a {@link Validator} based on the supplied ones. - */ - @SafeVarargs static Validator validatingAll(@NonNull Validator... validators) { - val validatorList = Arrays.stream(validators).map(Objects::requireNonNull).collect(toUnmodifiableList()); - return subject -> new ValidationContainer<>( - subject, - validatorList.stream() - .flatMap(validator -> validator.validate(subject).getErrors().stream()) - .collect(toUnmodifiableList())); - } - - /** - * @param getter the field getter mapping the validation subject. - * @param validator the {@link Validator} validating the field. - * @return a {@link Validator} validating the parent object. - */ - static Validator validatingField(@NonNull Function getter, - @NonNull Validator validator) { - //noinspection unchecked - return subject -> new ValidationContainer<>(subject, (List) validator.validate(getter.apply(subject)).getErrors()); - } -} -- cgit v1.2.3