From 51f86884fbe95435cf4bcab027ad0eb0efb464ec Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 12 Jul 2018 20:28:34 +0100 Subject: [PATCH] Add `Maybe.isJust()` and `Maybe.isNothing()` --- CHANGELOG | 3 ++ src/main/java/net/kemitix/mon/maybe/Just.java | 10 +++++ .../java/net/kemitix/mon/maybe/Maybe.java | 14 +++++++ .../java/net/kemitix/mon/maybe/Nothing.java | 10 +++++ .../java/net/kemitix/mon/result/Result.java | 4 +- src/test/java/net/kemitix/mon/MaybeTest.java | 40 +++++++++++++++++++ 6 files changed, 79 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d55eac1..5db0e0f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,9 @@ CHANGELOG 0.11.0 * Rename `Result.maybeThen()` as `Result.flatMapMaybe()` +* Add `Maybe.match(Consumer,Runnable)` +* Add `Maybe.isJust()` +* Add `Maybe.isNothing()` 0.10.0 ------ diff --git a/src/main/java/net/kemitix/mon/maybe/Just.java b/src/main/java/net/kemitix/mon/maybe/Just.java index b950904..9dea530 100644 --- a/src/main/java/net/kemitix/mon/maybe/Just.java +++ b/src/main/java/net/kemitix/mon/maybe/Just.java @@ -44,6 +44,16 @@ final class Just implements Maybe { private final T value; + @Override + public boolean isJust() { + return true; + } + + @Override + public boolean isNothing() { + return false; + } + @Override public Maybe flatMap(final Function> f) { return f.apply(value); diff --git a/src/main/java/net/kemitix/mon/maybe/Maybe.java b/src/main/java/net/kemitix/mon/maybe/Maybe.java index 723ccd4..0093c53 100644 --- a/src/main/java/net/kemitix/mon/maybe/Maybe.java +++ b/src/main/java/net/kemitix/mon/maybe/Maybe.java @@ -76,6 +76,20 @@ public interface Maybe extends Functor> { return just(value); } + /** + * Checks if the Maybe is a Just. + * + * @return true if the Maybe is a Just + */ + boolean isJust(); + + /** + * Checks if the Maybe is Nothing. + * + * @return true if the Maybe is Nothing + */ + boolean isNothing(); + /** * Monad binder maps the Maybe into another Maybe using the binder method f. * diff --git a/src/main/java/net/kemitix/mon/maybe/Nothing.java b/src/main/java/net/kemitix/mon/maybe/Nothing.java index 036cb14..58f169d 100644 --- a/src/main/java/net/kemitix/mon/maybe/Nothing.java +++ b/src/main/java/net/kemitix/mon/maybe/Nothing.java @@ -39,6 +39,16 @@ final class Nothing implements Maybe { static final Maybe INSTANCE = new Nothing<>(); + @Override + public boolean isJust() { + return false; + } + + @Override + public boolean isNothing() { + return true; + } + @Override public Maybe flatMap(final Function> f) { return Maybe.nothing(); diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 829fd64..72cafdb 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -134,14 +134,14 @@ public interface Result extends Functor> { } /** - * Checks of the Result is an error. + * Checks if the Result is an error. * * @return true if the Result is an error. */ boolean isError(); /** - * Checks of the Result is a success. + * Checks if the Result is a success. * * @return true if the Result is a success. */ diff --git a/src/test/java/net/kemitix/mon/MaybeTest.java b/src/test/java/net/kemitix/mon/MaybeTest.java index c35d4c8..74c6efe 100644 --- a/src/test/java/net/kemitix/mon/MaybeTest.java +++ b/src/test/java/net/kemitix/mon/MaybeTest.java @@ -219,4 +219,44 @@ public class MaybeTest implements WithAssertions { assertThat(flag).isTrue(); } + @Test + public void just_isJust_isTrue() { + //given + final Maybe maybe = just(1); + //when + final boolean isJust = maybe.isJust(); + //then + assertThat(isJust).isTrue(); + } + + @Test + public void just_isNothing_isFalse() { + //given + final Maybe maybe = just(1); + //when + final boolean isNothing = maybe.isNothing(); + //then + assertThat(isNothing).isFalse(); + } + + @Test + public void nothing_isJust_isFalse() { + //given + final Maybe maybe = nothing(); + //when + final boolean isJust = maybe.isJust(); + //then + assertThat(isJust).isFalse(); + } + + @Test + public void nothing_isNothing_isTrue() { + //given + final Maybe maybe = nothing(); + //when + final boolean isNothing = maybe.isNothing(); + //then + assertThat(isNothing).isTrue(); + } + }