aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.gradle35
-rw-r--r--settings.gradle19
-rw-r--r--src/main/java/org/pacien/lemonad/attempt/Attempt.java129
-rw-r--r--src/main/java/org/pacien/lemonad/attempt/Failure.java42
-rw-r--r--src/main/java/org/pacien/lemonad/attempt/Success.java42
-rw-r--r--src/main/java/org/pacien/lemonad/attempt/ThrowingSupplier.java32
-rw-r--r--src/main/java/org/pacien/lemonad/validation/ValidationResult.java129
-rw-r--r--src/main/java/org/pacien/lemonad/validation/ValidationResultContainer.java40
-rw-r--r--src/main/java/org/pacien/lemonad/validation/Validator.java80
-rw-r--r--src/test/java/org/pacien/lemonad/attempt/AttemptTest.java114
-rw-r--r--src/test/java/org/pacien/lemonad/validation/ValidationResultTest.java75
-rw-r--r--src/test/java/org/pacien/lemonad/validation/ValidatorTest.java61
12 files changed, 793 insertions, 5 deletions
diff --git a/build.gradle b/build.gradle
index fb83c0a..b4b5242 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,16 +1,43 @@
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
1plugins { 19plugins {
2 id 'java' 20 id 'java'
3} 21}
4 22
5group 'org.pacien' 23group 'org.pacien'
6version '1.0-SNAPSHOT' 24version '1.0-SNAPSHOT'
7 25
8sourceCompatibility = 1.8 26sourceCompatibility = 1.11
27
28test {
29 useJUnitPlatform()
30}
9 31
10repositories { 32repositories {
11 mavenCentral() 33 mavenCentral()
12} 34}
13 35
14dependencies { 36dependencies {
15 testCompile group: 'junit', name: 'junit', version: '4.12' 37 compileOnly 'org.projectlombok:lombok:1.18.6'
38 testCompileOnly 'org.projectlombok:lombok:1.18.6'
39 annotationProcessor 'org.projectlombok:lombok:1.18.6'
40
41 testCompile 'org.junit.jupiter:junit-jupiter-api:5.4.0'
42 testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.4.0'
16} 43}
diff --git a/settings.gradle b/settings.gradle
index cc93670..3796e5b 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1,2 +1,19 @@
1rootProject.name = 'lemonad' 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 */
2 18
19rootProject.name = 'lemonad'
diff --git a/src/main/java/org/pacien/lemonad/attempt/Attempt.java b/src/main/java/org/pacien/lemonad/attempt/Attempt.java
new file mode 100644
index 0000000..fc437ad
--- /dev/null
+++ b/src/main/java/org/pacien/lemonad/attempt/Attempt.java
@@ -0,0 +1,129 @@
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.attempt;
20
21import java.util.function.Consumer;
22import java.util.function.Function;
23
24import lombok.NonNull;
25
26/**
27 * Wraps either a value from a success or an error from a failure.
28 *
29 * @param <R> the potential wrapped result type.
30 * @param <E> the potential error type.
31 * @author pacien
32 */
33public interface Attempt<R, E> {
34 /**
35 * @return whether the {@link Attempt} is successful.
36 */
37 boolean isSuccess();
38
39 /**
40 * @return whether the {@link Attempt} is failed.
41 */
42 boolean isFailure();
43
44 /**
45 * @return the result if this {@link Attempt} is a success.
46 * @throws java.util.NoSuchElementException if this {@link Attempt} is a failure.
47 */
48 R getResult();
49
50 /**
51 * @return the error if this {@link Attempt} is a failure.
52 * @throws java.util.NoSuchElementException if this {@link Attempt} is a success.
53 */
54 E getError();
55
56 /**
57 * @param resultConsumer a {@link Consumer} of result called if the {@link Attempt} is a success.
58 * @return the current {@link Attempt}
59 */
60 default Attempt<R, E> ifSuccess(@NonNull Consumer<? super R> resultConsumer) {
61 if (isSuccess()) resultConsumer.accept(getResult());
62 return this;
63 }
64
65 /**
66 * @param errorConsumer a {@link Consumer} of error called if the {@link Attempt} is a failure.
67 * @return the current {@link Attempt}
68 */
69 default Attempt<R, E> ifFailure(@NonNull Consumer<? super E> errorConsumer) {
70 if (isFailure()) errorConsumer.accept(getError());
71 return this;
72 }
73
74 /**
75 * @param mapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success.
76 * @return this {@link Attempt} if it is a failure, or the produced one otherwise.
77 */
78 default <RR> Attempt<RR, E> mapResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends E>> mapper) {
79 //noinspection unchecked
80 return (Attempt<RR, E>) (isSuccess() ? mapper.apply(getResult()) : this);
81 }
82
83 /**
84 * @param mapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure.
85 * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise.
86 */
87 default <EE> Attempt<R, EE> mapFailure(@NonNull Function<? super E, ? extends Attempt<? extends R, ? extends EE>> mapper) {
88 //noinspection unchecked
89 return (Attempt<R, EE>) (isFailure() ? mapper.apply(getError()) : this);
90 }
91
92 /**
93 * @param mapper a function transforming an {@link Attempt}.
94 * @return the transformed {@link Attempt}
95 */
96 default <RR, EE> Attempt<RR, EE> flatMap(@NonNull Function<? super Attempt<? super R, ? super E>, ? extends Attempt<? extends RR, ? extends EE>> mapper) {
97 //noinspection unchecked
98 return (Attempt<RR, EE>) mapper.apply(this);
99 }
100
101 /**
102 * @param result the result of the {@link Attempt}.
103 * @return a successful {@link Attempt} wrapping the supplied result.
104 */
105 static <R, E> Attempt<R, E> success(R result) {
106 return new Success<>(result);
107 }
108
109 /**
110 * @param error the cause of the failure of the {@link Attempt}.
111 * @return a failed {@link Attempt} with the supplied error.
112 */
113 static <R, E> Attempt<R, E> failure(E error) {
114 return new Failure<>(error);
115 }
116
117 /**
118 * @param supplier a {@code Supplier} that may throw an {@link Throwable}.
119 * @return an {@link Attempt} wrapping either the result of the execution of the supplier or any thrown {@link Throwable}.
120 */
121 static <R, E extends Throwable> Attempt<R, E> attempt(@NonNull ThrowingSupplier<? extends R, ? extends E> supplier) {
122 try {
123 return success(supplier.get());
124 } catch (Throwable throwable) {
125 //noinspection unchecked
126 return (Attempt<R, E>) failure(throwable);
127 }
128 }
129}
diff --git a/src/main/java/org/pacien/lemonad/attempt/Failure.java b/src/main/java/org/pacien/lemonad/attempt/Failure.java
new file mode 100644
index 0000000..752e981
--- /dev/null
+++ b/src/main/java/org/pacien/lemonad/attempt/Failure.java