Add Maybe.match(Consumer,Runnable)
This commit is contained in:
parent
c64284872c
commit
d66c4c8502
4 changed files with 46 additions and 2 deletions
|
@ -98,6 +98,11 @@ final class Just<T> implements Maybe<T> {
|
||||||
// ignore - not nothing
|
// ignore - not nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void match(final Consumer<T> justMatcher, final Runnable nothingMatcher) {
|
||||||
|
justMatcher.accept(value);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void orElseThrow(final Supplier<Exception> e) {
|
public void orElseThrow(final Supplier<Exception> e) {
|
||||||
// do not throw
|
// do not throw
|
||||||
|
|
|
@ -149,4 +149,12 @@ public interface Maybe<T> extends Functor<T, Maybe<?>> {
|
||||||
* @param runnable the runnable to call if this is a Nothing
|
* @param runnable the runnable to call if this is a Nothing
|
||||||
*/
|
*/
|
||||||
void ifNothing(Runnable runnable);
|
void ifNothing(Runnable runnable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matches the Maybe, either just or nothing, and performs either the Consumer, for Just, or Runnable for nothing.
|
||||||
|
*
|
||||||
|
* @param justMatcher the Consumer to pass the value of a Just to
|
||||||
|
* @param nothingMatcher the Runnable to call if the Maybe is a Nothing
|
||||||
|
*/
|
||||||
|
void match(Consumer<T> justMatcher, Runnable nothingMatcher);
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ import java.util.stream.Stream;
|
||||||
* @param <T> the type of the missing content
|
* @param <T> the type of the missing content
|
||||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("methodcount")
|
||||||
final class Nothing<T> implements Maybe<T> {
|
final class Nothing<T> implements Maybe<T> {
|
||||||
|
|
||||||
static final Maybe<?> INSTANCE = new Nothing<>();
|
static final Maybe<?> INSTANCE = new Nothing<>();
|
||||||
|
@ -79,6 +80,11 @@ final class Nothing<T> implements Maybe<T> {
|
||||||
runnable.run();
|
runnable.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void match(final Consumer<T> justMatcher, final Runnable nothingMatcher) {
|
||||||
|
nothingMatcher.run();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void orElseThrow(final Supplier<Exception> e) throws Exception {
|
public void orElseThrow(final Supplier<Exception> e) throws Exception {
|
||||||
throw e.get();
|
throw e.get();
|
||||||
|
|
|
@ -22,7 +22,7 @@ public class MaybeTest implements WithAssertions {
|
||||||
@Test
|
@Test
|
||||||
public void justMustBeNonNull() {
|
public void justMustBeNonNull() {
|
||||||
assertThatNullPointerException().isThrownBy(() -> just(null))
|
assertThatNullPointerException().isThrownBy(() -> just(null))
|
||||||
.withMessageContaining("value");
|
.withMessageContaining("value");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -84,7 +84,7 @@ public class MaybeTest implements WithAssertions {
|
||||||
public void toOptional() {
|
public void toOptional() {
|
||||||
assertThat(just(1).toOptional()).isEqualTo(Optional.of(1));
|
assertThat(just(1).toOptional()).isEqualTo(Optional.of(1));
|
||||||
assertThat(nothing()
|
assertThat(nothing()
|
||||||
.toOptional()).isEqualTo(Optional.empty());
|
.toOptional()).isEqualTo(Optional.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -194,4 +194,29 @@ public class MaybeTest implements WithAssertions {
|
||||||
assertThat(capture).isTrue();
|
assertThat(capture).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void just_whenMatch_thenJustTriggers() {
|
||||||
|
//given
|
||||||
|
final Maybe<Integer> maybe = Maybe.just(1);
|
||||||
|
//then
|
||||||
|
maybe.match(
|
||||||
|
just -> assertThat(just).isEqualTo(1),
|
||||||
|
() -> fail("Not nothing")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nothing_whenMatch_thenNothingTriggers() {
|
||||||
|
//given
|
||||||
|
final Maybe<Integer> maybe = Maybe.nothing();
|
||||||
|
final AtomicBoolean flag = new AtomicBoolean(false);
|
||||||
|
//when
|
||||||
|
maybe.match(
|
||||||
|
just -> fail("Not a just"),
|
||||||
|
() -> flag.set(true)
|
||||||
|
);
|
||||||
|
//then
|
||||||
|
assertThat(flag).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue