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 ++++++++++++++-------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'src/main/java/org/pacien/lemonad') 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())); } /** -- cgit v1.2.3