From 614bd19ad5298bdf1504b8c2d190b1d6cad4e34d Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 5 Apr 2019 04:25:09 +0200 Subject: add shortcuts for mapping adapters --- .../java/org/pacien/lemonad/attempt/Attempt.java | 20 ++++++++++++++++++++ .../org/pacien/lemonad/attempt/AttemptTest.java | 22 ++++++++++++++-------- 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 @@ -80,6 +80,16 @@ public interface Attempt { return (Attempt) (isSuccess() ? mapper.apply(getResult()) : this); } + /** + * @param mapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success. + * @param errorAdapter a function adapting any intermediate error returned by the {@code mapper} function. + * @return this {@link Attempt} if it is a failure, or the produced one otherwise. + */ + default Attempt mapResult(@NonNull Function> mapper, + @NonNull Function errorAdapter) { + return mapResult(result -> mapper.apply(result).mapError(error -> failure(errorAdapter.apply(error)))); + } + /** * @param mapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure. * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise. @@ -89,6 +99,16 @@ public interface Attempt { return (Attempt) (isFailure() ? mapper.apply(getError()) : this); } + /** + * @param mapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure. + * @param resultAdapter a function adapting any intermediate result returned by the {@code mapper} function. + * @return this {@link Attempt} if it is a success, or the alternative {@link Attempt} retrieved from the supplier otherwise. + */ + default Attempt mapError(@NonNull Function> mapper, + @NonNull Function resultAdapter) { + return mapError(error -> mapper.apply(error).mapResult(result -> success(resultAdapter.apply(result)))); + } + /** * @param resultMapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success. * @param errorMapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure. 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 { @Test void testTransformationFlow() { var result0 = 0; var result1 = "res"; - var result2 = "result"; - var result3 = 0L; + var result2 = 0L; + var result3 = "0"; var error0 = 0; var error1 = 0L; var error2 = "fail"; var error3 = false; - Attempt.success(result0) - .mapError((Integer err) -> { + Attempt.success(result0) + .mapError((Long err) -> { fail(); return Attempt.failure(err); }) @@ -97,29 +97,35 @@ class AttemptTest { .mapResult((String res) -> { assertEquals(result1, res); return Attempt.failure(error0); + }, (Integer err) -> { + assertEquals(error0, err); + return (long) err; }) .ifSuccess((String __) -> fail()) .mapResult((String res) -> { fail(); return Attempt.success(res); }) - .mapError((Integer err) -> { + .mapError((Long err) -> { assertEquals(error0, err); return Attempt.failure(error1); }) .mapError((Long err) -> { assertEquals(error1, err); - return Attempt.success(result2); + return Attempt.success(result2); + }, (Long res) -> { + assertEquals(result2, res); + return res.toString(); }) .ifFailure((Long err) -> fail()) .flatMap((Attempt attempt) -> { - assertEquals(result2, attempt.getResult()); + assertEquals(Long.toString(result2), attempt.getResult()); return Attempt.failure(error2); }) .ifSuccess(__ -> fail()) .ifFailure(f -> assertEquals(error2, f)) .map((String __) -> Attempt.failure(error3), (String __) -> Attempt.success(result3)) - .ifSuccess((Long result) -> assertEquals(result3, result)) + .ifSuccess((String result) -> assertEquals(result3, result)) .ifFailure((Boolean __) -> fail()); } } -- cgit v1.2.3