Add Maybe.or(Supplier<Maybe>)
Like a flatMap for the negative case.
This commit is contained in:
parent
078e1463f4
commit
1ca686a3f6
6 changed files with 49 additions and 0 deletions
|
@ -4,6 +4,7 @@ CHANGELOG
|
||||||
0.12.0
|
0.12.0
|
||||||
------
|
------
|
||||||
|
|
||||||
|
* Add Maybe.or(Supplier<Maybe>)
|
||||||
* Add Result.reduce(Result,BinaryOperator)
|
* Add Result.reduce(Result,BinaryOperator)
|
||||||
* Rename Result.invert() as Result.swap()
|
* Rename Result.invert() as Result.swap()
|
||||||
* [admin] pom: update urls to github
|
* [admin] pom: update urls to github
|
||||||
|
|
10
README.org
10
README.org
|
@ -444,6 +444,16 @@
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
|
|
||||||
|
**** =T or(Supplier<Maybe<T> alternative)=
|
||||||
|
|
||||||
|
Provide an alternative Maybe to use when Maybe is Nothing.
|
||||||
|
|
||||||
|
#+BEGIN_SRC java
|
||||||
|
final Maybe<Integer> value = Maybe.maybe(getValue())
|
||||||
|
.or(() -> Maybe.just(defaultValue));
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
|
||||||
**** =void orElseThrow(Supplier<Exception> error)=
|
**** =void orElseThrow(Supplier<Exception> error)=
|
||||||
|
|
||||||
Throw the exception if the Maybe is a Nothing.
|
Throw the exception if the Maybe is a Nothing.
|
||||||
|
|
|
@ -118,6 +118,11 @@ final class Just<T> implements Maybe<T> {
|
||||||
justMatcher.accept(value);
|
justMatcher.accept(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Maybe<T> or(final Supplier<Maybe<T>> alternative) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stream<T> stream() {
|
public Stream<T> stream() {
|
||||||
return Stream.of(value);
|
return Stream.of(value);
|
||||||
|
|
|
@ -177,4 +177,13 @@ public interface Maybe<T> extends Functor<T, Maybe<?>> {
|
||||||
* @param nothingMatcher the Runnable to call if the Maybe is a Nothing
|
* @param nothingMatcher the Runnable to call if the Maybe is a Nothing
|
||||||
*/
|
*/
|
||||||
void match(Consumer<T> justMatcher, Runnable nothingMatcher);
|
void match(Consumer<T> 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<T> or(Supplier<Maybe<T>> alternative);
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,6 +100,11 @@ final class Nothing<T> implements Maybe<T> {
|
||||||
nothingMatcher.run();
|
nothingMatcher.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Maybe<T> or(final Supplier<Maybe<T>> alternative) {
|
||||||
|
return alternative.get();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stream<T> stream() {
|
public Stream<T> stream() {
|
||||||
return Stream.empty();
|
return Stream.empty();
|
||||||
|
|
|
@ -292,4 +292,23 @@ public class MaybeTest implements WithAssertions {
|
||||||
assertThat(isNothing).isTrue();
|
assertThat(isNothing).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void just_or_ignoreTheAlternative() {
|
||||||
|
//given
|
||||||
|
final Maybe<String> one = Maybe.just("one");
|
||||||
|
//when
|
||||||
|
final Maybe<String> result = one.or(() -> Maybe.just("two"));
|
||||||
|
//then
|
||||||
|
assertThat(result.toOptional()).contains("one");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nothing_or_isTheAlternative() {
|
||||||
|
//given
|
||||||
|
final Maybe<String> one = Maybe.nothing();
|
||||||
|
//when
|
||||||
|
final Maybe<String> result = one.or(() -> Maybe.just("two"));
|
||||||
|
//then
|
||||||
|
assertThat(result.toOptional()).contains("two");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue