diff options
author | pacien | 2019-03-29 01:31:15 +0100 |
---|---|---|
committer | pacien | 2019-03-29 01:31:15 +0100 |
commit | b6869c1b17c65594f65c3ad5e53461082e5c3088 (patch) | |
tree | 1b924d802852efbbc0519210fb947b095ddf48b4 /src/main/java/org/pacien/lemonad/validation/Validator.java | |
parent | eadf4a9f3c7d539f1af08ee37928ad0f80f4e4d3 (diff) | |
download | java-lemonad-b6869c1b17c65594f65c3ad5e53461082e5c3088.tar.gz |
initial impl
Diffstat (limited to 'src/main/java/org/pacien/lemonad/validation/Validator.java')
-rw-r--r-- | src/main/java/org/pacien/lemonad/validation/Validator.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/main/java/org/pacien/lemonad/validation/Validator.java b/src/main/java/org/pacien/lemonad/validation/Validator.java new file mode 100644 index 0000000..a8a9e3f --- /dev/null +++ b/src/main/java/org/pacien/lemonad/validation/Validator.java | |||
@@ -0,0 +1,80 @@ | |||
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 | |||
19 | package org.pacien.lemonad.validation; | ||
20 | |||
21 | import java.util.Arrays; | ||
22 | import java.util.List; | ||
23 | import java.util.Objects; | ||
24 | import java.util.function.Function; | ||
25 | import java.util.function.Predicate; | ||
26 | |||
27 | import lombok.NonNull; | ||
28 | import lombok.val; | ||
29 | |||
30 | import static java.util.stream.Collectors.toUnmodifiableList; | ||
31 | import static org.pacien.lemonad.validation.ValidationResult.invalid; | ||
32 | import static org.pacien.lemonad.validation.ValidationResult.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 | ValidationResult<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 ValidationResultContainer<>( | ||
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 ValidationResultContainer<>(subject, (List<E>) validator.validate(getter.apply(subject)).getErrors()); | ||
79 | } | ||
80 | } | ||