Add Maybe.isJust() and Maybe.isNothing()

This commit is contained in:
Paul Campbell 2018-07-12 20:28:34 +01:00
parent d66c4c8502
commit 51f86884fb
6 changed files with 79 additions and 2 deletions

View file

@ -4,6 +4,9 @@ CHANGELOG
0.11.0 0.11.0
* Rename `Result.maybeThen()` as `Result.flatMapMaybe()` * Rename `Result.maybeThen()` as `Result.flatMapMaybe()`
* Add `Maybe.match(Consumer,Runnable)`
* Add `Maybe.isJust()`
* Add `Maybe.isNothing()`
0.10.0 0.10.0
------ ------

View file

@ -44,6 +44,16 @@ final class Just<T> implements Maybe<T> {
private final T value; private final T value;
@Override
public boolean isJust() {
return true;
}
@Override
public boolean isNothing() {
return false;
}
@Override @Override
public <R> Maybe<R> flatMap(final Function<T, Maybe<R>> f) { public <R> Maybe<R> flatMap(final Function<T, Maybe<R>> f) {
return f.apply(value); return f.apply(value);

View file

@ -76,6 +76,20 @@ public interface Maybe<T> extends Functor<T, Maybe<?>> {
return just(value); 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. * Monad binder maps the Maybe into another Maybe using the binder method f.
* *

View file

@ -39,6 +39,16 @@ final class Nothing<T> implements Maybe<T> {
static final Maybe<?> INSTANCE = new Nothing<>(); static final Maybe<?> INSTANCE = new Nothing<>();
@Override
public boolean isJust() {
return false;
}
@Override
public boolean isNothing() {
return true;
}
@Override @Override
public <R> Maybe<R> flatMap(final Function<T, Maybe<R>> f) { public <R> Maybe<R> flatMap(final Function<T, Maybe<R>> f) {
return Maybe.nothing(); return Maybe.nothing();

View file

@ -134,14 +134,14 @@ public interface Result<T> extends Functor<T, Result<?>> {
} }
/** /**
* Checks of the Result is an error. * Checks if the Result is an error.
* *
* @return true if the Result is an error. * @return true if the Result is an error.
*/ */
boolean isError(); boolean isError();
/** /**
* Checks of the Result is a success. * Checks if the Result is a success.
* *
* @return true if the Result is a success. * @return true if the Result is a success.
*/ */

View file

@ -219,4 +219,44 @@ public class MaybeTest implements WithAssertions {
assertThat(flag).isTrue(); assertThat(flag).isTrue();
} }
@Test
public void just_isJust_isTrue() {
//given
final Maybe<Integer> maybe = just(1);
//when
final boolean isJust = maybe.isJust();
//then
assertThat(isJust).isTrue();
}
@Test
public void just_isNothing_isFalse() {
//given
final Maybe<Integer> maybe = just(1);
//when
final boolean isNothing = maybe.isNothing();
//then
assertThat(isNothing).isFalse();
}
@Test
public void nothing_isJust_isFalse() {
//given
final Maybe<Object> maybe = nothing();
//when
final boolean isJust = maybe.isJust();
//then
assertThat(isJust).isFalse();
}
@Test
public void nothing_isNothing_isTrue() {
//given
final Maybe<Object> maybe = nothing();
//when
final boolean isNothing = maybe.isNothing();
//then
assertThat(isNothing).isTrue();
}
} }