From 60a665926403e65ae6abf181916c439db52ce0e2 Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 5 Apr 2019 05:11:13 +0200 Subject: refactor and add simple mapping inner methods --- .../java/org/pacien/lemonad/attempt/Attempt.java | 58 ++++++++++++++-------- .../org/pacien/lemonad/attempt/AttemptTest.java | 17 ++++--- 2 files changed, 47 insertions(+), 28 deletions(-) (limited to 'src') 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 { } /** - * @param mapper a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success. + * @param transformer a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success. * @return this {@link Attempt} if it is a failure, or the produced one otherwise. */ - default Attempt mapResult(@NonNull Function> mapper) { + default Attempt transformResult(@NonNull Function> transformer) { //noinspection unchecked - return (Attempt) (isSuccess() ? mapper.apply(getResult()) : this); + return (Attempt) (isSuccess() ? transformer.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. + * @param transformer 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 transformer} 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)))); + default Attempt transformResult(@NonNull Function> transformer, + @NonNull Function errorAdapter) { + return transformResult(transformer.andThen(attempt -> attempt.recoverError(errorAdapter.andThen(Attempt::failure)))); } /** - * @param mapper a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure. + * @param mapper a function mapping used to map any result if this {@link Attempt} is a success. + * @return this {@link Attempt} if it is a failure, or the mutated one otherwise. + */ + default Attempt mapResult(@NonNull Function mapper) { + return transformResult(mapper.andThen(Attempt::success)); + } + + /** + * @param recoverer 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. */ - default Attempt mapError(@NonNull Function> mapper) { + default Attempt recoverError(@NonNull Function> recoverer) { //noinspection unchecked - return (Attempt) (isFailure() ? mapper.apply(getError()) : this); + return (Attempt) (isFailure() ? recoverer.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. + * @param recoverer 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 recoverer} 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)))); + default Attempt recoverError(@NonNull Function> recoverer, + @NonNull Function resultAdapter) { + return recoverError(recoverer.andThen(attempt -> attempt.transformResult(resultAdapter.andThen(Attempt::success)))); + } + + /** + * @param mapper a function mapping used to map any result if this {@link Attempt} is a failure. + * @return this {@link Attempt} if it is a success, or the mutated one otherwise. + */ + default Attempt mapError(@NonNull Function mapper) { + return recoverError(mapper.andThen(Attempt::failure)); } /** - * @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. + * @param resultTransformer a function producing an {@link Attempt}, called with the current result if this {@link Attempt} is a success. + * @param errorTransformer a function producing an {@link Attempt}, called with the current error if this {@link Attempt} is a failure. * @return the transformed {@link Attempt}. */ - default Attempt map(@NonNull Function> resultMapper, - @NonNull Function> errorMapper) { + default Attempt transform(@NonNull Function> resultTransformer, + @NonNull Function> errorTransformer) { //noinspection unchecked - return (Attempt) (isSuccess() ? resultMapper.apply(getResult()) : errorMapper.apply(getError())); + return (Attempt) (isSuccess() ? resultTransformer.apply(getResult()) : errorTransformer.apply(getError())); } /** 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 { var error3 = false; Attempt.success(result0) - .mapError((Long err) -> { + .recoverError((Long err) -> { fail(); return Attempt.failure(err); }) - .mapResult((Integer res) -> Attempt.success(result1)) - .mapResult((String res) -> { + .mapResult((Integer res) -> { + assertEquals(result0, res); + return result1; + }) + .transformResult((String res) -> { assertEquals(result1, res); return Attempt.failure(error0); }, (Integer err) -> { @@ -102,15 +105,15 @@ class AttemptTest { return (long) err; }) .ifSuccess((String __) -> fail()) - .mapResult((String res) -> { + .transformResult((String res) -> { fail(); return Attempt.success(res); }) .mapError((Long err) -> { assertEquals(error0, err); - return Attempt.failure(error1); + return error1; }) - .mapError((Long err) -> { + .recoverError((Long err) -> { assertEquals(error1, err); return Attempt.success(result2); }, (Long res) -> { @@ -124,7 +127,7 @@ class AttemptTest { }) .ifSuccess(__ -> fail()) .ifFailure(f -> assertEquals(error2, f)) - .map((String __) -> Attempt.failure(error3), (String __) -> Attempt.success(result3)) + .transform((String __) -> Attempt.failure(error3), (String __) -> Attempt.success(result3)) .ifSuccess((String result) -> assertEquals(result3, result)) .ifFailure((Boolean __) -> fail()); } -- cgit v1.2.3