aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2019-04-05 05:11:13 +0200
committerpacien2019-04-05 05:11:13 +0200
commit60a665926403e65ae6abf181916c439db52ce0e2 (patch)
tree0a741757d8fb537d378babfe1c1f1c6fb62b4586 /src
parent463df829ac10ee981ed64bca100c82549f020bcc (diff)
downloadjava-lemonad-60a665926403e65ae6abf181916c439db52ce0e2.tar.gz
refactor and add simple mapping inner methods
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/pacien/lemonad/attempt/Attempt.java58
-rw-r--r--src/test/java/org/pacien/lemonad/attempt/AttemptTest.java17
2 files changed, 47 insertions, 28 deletions
diff --git a/src/main/java/org/pacien/lemonad/attempt/Attempt.java b/src/main/java/org/pacien/lemonad/attempt/Attempt.java
index d5d82ed..574b6ed 100644
--- a/src/main/java/org/pacien/lemonad/attempt/Attempt.java
+++ b/src/main/java/org/pacien/lemonad/attempt/Attempt.java
@@ -72,52 +72,68 @@ public interface Attempt<R, E> {
72 } 72 }
73 73
74 /** 74 /**
75 * @param mapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success. 75 * @param transformer 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. 76 * @return this {@link Attempt} if it is a failure, or the produced one otherwise.
77 */ 77 */
78 default <RR> Attempt<RR, E> mapResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends E>> mapper) { 78 default <RR> Attempt<RR, E> transformResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends E>> transformer) {
79 //noinspection unchecked 79 //noinspection unchecked
80 return (Attempt<RR, E>) (isSuccess() ? mapper.apply(getResult()) : this); 80 return (Attempt<RR, E>) (isSuccess() ? transformer.apply(getResult()) : this);
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. 84 * @param transformer 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. 85 * @param errorAdapter a function adapting any intermediate error returned by the {@code transformer} function.
86 * @return this {@link Attempt} if it is a failure, or the produced one otherwise. 86 * @return this {@link Attempt} if it is a failure, or the produced one otherwise.
87 */ 87 */
88 default <RR, IE> Attempt<RR, E> mapResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends IE>> mapper, 88 default <RR, IE> Attempt<RR, E> transformResult(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends IE>> transformer,
89 @NonNull Function<? super IE, ? extends E> errorAdapter) { 89 @NonNull Function<? super IE, ? extends E> errorAdapter) {
90 return mapResult(result -> mapper.apply(result).mapError(error -> failure(errorAdapter.apply(error)))); 90 return transformResult(transformer.andThen(attempt -> attempt.recoverError(errorAdapter.andThen(Attempt::failure))));
91 } 91 }
92 92
93 /** 93 /**
94 * @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 mapping used to map any result if this {@link Attempt} is a success.
95 * @return this {@link Attempt} if it is a failure, or the mutated one otherwise.
96 */
97 default <RR> Attempt<RR, E> mapResult(@NonNull Function<? super R, ? extends RR> mapper) {
98 return transformResult(mapper.andThen(Attempt::success));
99 }
100
101 /**
102 * @param recoverer a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure.
95 * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise. 103 * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise.
96 */ 104 */
97 default <EE> Attempt<R, EE> mapError(@NonNull Function<? super E, ? extends Attempt<? extends R, ? extends EE>> mapper) { 105 default <EE> Attempt<R, EE> recoverError(@NonNull Function<? super E, ? extends Attempt<? extends R, ? extends EE>> recoverer) {
98 //noinspection unchecked 106 //noinspection unchecked
99 return (Attempt<R, EE>) (isFailure() ? mapper.apply(getError()) : this); 107 return (Attempt<R, EE>) (isFailure() ? recoverer.apply(getError()) : this);
100 } 108 }
101 109
102 /** 110 /**
103 * @param mapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure. 111 * @param recoverer 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. 112 * @param resultAdapter a function adapting any intermediate result returned by the {@code recoverer} function.
105 * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise. 113 * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise.
106 */ 114 */
107 default <IR, EE> Attempt<R, EE> mapError(@NonNull Function<? super E, ? extends Attempt<? extends IR, ? extends EE>> mapper, 115 default <IR, EE> Attempt<R, EE> recoverError(@NonNull Function<? super E, ? extends Attempt<? extends IR, ? extends EE>> recoverer,
108 @NonNull Function<? super IR, ? extends R> resultAdapter) { 116 @NonNull Function<? super IR, ? extends R> resultAdapter) {
109 return mapError(error -> mapper.apply(error).mapResult(result -> success(resultAdapter.apply(result)))); 117 return recoverError(recoverer.andThen(attempt -> attempt.transformResult(resultAdapter.andThen(Attempt::success))));
118 }
119
120 /**
121 * @param mapper a function mapping used to map any result if this {@link Attempt} is a failure.
122 * @return this {@link Attempt} if it is a success, or the mutated one otherwise.
123 */
124 default <EE> Attempt<R, EE> mapError(@NonNull Function<? super E, ? extends EE> mapper) {
125 return recoverError(mapper.andThen(Attempt::failure));
110 } 126 }
111 127
112 /** 128 /**
113 * @param resultMapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success. 129 * @param resultTransformer a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success.
114 * @param errorMapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure. 130 * @param errorTransformer a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure.
115 * @return the transformed {@link Attempt}. 131 * @return the transformed {@link Attempt}.
116 */ 132 */
117 default <RR, EE> Attempt<RR, EE> map(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends EE>> resultMapper, 133 default <RR, EE> Attempt<RR, EE> transform(@NonNull Function<? super R, ? extends Attempt<? extends RR, ? extends EE>> resultTransformer,
118 @NonNull Function<? super E, ? extends Attempt<? extends RR, ? extends EE>> errorMapper) { 134 @NonNull Function<? super E, ? extends Attempt<? extends RR, ? extends EE>> errorTransformer) {
119 //noinspection unchecked 135 //noinspection unchecked
120 return (Attempt<RR, EE>) (isSuccess() ? resultMapper.apply(getResult()) : errorMapper.apply(getError())); 136 return (Attempt<RR, EE>) (isSuccess() ? resultTransformer.apply(getResult()) : errorTransformer.apply(getError()));
121 } 137 }
122 138
123 /** 139 /**
diff --git a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
index 1cb79c9..0d16c31 100644
--- a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
+++ b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
@@ -89,12 +89,15 @@ class AttemptTest {
89 var error3 = false; 89 var error3 = false;
90 90
91 Attempt.<Integer, Long>success(result0) 91 Attempt.<Integer, Long>success(result0)
92 .mapError((Long err) -> { 92 .recoverError((Long err) -> {
93 fail(); 93 fail();
94 return Attempt.failure(err); 94 return Attempt.failure(err);
95 }) 95 })
96 .mapResult((Integer res) -> Attempt.success(result1)) 96 .mapResult((Integer res) -> {
97 .mapResult((String res) -> { 97 assertEquals(result0, res);
98 return result1;
99 })
100 .transformResult((String res) -> {
98 assertEquals(result1, res); 101 assertEquals(result1, res);
99 return Attempt.<String, Integer>failure(error0); 102 return Attempt.<String, Integer>failure(error0);
100 }, (Integer err) -> { 103 }, (Integer err) -> {
@@ -102,15 +105,15 @@ class AttemptTest {
102 return (long) err; 105 return (long) err;
103 }) 106 })
104 .ifSuccess((String __) -> fail()) 107 .ifSuccess((String __) -> fail())
105 .mapResult((String res) -> { 108 .transformResult((String res) -> {
106 fail(); 109 fail();
107 return Attempt.success(res); 110 return Attempt.success(res);
108 }) 111 })
109 .mapError((Long err) -> { 112 .mapError((Long err) -> {
110 assertEquals(error0, err); 113 assertEquals(error0, err);
111 return Attempt.failure(error1); 114 return error1;
112 }) 115 })
113 .mapError((Long err) -> { 116 .recoverError((Long err) -> {
114 assertEquals(error1, err); 117 assertEquals(error1, err);
115 return Attempt.<Long, Long>success(result2); 118 return Attempt.<Long, Long>success(result2);
116 }, (Long res) -> { 119 }, (Long res) -> {
@@ -124,7 +127,7 @@ class AttemptTest {
124 }) 127 })
125 .ifSuccess(__ -> fail()) 128 .ifSuccess(__ -> fail())
126 .ifFailure(f -> assertEquals(error2, f)) 129 .ifFailure(f -> assertEquals(error2, f))
127 .map((String __) -> Attempt.failure(error3), (String __) -> Attempt.success(result3)) 130 .transform((String __) -> Attempt.failure(error3), (String __) -> Attempt.success(result3))
128 .ifSuccess((String result) -> assertEquals(result3, result)) 131 .ifSuccess((String result) -> assertEquals(result3, result))
129 .ifFailure((Boolean __) -> fail()); 132 .ifFailure((Boolean __) -> fail());
130 } 133 }