diff options
Diffstat (limited to 'src/main/java/org/pacien/lemonad')
-rw-r--r-- | src/main/java/org/pacien/lemonad/validation/Validation.java (renamed from src/main/java/org/pacien/lemonad/validation/ValidationResult.java) | 34 | ||||
-rw-r--r-- | src/main/java/org/pacien/lemonad/validation/ValidationContainer.java (renamed from src/main/java/org/pacien/lemonad/validation/ValidationResultContainer.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/pacien/lemonad/validation/Validator.java | 10 |
3 files changed, 23 insertions, 23 deletions
diff --git a/src/main/java/org/pacien/lemonad/validation/ValidationResult.java b/src/main/java/org/pacien/lemonad/validation/Validation.java index 65bb389..04c1ed0 100644 --- a/src/main/java/org/pacien/lemonad/validation/ValidationResult.java +++ b/src/main/java/org/pacien/lemonad/validation/Validation.java | |||
@@ -41,7 +41,7 @@ import static org.pacien.lemonad.attempt.Attempt.success; | |||
41 | * @param <E> the error type. | 41 | * @param <E> the error type. |
42 | * @author pacien | 42 | * @author pacien |
43 | */ | 43 | */ |
44 | public interface ValidationResult<S, E> { | 44 | public interface Validation<S, E> { |
45 | /** | 45 | /** |
46 | * @return whether no error have been reported during the validation. | 46 | * @return whether no error have been reported during the validation. |
47 | */ | 47 | */ |
@@ -66,7 +66,7 @@ public interface ValidationResult<S, E> { | |||
66 | * @param consumer a subject consumer called if the validation is successful. | 66 | * @param consumer a subject consumer called if the validation is successful. |
67 | * @return the current object. | 67 | * @return the current object. |
68 | */ | 68 | */ |
69 | default ValidationResult<S, E> ifValid(@NonNull Consumer<? super S> consumer) { | 69 | default Validation<S, E> ifValid(@NonNull Consumer<? super S> consumer) { |
70 | if (isValid()) consumer.accept(getSubject()); | 70 | if (isValid()) consumer.accept(getSubject()); |
71 | return this; | 71 | return this; |
72 | } | 72 | } |
@@ -75,7 +75,7 @@ public interface ValidationResult<S, E> { | |||
75 | * @param consumer the consumer called with the validation subject and reported errors if the validation is failed. | 75 | * @param consumer the consumer called with the validation subject and reported errors if the validation is failed. |
76 | * @return the current object. | 76 | * @return the current object. |
77 | */ | 77 | */ |
78 | default ValidationResult<S, E> ifInvalid(@NonNull BiConsumer<? super S, ? super List<? super E>> consumer) { | 78 | default Validation<S, E> ifInvalid(@NonNull BiConsumer<? super S, ? super List<? super E>> consumer) { |
79 | if (!isValid()) consumer.accept(getSubject(), getErrors()); | 79 | if (!isValid()) consumer.accept(getSubject(), getErrors()); |
80 | return this; | 80 | return this; |
81 | } | 81 | } |
@@ -88,41 +88,41 @@ public interface ValidationResult<S, E> { | |||
88 | } | 88 | } |
89 | 89 | ||
90 | /** | 90 | /** |
91 | * @param mapper a function transforming a {@link ValidationResult}. | 91 | * @param mapper a function transforming a {@link Validation}. |
92 | * @return the transformed {@link ValidationResult}. | 92 | * @return the transformed {@link Validation}. |
93 | */ | 93 | */ |
94 | default <SS, EE> ValidationResult<SS, EE> flatMap(@NonNull Function<? super ValidationResult<? super S, ? super E>, ? extends ValidationResult<? extends SS, ? extends EE>> mapper) { | 94 | default <SS, EE> Validation<SS, EE> flatMap(@NonNull Function<? super Validation<? super S, ? super E>, ? extends Validation<? extends SS, ? extends EE>> mapper) { |
95 | //noinspection unchecked | 95 | //noinspection unchecked |
96 | return (ValidationResult<SS, EE>) mapper.apply(this); | 96 | return (Validation<SS, EE>) mapper.apply(this); |
97 | } | 97 | } |
98 | 98 | ||
99 | /** | 99 | /** |
100 | * @param subject an overriding subject. | 100 | * @param subject an overriding subject. |
101 | * @param validationResults a {@link Stream} of {@link ValidationResult}s to merge. | 101 | * @param validationResults a {@link Stream} of {@link Validation}s to merge. |
102 | * @return the merged {@link ValidationResult} containing all errors from the supplied ones. | 102 | * @return the merged {@link Validation} containing all errors from the supplied ones. |
103 | */ | 103 | */ |
104 | static <S, E> ValidationResult<S, E> merge(S subject, @NonNull Stream<? extends ValidationResult<?, ? extends E>> validationResults) { | 104 | static <S, E> Validation<S, E> merge(S subject, @NonNull Stream<? extends Validation<?, ? extends E>> validationResults) { |
105 | return new ValidationResultContainer<>( | 105 | return new ValidationContainer<>( |
106 | subject, | 106 | subject, |
107 | validationResults.flatMap(res -> res.getErrors().stream()).collect(toUnmodifiableList())); | 107 | validationResults.flatMap(res -> res.getErrors().stream()).collect(toUnmodifiableList())); |
108 | } | 108 | } |
109 | 109 | ||
110 | /** | 110 | /** |
111 | * @param subject the suject of the validation. | 111 | * @param subject the suject of the validation. |
112 | * @return a successful {@link ValidationResult}. | 112 | * @return a successful {@link Validation}. |
113 | */ | 113 | */ |
114 | static <S, E> ValidationResult<S, E> valid(S subject) { | 114 | static <S, E> Validation<S, E> valid(S subject) { |
115 | return new ValidationResultContainer<>(subject, List.of()); | 115 | return new ValidationContainer<>(subject, List.of()); |
116 | } | 116 | } |
117 | 117 | ||
118 | /** | 118 | /** |
119 | * @param subject the suject of the validation. | 119 | * @param subject the suject of the validation. |
120 | * @param error a validation error. | 120 | * @param error a validation error. |
121 | * @param errors additional validation errors. | 121 | * @param errors additional validation errors. |
122 | * @return a failed {@link ValidationResult} for the supplied subject. | 122 | * @return a failed {@link Validation} for the supplied subject. |
123 | */ | 123 | */ |
124 | @SafeVarargs static <S, E> ValidationResult<S, E> invalid(S subject, E error, E... errors) { | 124 | @SafeVarargs static <S, E> Validation<S, E> invalid(S subject, E error, E... errors) { |
125 | return new ValidationResultContainer<>( | 125 | return new ValidationContainer<>( |
126 | subject, | 126 | subject, |
127 | Stream.concat(Stream.of(error), Arrays.stream(errors)).map(Objects::requireNonNull).collect(toUnmodifiableList())); | 127 | Stream.concat(Stream.of(error), Arrays.stream(errors)).map(Objects::requireNonNull).collect(toUnmodifiableList())); |
128 | } | 128 | } |
diff --git a/src/main/java/org/pacien/lemonad/validation/ValidationResultContainer.java b/src/main/java/org/pacien/lemonad/validation/ValidationContainer.java index 2c752f6..03f77be 100644 --- a/src/main/java/org/pacien/lemonad/validation/ValidationResultContainer.java +++ b/src/main/java/org/pacien/lemonad/validation/ValidationContainer.java | |||
@@ -26,7 +26,7 @@ import lombok.Value; | |||
26 | /** | 26 | /** |
27 | * @author pacien | 27 | * @author pacien |
28 | */ | 28 | */ |
29 | @Value class ValidationResultContainer<S, E> implements ValidationResult<S, E> { | 29 | @Value class ValidationContainer<S, E> implements Validation<S, E> { |
30 | S subject; | 30 | S subject; |
31 | @NonNull List<E> errors; | 31 | @NonNull List<E> errors; |
32 | 32 | ||
diff --git a/src/main/java/org/pacien/lemonad/validation/Validator.java b/src/main/java/org/pacien/lemonad/validation/Validator.java index a8a9e3f..e04cde8 100644 --- a/src/main/java/org/pacien/lemonad/validation/Validator.java +++ b/src/main/java/org/pacien/lemonad/validation/Validator.java | |||
@@ -28,8 +28,8 @@ import lombok.NonNull; | |||
28 | import lombok.val; | 28 | import lombok.val; |
29 | 29 | ||
30 | import static java.util.stream.Collectors.toUnmodifiableList; | 30 | import static java.util.stream.Collectors.toUnmodifiableList; |
31 | import static org.pacien.lemonad.validation.ValidationResult.invalid; | 31 | import static org.pacien.lemonad.validation.Validation.invalid; |
32 | import static org.pacien.lemonad.validation.ValidationResult.valid; | 32 | import static org.pacien.lemonad.validation.Validation.valid; |
33 | 33 | ||
34 | /** | 34 | /** |
35 | * A function which applies validation rules on a subject and reports possible errors. | 35 | * A function which applies validation rules on a subject and reports possible errors. |
@@ -43,7 +43,7 @@ import static org.pacien.lemonad.validation.ValidationResult.valid; | |||
43 | * @param subject the subject to validate, which can potentially be null. | 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. | 44 | * @return the non-null result of the validation of the supplied subject. |
45 | */ | 45 | */ |
46 | ValidationResult<S, E> validate(S subject); | 46 | Validation<S, E> validate(S subject); |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * @param predicate the validation predicate testing the validity of a subject. | 49 | * @param predicate the validation predicate testing the validity of a subject. |
@@ -60,7 +60,7 @@ import static org.pacien.lemonad.validation.ValidationResult.valid; | |||
60 | */ | 60 | */ |
61 | @SafeVarargs static <S, E> Validator<S, E> validatingAll(@NonNull Validator<? super S, ? extends E>... validators) { | 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()); | 62 | val validatorList = Arrays.stream(validators).map(Objects::requireNonNull).collect(toUnmodifiableList()); |
63 | return subject -> new ValidationResultContainer<>( | 63 | return subject -> new ValidationContainer<>( |
64 | subject, | 64 | subject, |
65 | validatorList.stream() | 65 | validatorList.stream() |
66 | .flatMap(validator -> validator.validate(subject).getErrors().stream()) | 66 | .flatMap(validator -> validator.validate(subject).getErrors().stream()) |
@@ -75,6 +75,6 @@ import static org.pacien.lemonad.validation.ValidationResult.valid; | |||
75 | static <S, F, E> Validator<S, E> validatingField(@NonNull Function<? super S, ? extends F> getter, | 75 | static <S, F, E> Validator<S, E> validatingField(@NonNull Function<? super S, ? extends F> getter, |
76 | @NonNull Validator<? super F, ? extends E> validator) { | 76 | @NonNull Validator<? super F, ? extends E> validator) { |
77 | //noinspection unchecked | 77 | //noinspection unchecked |
78 | return subject -> new ValidationResultContainer<>(subject, (List<E>) validator.validate(getter.apply(subject)).getErrors()); | 78 | return subject -> new ValidationContainer<>(subject, (List<E>) validator.validate(getter.apply(subject)).getErrors()); |
79 | } | 79 | } |
80 | } | 80 | } |