diff options
author | pacien | 2019-04-05 04:25:09 +0200 |
---|---|---|
committer | pacien | 2019-04-05 04:25:09 +0200 |
commit | 614bd19ad5298bdf1504b8c2d190b1d6cad4e34d (patch) | |
tree | eaffa151d9d3dbde1c231f7070689b24c273050b /src/main/java/org/pacien/lemonad/attempt | |
parent | 00045fe7355b4cf07afe6bedba85d0bf7bde2b26 (diff) | |
download | java-lemonad-614bd19ad5298bdf1504b8c2d190b1d6cad4e34d.tar.gz |
add shortcuts for mapping adapters
Diffstat (limited to 'src/main/java/org/pacien/lemonad/attempt')
-rw-r--r-- | src/main/java/org/pacien/lemonad/attempt/Attempt.java | 20 |
1 files changed, 20 insertions, 0 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}. |