From 0ac555a6d383fa76626a6123e008365e005558a3 Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 5 Apr 2019 03:00:45 +0200 Subject: add missing map method --- src/main/java/org/pacien/lemonad/attempt/Attempt.java | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/main/java/org') diff --git a/src/main/java/org/pacien/lemonad/attempt/Attempt.java b/src/main/java/org/pacien/lemonad/attempt/Attempt.java index e36ac4b..9f5befa 100644 --- a/src/main/java/org/pacien/lemonad/attempt/Attempt.java +++ b/src/main/java/org/pacien/lemonad/attempt/Attempt.java @@ -89,6 +89,17 @@ public interface Attempt { return (Attempt) (isFailure() ? mapper.apply(getError()) : this); } + /** + * @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. + * @return the transformed {@link Attempt}. + */ + default Attempt map(@NonNull Function> resultMapper, + @NonNull Function> errorMapper) { + //noinspection unchecked + return (Attempt) (isSuccess() ? resultMapper.apply(getResult()) : errorMapper.apply(getError())); + } + /** * @param mapper a function transforming an {@link Attempt}. * @return the transformed {@link Attempt} -- cgit v1.2.3