aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2019-04-05 04:25:09 +0200
committerpacien2019-04-05 04:25:09 +0200
commit614bd19ad5298bdf1504b8c2d190b1d6cad4e34d (patch)
treeeaffa151d9d3dbde1c231f7070689b24c273050b /src
parent00045fe7355b4cf07afe6bedba85d0bf7bde2b26 (diff)
downloadjava-lemonad-614bd19ad5298bdf1504b8c2d190b1d6cad4e34d.tar.gz
add shortcuts for mapping adapters
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/pacien/lemonad/attempt/Attempt.java20
-rw-r--r--src/test/java/org/pacien/lemonad/attempt/AttemptTest.java22
2 files changed, 34 insertions, 8 deletions
diff --git a/src/main/java/org/pacien/lemonad/attempt/Attempt.java b/src/main/java/org/pacien/lemonad/attempt/Attempt.java
index dcbb2d3..d5d82ed 100644
--- a/src/main/java/org/pacien/lemonad/attempt/Attempt.java
+++ b/src/main/java/org/pacien/lemonad/attempt/Attempt.java
@@ -81,6 +81,16 @@ public interface Attempt<R, E> {
81 } 81 }
82 82
83 /** 83 /**
84 * @param mapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success.
85 * @param errorAdapter a function adapting any intermediate error returned by the {@code mapper} function.
86 * @return this {@link Attempt} if it is a failure, or the produced one otherwise.
87 */
88 default <RR, IE> Attempt<RR, E> mapResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends IE>> mapper,
89 @NonNull Function<? super IE, ? extends E> errorAdapter) {
90 return mapResult(result -> mapper.apply(result).mapError(error -> failure(errorAdapter.apply(error))));
91 }
92
93 /**
84 * @param mapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure. 94 * @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. 95 * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise.
86 */ 96 */
@@ -90,6 +100,16 @@ public interface Attempt<R, E> {
90 } 100 }
91 101
92 /** 102 /**
103 * @param mapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure.
104 * @param resultAdapter a function adapting any intermediate result returned by the {@code mapper} function.
105 * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise.
106 */
107 default <IR, EE> Attempt<R, EE> mapError(@NonNull Function<? super E, ? extends Attempt<? extends IR, ? extends EE>> mapper,
108 @NonNull Function<? super IR, ? extends R> resultAdapter) {
109 return mapError(error -> mapper.apply(error).mapResult(result -> success(resultAdapter.apply(result))));
110 }
111
112 /**
93 * @param resultMapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success. 113 * @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. 114 * @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}. 115 * @return the transformed {@link Attempt}.
diff --git a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
index 706a81d..1cb79c9 100644
--- a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
+++ b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
@@ -81,15 +81,15 @@ 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 = "result"; 84 var result2 = 0L;
85 var result3 = 0L; 85 var result3 = "0";
86 var error0 = 0; 86 var error0 = 0;
87 var error1 = 0L; 87 var error1 = 0L;
88 var error2 = "fail"; 88 var error2 = "fail";
89 var error3 = false; 89 var error3 = false;
90 90
91 Attempt.<Integer, Integer>success(result0) 91 Attempt.<Integer, Long>success(result0)
92 .mapError((Integer err) -> { 92 .mapError((Long err) -> {
93 fail(); 93 fail();
94 return Attempt.failure(err); 94 return Attempt.failure(err);
95 }) 95 })
@@ -97,29 +97,35 @@ class AttemptTest {
97 .mapResult((String res) -> { 97 .mapResult((String res) -> {
98 assertEquals(result1, res); 98 assertEquals(result1, res);
99 return Attempt.<String, Integer>failure(error0); 99 return Attempt.<String, Integer>failure(error0);
100 }, (Integer err) -> {
101 assertEquals(error0, err);
102 return (long) err;
100 }) 103 })
101 .ifSuccess((String __) -> fail()) 104 .ifSuccess((String __) -> fail())
102 .mapResult((String res) -> { 105 .mapResult((String res) -> {
103 fail(); 106 fail();
104 return Attempt.success(res); 107 return Attempt.success(res);
105 }) 108 })
106 .mapError((Integer err) -> { 109 .mapError((Long err) -> {
107 assertEquals(error0, err); 110 assertEquals(error0, err);
108 return Attempt.failure(error1); 111 return Attempt.failure(error1);
109 }) 112 })
110 .mapError((Long err) -> { 113 .mapError((Long err) -> {
111 assertEquals(error1, err); 114 assertEquals(error1, err);
112 return Attempt.<String, Long>success(result2); 115 return Attempt.<Long, Long>success(result2);
116 }, (Long res) -> {
117 assertEquals(result2, res);
118 return res.toString();
113 }) 119 })
114 .ifFailure((Long err) -> fail()) 120 .ifFailure((Long err) -> fail())
115 .flatMap((Attempt<? super String, ? super Long> attempt) -> { 121 .flatMap((Attempt<? super String, ? super Long> attempt) -> {
116 assertEquals(result2, attempt.getResult()); 122 assertEquals(Long.toString(result2), attempt.getResult());
117 return Attempt.<String, String>failure(error2); 123 return Attempt.<String, String>failure(error2);
118 }) 124 })
119 .ifSuccess(__ -> fail()) 125 .ifSuccess(__ -> fail())
120 .ifFailure(f -> assertEquals(error2, f)) 126 .ifFailure(f -> assertEquals(error2, f))
121 .map((String __) -> Attempt.failure(error3), (String __) -> Attempt.success(result3)) 127 .map((String __) -> Attempt.failure(error3), (String __) -> Attempt.success(result3))
122 .ifSuccess((Long result) -> assertEquals(result3, result)) 128 .ifSuccess((String result) -> assertEquals(result3, result))
123 .ifFailure((Boolean __) -> fail()); 129 .ifFailure((Boolean __) -> fail());
124 } 130 }
125} 131}