From 1ca686a3f66b25f68e1fc8668aff4fa1b5a9a743 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 31 Aug 2018 22:53:06 +0100 Subject: [PATCH] Add Maybe.or(Supplier) Like a flatMap for the negative case. --- CHANGELOG | 1 + README.org | 10 ++++++++++ src/main/java/net/kemitix/mon/maybe/Just.java | 5 +++++ .../java/net/kemitix/mon/maybe/Maybe.java | 9 +++++++++ .../java/net/kemitix/mon/maybe/Nothing.java | 5 +++++ src/test/java/net/kemitix/mon/MaybeTest.java | 19 +++++++++++++++++++ 6 files changed, 49 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 5362e87..c5db080 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ CHANGELOG 0.12.0 ------ +* Add Maybe.or(Supplier) * Add Result.reduce(Result,BinaryOperator) * Rename Result.invert() as Result.swap() * [admin] pom: update urls to github diff --git a/README.org b/README.org index 8117b51..d24484d 100644 --- a/README.org +++ b/README.org @@ -444,6 +444,16 @@ #+END_SRC +**** =T or(Supplier alternative)= + + Provide an alternative Maybe to use when Maybe is Nothing. + + #+BEGIN_SRC java + final Maybe value = Maybe.maybe(getValue()) + .or(() -> Maybe.just(defaultValue)); + #+END_SRC + + **** =void orElseThrow(Supplier error)= Throw the exception if the Maybe is a Nothing. diff --git a/src/main/java/net/kemitix/mon/maybe/Just.java b/src/main/java/net/kemitix/mon/maybe/Just.java index fd916bf..46304c3 100644 --- a/src/main/java/net/kemitix/mon/maybe/Just.java +++ b/src/main/java/net/kemitix/mon/maybe/Just.java @@ -118,6 +118,11 @@ final class Just implements Maybe { justMatcher.accept(value); } + @Override + public Maybe or(final Supplier> alternative) { + return this; + } + @Override public Stream stream() { return Stream.of(value); diff --git a/src/main/java/net/kemitix/mon/maybe/Maybe.java b/src/main/java/net/kemitix/mon/maybe/Maybe.java index e24dec7..c862a32 100644 --- a/src/main/java/net/kemitix/mon/maybe/Maybe.java +++ b/src/main/java/net/kemitix/mon/maybe/Maybe.java @@ -177,4 +177,13 @@ public interface Maybe extends Functor> { * @param nothingMatcher the Runnable to call if the Maybe is a Nothing */ void match(Consumer justMatcher, Runnable nothingMatcher); + + /** + * Maps the Maybe into another Maybe only when it is nothing. + * + * @param alternative the maybe to map the nothing into + * + * @return the original Maybe if not nothing, or the alternative + */ + Maybe or(Supplier> alternative); } diff --git a/src/main/java/net/kemitix/mon/maybe/Nothing.java b/src/main/java/net/kemitix/mon/maybe/Nothing.java index ada237e..9eed4f8 100644 --- a/src/main/java/net/kemitix/mon/maybe/Nothing.java +++ b/src/main/java/net/kemitix/mon/maybe/Nothing.java @@ -100,6 +100,11 @@ final class Nothing implements Maybe { nothingMatcher.run(); } + @Override + public Maybe or(final Supplier> alternative) { + return alternative.get(); + } + @Override public Stream stream() { return Stream.empty(); diff --git a/src/test/java/net/kemitix/mon/MaybeTest.java b/src/test/java/net/kemitix/mon/MaybeTest.java index ae354fc..af35dca 100644 --- a/src/test/java/net/kemitix/mon/MaybeTest.java +++ b/src/test/java/net/kemitix/mon/MaybeTest.java @@ -292,4 +292,23 @@ public class MaybeTest implements WithAssertions { assertThat(isNothing).isTrue(); } + @Test + public void just_or_ignoreTheAlternative() { + //given + final Maybe one = Maybe.just("one"); + //when + final Maybe result = one.or(() -> Maybe.just("two")); + //then + assertThat(result.toOptional()).contains("one"); + } + + @Test + public void nothing_or_isTheAlternative() { + //given + final Maybe one = Maybe.nothing(); + //when + final Maybe result = one.or(() -> Maybe.just("two")); + //then + assertThat(result.toOptional()).contains("two"); + } }