aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/pacien/lemonad/validation/Validator.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/pacien/lemonad/validation/Validator.java')
-rw-r--r--src/main/java/org/pacien/lemonad/validation/Validator.java80
1 files changed, 0 insertions, 80 deletions
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 @@
1/*
2 * lemonad - Some functional sweetness for Java
3 * Copyright (C) 2019 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.lemonad.validation;
20
21import java.util.Arrays;
22import java.util.List;
23import java.util.Objects;
24import java.util.function.Function;
25import java.util.function.Predicate;
26
27import lombok.NonNull;
28import lombok.val;
29
30import static java.util.stream.Collectors.toUnmodifiableList;
31import static org.pacien.lemonad.validation.Validation.invalid;
32import static org.pacien.lemonad.validation.Validation.valid;
33
34/**
35 * A function which applies validation rules on a subject and reports possible errors.
36 *
37 * @param <S> the subject type
38 * @param <E> the error type
39 * @author pacien
40 */
41@FunctionalInterface public interface Validator<S, E> {
42 /**
43 * @param subject the subject to validate, which can potentially be null.
44 * @return the non-null result of the validation of the supplied subject.
45 */
46 Validation<S, E> validate(S subject);
47
48 /**
49 * @param predicate the validation predicate testing the validity of a subject.
50 * @param negativeError an error to return if the subject does not pass the test.
51 * @return a {@link Validator} based on the supplied predicate and error.
52 */
53 static <S, E> Validator<S, E> ensuringPredicate(@NonNull Predicate<? super S> predicate, @NonNull E negativeError) {
54 return subject -> predicate.test(subject) ? valid(subject) : invalid(subject, negativeError);
55 }
56
57 /**
58 * @param validators the {@link Validator}s to combine, to be evaluated in order of listing.
59 * @return a {@link Validator} based on the supplied ones.
60 */
61 @SafeVarargs static <S, E> Validator<S, E> validatingAll(@NonNull Validator<? super S, ? extends E>... validators) {
62 val validatorList = Arrays.stream(validators).map(Objects::requireNonNull).collect(toUnmodifiableList());
63 return subject -> new ValidationContainer<>(
64 subject,
65 validatorList.stream()
66 .flatMap(validator -> validator.validate(subject).getErrors().stream())
67 .collect(toUnmodifiableList()));
68 }
69
70 /**
71 * @param getter the field getter mapping the validation subject.
72 * @param validator the {@link Validator} validating the field.
73 * @return a {@link Validator} validating the parent object.
74 */
75 static <S, F, E> Validator<S, E> validatingField(@NonNull Function<? super S, ? extends F> getter,
76 @NonNull Validator<? super F, ? extends E> validator) {
77 //noinspection unchecked
78 return subject -> new ValidationContainer<>(subject, (List<E>) validator.validate(getter.apply(subject)).getErrors());
79 }
80}