aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/pacien/lemonad/attempt/Attempt.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/pacien/lemonad/attempt/Attempt.java')
-rw-r--r--src/main/java/org/pacien/lemonad/attempt/Attempt.java58
1 files changed, 37 insertions, 21 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 /**