diff --git a/src/main/java/net/kemitix/mon/result/Err.java b/src/main/java/net/kemitix/mon/result/Err.java index ca2d8dc..e5fca3c 100644 --- a/src/main/java/net/kemitix/mon/result/Err.java +++ b/src/main/java/net/kemitix/mon/result/Err.java @@ -24,7 +24,6 @@ package net.kemitix.mon.result; import lombok.RequiredArgsConstructor; import java.util.Objects; -import java.util.concurrent.Callable; import java.util.function.BinaryOperator; import java.util.function.Consumer; import java.util.function.Function; @@ -123,11 +122,6 @@ class Err implements Result { return this; } - @Override - public Result andThen(final Function> f) { - return (Result) this; - } - @Override public Result thenWith(final Function> f) { return this; diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 3ecbada..5f72fcd 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -446,23 +446,6 @@ public interface Result extends BaseResult, ThrowableFunctor the type of the value that may be in the Result - * @return a Result containing a Maybe, the value in the Maybe was the value in a successful Result within the - * original Maybe. If the original Maybe is Nothing, the Result will contain Nothing. If the original Result was an - * error, then the Result will also be an error. - * @deprecated - */ - @API(status = DEPRECATED) - @Deprecated - static Result> swap(final Maybe> maybeResult) { - return maybeResult.orElseGet(() -> Result.ok(null)) - .flatMap(value -> Result.ok(Maybe.maybe(value))); - } - /** * Creates a {@link Maybe} from the {@code Result}. * @@ -740,29 +723,6 @@ public interface Result extends BaseResult, ThrowableFunctor consumer ); - /** - * Maps a Success Result to another Result using a Callable that is able to throw a checked exception. - * - *

Combination of {@link #flatMap(Function)} and {@link #of(Callable)}.

- * - *

-     * Integer doSomething() {...}
-     * String doSomethingElse(final Integer value) {...}
-     * Result<String> r = Result.of(() -> doSomething())
-     *                          .andThen(value -> () -> doSomethingElse(value));
-     * 
- * - *

When the Result is an Err, then the original error is carried over and the Callable is never called.

- * - * @param f the function to map the Success value into the Callable - * @param the type of the final Result - * @return a new Result - * @deprecated Use {@link #map(ThrowableFunction)} - */ - @API(status = DEPRECATED) - @Deprecated - Result andThen(Function> f); - /** * Perform the continuation with the value within the success {@code Result} * and return itself. diff --git a/src/main/java/net/kemitix/mon/result/Success.java b/src/main/java/net/kemitix/mon/result/Success.java index afd0dfa..e9b5dbd 100644 --- a/src/main/java/net/kemitix/mon/result/Success.java +++ b/src/main/java/net/kemitix/mon/result/Success.java @@ -24,7 +24,6 @@ package net.kemitix.mon.result; import lombok.RequiredArgsConstructor; import java.util.Objects; -import java.util.concurrent.Callable; import java.util.function.BinaryOperator; import java.util.function.Consumer; import java.util.function.Function; @@ -112,11 +111,6 @@ class Success implements Result { return this; } - @Override - public Result andThen(final Function> f) { - return result(f.apply(value)); - } - @Override public Result thenWith(final Function> f) { return f.apply(value).call(this); diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index 6567e60..167fb5e 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -583,50 +583,6 @@ class ResultTest implements WithAssertions { } } - @Nested - @DisplayName("invert") - class InvertTests { - @Test - void justOkay_whenInvert_thenOkayJust() { - //given - final Maybe> justSuccess = Maybe.just(Result.ok(1)); - //when - final Result> result = Result.swap(justSuccess); - //then - result.match( - success -> assertThat(success.toOptional()).contains(1), - error -> fail("Not an error") - ); - } - - @Test - void JustError_whenInvert_isError() { - //given - final RuntimeException exception = new RuntimeException(); - final Maybe> justError = Maybe.just(anError(exception)); - //when - final Result> result = Result.swap(justError); - //then - result.match( - success -> fail("Not a success"), - error -> assertThat(error).isSameAs(exception) - ); - } - - @Test - void nothing_whenInvert_thenOkayNothing() { - //given - final Maybe> nothing = Maybe.nothing(); - //when - final Result> result = Result.swap(nothing); - //then - result.match( - success -> assertThat(success.toOptional()).isEmpty(), - error -> fail("Not an error") - ); - } - } - @Nested @DisplayName("use cases") class UseCaseTests { @@ -1059,122 +1015,6 @@ class ResultTest implements WithAssertions { } - @Nested - @DisplayName("andThen") - class AndThenTests { - @Test - void okay_whenAndThen_whereSuccess_isUpdatedSuccess() { - //given - final Result ok = Result.ok(1); - //when - final Result result = ok.andThen(v -> () -> "success"); - //then - result.match( - v -> assertThat(v).isEqualTo("success"), - e -> fail("not an error")); - } - - @Test - void okay_whenAndThen_whereError_isError() { - //given - final Result ok = Result.ok(1); - final RuntimeException exception = new RuntimeException(); - //when - final Result result = ok.andThen(v -> () -> { - throw exception; - }); - //then - result.match( - x -> fail("not a success"), - e -> assertThat(e).isSameAs(exception)); - } - - @Test - void error_whereAndThen_whereSuccess_isError() { - //given - final RuntimeException exception = new RuntimeException(); - final Result error = anError(exception); - //when - final Result result = error.andThen(v -> () -> "success"); - //then - result.match( - x -> fail("not a success"), - e -> assertThat(e).isSameAs(exception)); - } - - @Test - void error_whenAndThen_whereError_isOriginalError() { - //given - final RuntimeException exception1 = new RuntimeException(); - final Result error = anError(exception1); - //when - final Result result = error.andThen(v -> () -> { - throw new RuntimeException(); - }); - //then - result.match( - x -> fail("not a success"), - e -> assertThat(e).isSameAs(exception1)); - } - - @Test - void okayVoid_whenAndThen_whereSuccess_isUpdatedSuccess() { - //given - final ResultVoid ok = Result.ok(); - //when - final ResultVoid result = ok.andThen(() -> { - // do nothing - }); - //then - assertThat(result.isOkay()).isTrue(); - } - - @Test - void okayVoid_whenAndThen_whereError_isError() { - //given - final ResultVoid ok = Result.ok(); - final RuntimeException exception = new RuntimeException(); - //when - final ResultVoid result = ok.andThen(() -> { - throw exception; - }); - //then - result.match( - () -> fail("not a success"), - e -> assertThat(e).isSameAs(exception)); - } - - @Test - void errorVoid_whereAndThen_whereSuccess_isError() { - //given - final RuntimeException exception = new RuntimeException(); - final ResultVoid error = Result.error(exception); - //when - final ResultVoid result = error.andThen(() -> { - // do nothing - }); - //then - result.match( - () -> fail("not a success"), - e -> assertThat(e).isSameAs(exception)); - } - - @Test - void errorVoid_whenAndThen_whereError_isOriginalError() { - //given - final RuntimeException exception1 = new RuntimeException(); - final ResultVoid error = Result.error(exception1); - //when - final ResultVoid result = error.andThen(() -> { - throw new RuntimeException(); - }); - //then - result.match( - () -> fail("not a success"), - e -> assertThat(e).isSameAs(exception1)); - } - } - @Nested @DisplayName("thenWith") class ThenWithTests { @@ -1770,6 +1610,64 @@ class ResultTest implements WithAssertions { } + @Nested + @DisplayName("andThen") + class AndThenTests { + + @Test + void successVoid_andThen_returnSelf() { + //given + ResultVoid ok = Result.ok(); + //when + ResultVoid result = ok.andThen(() -> {}); + //then + assertThat(result).isSameAs(ok); + } + + @Test + void successVoid_andThen_isCalled() { + //given + ResultVoid ok = Result.ok(); + AtomicBoolean called = new AtomicBoolean(false); + //when + ok.andThen(() -> called.set(true)); + //then + assertThat(called).isTrue(); + } + + @Test + void successVoid_andThen_exception_errorVoid() { + //given + ResultVoid ok = Result.ok(); + //when + ResultVoid result = ok.andThen(() -> {throw new RuntimeException();}); + //then + assertThat(result.isError()).isTrue(); + } + + @Test + void errorVoid_andThen_returnsSelf() { + //given + ResultVoid error = Result.error(new RuntimeException()); + //when + ResultVoid result = error.andThen(() -> {}); + //then + assertThat(result).isSameAs(error); + } + + @Test + void errorVoid_andThen_notCalled() { + //given + ResultVoid error = Result.error(new RuntimeException()); + final AtomicBoolean called = new AtomicBoolean(false); + //when + error.andThen(() -> called.set(true)); + //then + assertThat(called).isFalse(); + } + + } + /** * These include snippets from the Javadocs and are meant to prove that the examples are valid. */ @@ -2395,7 +2293,7 @@ class ResultTest implements WithAssertions { Result businessOperation(final String fileName1, final String fileName2) { return readIntFromFile(fileName1) - .andThen(intFromFile1 -> () -> adjustValue(intFromFile1)) + .map(this::adjustValue) .flatMap(adjustedIntFromFile1 -> readIntFromFile(fileName2) .flatMap(intFromFile2 -> adjustedIntFromFile1 .flatMap(aif1 -> calculateAverage(aif1, intFromFile2))));