aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/pacien/lemonad/attempt/Attempt.java11
-rw-r--r--src/test/java/org/pacien/lemonad/attempt/AttemptTest.java15
2 files changed, 21 insertions, 5 deletions
diff --git a/src/main/java/org/pacien/lemonad/attempt/Attempt.java b/src/main/java/org/pacien/lemonad/attempt/Attempt.java
index e36ac4b..9f5befa 100644
--- a/src/main/java/org/pacien/lemonad/attempt/Attempt.java
+++ b/src/main/java/org/pacien/lemonad/attempt/Attempt.java
@@ -90,6 +90,17 @@ public interface Attempt<R, E> {
90 } 90 }
91 91
92 /** 92 /**
93 * @param resultMapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success.
94 * @param errorMapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure.
95 * @return the transformed {@link Attempt}.
96 */
97 default <RR, EE> Attempt<RR, EE> map(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends EE>> resultMapper,
98 @NonNull Function<? super E, ? extends Attempt<? extends RR, ? extends EE>> errorMapper) {
99 //noinspection unchecked
100 return (Attempt<RR, EE>) (isSuccess() ? resultMapper.apply(getResult()) : errorMapper.apply(getError()));
101 }
102
103 /**
93 * @param mapper a function transforming an {@link Attempt}. 104 * @param mapper a function transforming an {@link Attempt}.
94 * @return the transformed {@link Attempt} 105 * @return the transformed {@link Attempt}
95 */ 106 */
diff --git a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
index 6d87335..bb367dc 100644
--- a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
+++ b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
@@ -81,10 +81,12 @@ class AttemptTest {
81 @Test void testTransformationFlow() { 81 @Test void testTransformationFlow() {
82 var result0 = 0; 82 var result0 = 0;
83 var result1 = "res"; 83 var result1 = "res";
84 var result2 = 0L; 84 var result2 = true;
85 var fault0 = 0; 85 var result3 = 0L;
86 var fault1 = 1; 86 var fault0 = 0L;
87 var fault2 = 2; 87 var fault1 = 0;
88 var fault2 = "fail";
89 var fault3 = false;
88 90
89 Attempt.success(result0) 91 Attempt.success(result0)
90 .mapError(__ -> fail()) 92 .mapError(__ -> fail())
@@ -109,6 +111,9 @@ class AttemptTest {
109 return Attempt.failure(fault2); 111 return Attempt.failure(fault2);
110 }) 112 })
111 .ifSuccess(__ -> fail()) 113 .ifSuccess(__ -> fail())
112 .ifFailure(f -> assertEquals(fault2, f)); 114 .ifFailure(f -> assertEquals(fault2, f))
115 .map(__ -> Attempt.failure(fault3), __ -> Attempt.success(result3))
116 .ifSuccess(result -> assertEquals(result3, result))
117 .ifFailure(__ -> fail());
113 } 118 }
114} 119}