2018-07-14 20:12:06 +01:00
|
|
|
* Mon
|
|
|
|
|
|
|
|
** TypeAlias, Maybe and Result for Java.
|
|
|
|
|
|
|
|
[[https://oss.sonatype.org/content/repositories/releases/net/kemitix/mon][file:https://img.shields.io/nexus/r/https/oss.sonatype.org/net.kemitix/mon.svg?style=for-the-badge]]
|
|
|
|
[[https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.kemitix%22%20AND%20a%3A%22mon%22][file:https://img.shields.io/maven-central/v/net.kemitix/mon.svg?style=for-the-badge]]
|
|
|
|
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://img.shields.io/sonar/https/sonarcloud.io/net.kemitix%3Amon/coverage.svg?style=for-the-badge#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://img.shields.io/sonar/https/sonarcloud.io/net.kemitix%3Amon/tech_debt.svg?style=for-the-badge#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=sqale_rating#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=alert_status#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=reliability_rating#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=security_rating#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=sqale_index#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=vulnerabilities#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=bugs#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=code_smells#.svg]]
|
|
|
|
[[https://sonarcloud.io/dashboard?id=net.kemitix%3Amon][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Amon&metric=ncloc#.svg]]
|
|
|
|
|
|
|
|
[[https://app.codacy.com/project/kemitix/mon/dashboard][file:https://img.shields.io/codacy/grade/d57096b0639d496aba9a7e43e7cf5b4c.svg?style=for-the-badge]]
|
|
|
|
[[http://i.jpeek.org/net.kemitix/mon/index.html][file:http://i.jpeek.org/net.kemitix/mon/badge.svg]]
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
|
2018-07-14 20:12:06 +01:00
|
|
|
** Maven
|
|
|
|
|
|
|
|
#+BEGIN_SRC xml
|
|
|
|
<dependency>
|
|
|
|
<groupId>net.kemitix</groupId>
|
|
|
|
<artifactId>mon</artifactId>
|
|
|
|
<version>RELEASE</version>
|
|
|
|
</dependency>
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
The latest version should be shown above with the nexus and maven-central
|
|
|
|
badges or can be found on [[https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.kemitix%22%20AND%20a%3A%22mon%22][Maven Central]].
|
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
|
|
|
|
** TypeAlias
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
In Haskell it is possible to create an alias for a Type, and to then use
|
|
|
|
that alias with the same behaviour as the original, except that the compiler
|
|
|
|
doesn't treat the alias as the same Type and will generate compiler errors
|
|
|
|
if you try and use them together. e.g.:
|
|
|
|
|
|
|
|
#+BEGIN_SRC haskell
|
|
|
|
type PhoneNumber = String
|
|
|
|
type Name = String
|
|
|
|
type PhoneBook = [(Name,PhoneNumber)]
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
In Java we don't have the ability to have that true alias, so TypeAlias is
|
|
|
|
more of a type-wrapper. It's as close as I could get to a Haskell type alias
|
|
|
|
in Java.
|
|
|
|
|
|
|
|
The benefits of using TypeAlias are:
|
|
|
|
|
|
|
|
- encapsulation of the wrapped type when passing references through code
|
|
|
|
that doesn't need to access the actual value, but only to pass it on
|
|
|
|
- type-safe parameters where you would otherwise be passing Strings,
|
|
|
|
Integers, Lists, or other general classes
|
|
|
|
- equality and hashcode
|
|
|
|
- less verbose than implementing your own
|
|
|
|
|
|
|
|
*TypeAlias Example:*
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
class PhoneNumber extends TypeAlias<String> {
|
|
|
|
private PhoneNumber(final String value) {
|
|
|
|
super(value);
|
|
|
|
}
|
|
|
|
public static PhoneNumber of(final String phoneNumber) {
|
|
|
|
return new PhoneNumber(phoneNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
*Roll your own:*
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
class PhoneNumber {
|
|
|
|
private final String value;
|
|
|
|
private PhoneNumber(final String value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
public static PhoneNumber of(final String phoneNumber) {
|
|
|
|
return new PhoneNumber(phoneNumber);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
PhoneNumber that = (PhoneNumber) o;
|
|
|
|
return Objects.equals(value, that.value);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return Objects.hash(value);
|
|
|
|
}
|
|
|
|
public String getValue() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
*Lombok:*
|
|
|
|
|
|
|
|
Although, if you are using Lombok, that can be equally terse, both it and
|
|
|
|
TypeAlias<String> coming in at 8 lines each, compared to 24 for rolling your
|
|
|
|
own:
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
@Value
|
|
|
|
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
|
|
|
class PhoneNumber {
|
|
|
|
private final String value;
|
|
|
|
public static PhoneNumber of(final String phoneNumber) {
|
|
|
|
return new PhoneNumber(phoneNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
2018-07-16 20:56:56 +01:00
|
|
|
*** =TypeAlias= *can* be a Monad
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
package net.kemitix.mon;
|
|
|
|
|
|
|
|
import org.assertj.core.api.WithAssertions;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
public class TypeAliasMonadTest implements WithAssertions {
|
|
|
|
|
|
|
|
private final int v = 1;
|
|
|
|
private final Function<Integer, AnAlias<Integer>> f = i -> a(i * 2);
|
|
|
|
private final Function<Integer, AnAlias<Integer>> g = i -> a(i + 6);
|
|
|
|
|
|
|
|
private static AnAlias<Integer> a(Integer v) {
|
|
|
|
return AnAlias.of(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void leftIdentity() {
|
|
|
|
assertThat(
|
|
|
|
a(v).flatMap(f)
|
|
|
|
).isEqualTo(
|
|
|
|
f.apply(v)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void rightIdentity_inline() {
|
|
|
|
// java isn't able to properly infer the correct types when used in-line
|
|
|
|
assertThat(
|
|
|
|
a(v).<Integer, AnAlias<Integer>>flatMap(x -> a(x))
|
|
|
|
).isEqualTo(
|
|
|
|
a(v)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void rightIdentity_explicitValue() {
|
|
|
|
final AnAlias<Integer> integerAnAlias = a(v).flatMap(x -> a(x));
|
|
|
|
assertThat(
|
|
|
|
integerAnAlias
|
|
|
|
).isEqualTo(
|
|
|
|
a(v)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void associativity() {
|
|
|
|
assertThat(
|
|
|
|
a(v).flatMap(f).flatMap(g)
|
|
|
|
).isEqualTo(
|
|
|
|
a(v).flatMap(x -> f.apply(x).flatMap(g))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static class AnAlias<T> extends TypeAlias<T> {
|
|
|
|
private AnAlias(T value) {
|
|
|
|
super(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static <T> AnAlias<T> of(T value) {
|
|
|
|
return new AnAlias<>(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
*** Instance Methods
|
|
|
|
|
|
|
|
**** =final <R> R map(final Function<T, R> f)=
|
|
|
|
|
|
|
|
Map the TypeAlias into another value.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final StudentId studentId = StudentId.of(123);
|
|
|
|
final String idString = studentId.map(id -> String.valueOf(id));
|
|
|
|
|
|
|
|
class StudentId extends TypeAlias<Integer> {
|
|
|
|
private StudentId(Integer value) {
|
|
|
|
super(value);
|
|
|
|
}
|
|
|
|
static StudentId of(Integer id) {
|
|
|
|
return new StudentId(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
**** =final <R, U extends TypeAlias<R>> U flatMap(final Function<T, U> f)=
|
|
|
|
|
|
|
|
Map the TypeAlias into another TypeAlias.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final StudentId studentId = StudentId.of(123);
|
|
|
|
final StudentName studentName = studentId.flatMap(id -> getStudentName(id));
|
|
|
|
|
|
|
|
class StudentName extends TypeAlias<String> {
|
|
|
|
private StudentName(String value) {
|
|
|
|
super(value);
|
|
|
|
}
|
|
|
|
static StudentName of(final String name) {
|
|
|
|
return new StudentName(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
**** =T getValue()=
|
|
|
|
|
|
|
|
Get the value of the TypeAlias.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final String name = studentName.getValue();
|
|
|
|
#+END_SRC
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
** Maybe
|
|
|
|
|
|
|
|
Allows specifying that a value may or may not be present. Similar to
|
|
|
|
=Optional=. =Maybe= provides additional methods that =Optional= doesn't:
|
|
|
|
=isNothing()=, =stream()=, =ifNothing()= and =match()=. =Maybe= does not
|
|
|
|
have a =get()= method.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
Unlike =Optional=, when a =map()= results in a =null=, the =Maybe= will
|
|
|
|
continue to be a =Just=. =Optional= would switch to being empty. [[http://blog.vavr.io/the-agonizing-death-of-an-astronaut/][vavi.io
|
|
|
|
follows the same behaviour as =Maybe=]].
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
import net.kemitix.mon.maybe.Maybe;
|
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
|
|
class MaybeExample {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Maybe.just(countArgs(args))
|
|
|
|
.filter(isEven())
|
|
|
|
.map(validMessage())
|
|
|
|
.match(
|
|
|
|
just -> System.out.println(just),
|
|
|
|
() -> System.out.println("Not an valid value")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Function<Integer, String> validMessage() {
|
|
|
|
return v -> String.format("Value %d is even", v);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Predicate<Integer> isEven() {
|
|
|
|
return v -> v % 2 == 0;
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
private static Integer countArgs(String[] args) {
|
|
|
|
return args.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
In the above example, the number of command line arguments are counted, if
|
|
|
|
there are an even number of them then a message is created and printed by
|
|
|
|
the Consumer parameter in the =match= call. If there is an odd number of
|
|
|
|
arguments, then the filter will return =Maybe.nothing()=, meaning that the
|
|
|
|
=nothing= drops straight through the map and triggers the Runnable parameter
|
|
|
|
in the =match= call.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
*** =Maybe= is a Monad:
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
package net.kemitix.mon;
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
import net.kemitix.mon.maybe.Maybe;
|
|
|
|
import org.assertj.core.api.WithAssertions;
|
|
|
|
import org.junit.Test;
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
import java.util.function.Function;
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
public class MaybeMonadTest implements WithAssertions {
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
private final int v = 1;
|
|
|
|
private final Function<Integer, Maybe<Integer>> f = i -> m(i * 2);
|
|
|
|
private final Function<Integer, Maybe<Integer>> g = i -> m(i + 6);
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
private static Maybe<Integer> m(int value) {
|
|
|
|
return Maybe.maybe(value);
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
@Test
|
|
|
|
public void leftIdentity() {
|
|
|
|
assertThat(
|
|
|
|
m(v).flatMap(f)
|
|
|
|
).isEqualTo(
|
|
|
|
f.apply(v)
|
|
|
|
);
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
@Test
|
|
|
|
public void rightIdentity() {
|
|
|
|
assertThat(
|
|
|
|
m(v).flatMap(x -> m(x))
|
|
|
|
).isEqualTo(
|
|
|
|
m(v)
|
|
|
|
);
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
@Test
|
|
|
|
public void associativity() {
|
|
|
|
assertThat(
|
|
|
|
m(v).flatMap(f).flatMap(g)
|
|
|
|
).isEqualTo(
|
|
|
|
m(v).flatMap(x -> f.apply(x).flatMap(g))
|
|
|
|
);
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
}
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
*** Static Constructors
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =static <T> Maybe<T> maybe(T value)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Create a Maybe for the value that may or may not be present.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Where the value is =null=, that is taken as not being present.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Integer> just = Maybe.maybe(1);
|
|
|
|
final Maybe<Integer> nothing = Maybe.maybe(null);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =static <T> Maybe<T> just(T value)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Create a Maybe for the value that is present.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
The =value= must not be =null= or a =NullPointerException= will be thrown.
|
|
|
|
If you can't prove that the value won't be =null= you should use
|
|
|
|
=Maybe.maybe(value)= instead.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Integer> just = Maybe.just(1);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =static <T> Maybe<T> nothing()=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Create a Maybe for a lack of a value.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Integer> nothing = Maybe.nothing();
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
*** Instance Methods
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =Maybe<T> filter(Predicate<T> predicate)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Filter a Maybe by the predicate, replacing with Nothing when it fails.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Integer> maybe = Maybe.maybe(getValue())
|
|
|
|
.filter(v -> v % 2 == 0);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =<R> Maybe<R> map(Function<T,R> f)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Applies the function to the value within the Maybe, returning the result within another Maybe.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Integer> maybe = Maybe.maybe(getValue())
|
|
|
|
.map(v -> v * 100);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =<R> Maybe<R> flatMap(Function<T,Maybe<R>> f)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Applies the function to the value within the =Maybe=, resulting in another =Maybe=, then flattens the resulting =Maybe<Maybe<T>>= into =Maybe<T>=.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Monad binder maps the Maybe into another Maybe using the binder method f
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Integer> maybe = Maybe.maybe(getValue())
|
|
|
|
.flatMap(v -> Maybe.maybe(getValueFor(v)));
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =void match(Consumer<T> just, Runnable nothing)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Matches the Maybe, either just or nothing, and performs either the Consumer, for Just, or Runnable for nothing.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
Maybe.maybe(getValue())
|
|
|
|
.match(
|
|
|
|
just -> workWithValue(just),
|
|
|
|
() -> nothingToWorkWith()
|
|
|
|
);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =T orElse(T otherValue)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
A value to use when Maybe is Nothing.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Integer value = Maybe.maybe(getValue())
|
|
|
|
.orElse(1);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =T orElseGet(Supplier<T> otherValueSupplier)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Provide a value to use when Maybe is Nothing.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Integer value = Maybe.maybe(getValue())
|
|
|
|
.orElseGet(() -> getDefaultValue());
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
2018-08-31 22:53:06 +01:00
|
|
|
**** =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
|
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =void orElseThrow(Supplier<Exception> error)=
|
|
|
|
|
|
|
|
Throw the exception if the Maybe is a Nothing.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Integer value = Maybe.maybe(getValue())
|
|
|
|
.orElseThrow(() -> new RuntimeException("error"));
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
**** =Maybe<T> peek(Consumer<T> consumer)=
|
|
|
|
|
|
|
|
Provide the value within the Maybe, if it exists, to the Consumer, and returns this Maybe. Conceptually equivalent to the idea of =ifPresent(...)=.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Integer> maybe = Maybe.maybe(getValue())
|
|
|
|
.peek(v -> v.foo());
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
**** =void ifNothing(Runnable runnable)=
|
|
|
|
|
|
|
|
Run the runnable if the Maybe is a Nothing, otherwise do nothing.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
Maybe.maybe(getValue())
|
|
|
|
.ifNothing(() -> doSomething());
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
**** =Stream<T> stream()=
|
|
|
|
|
|
|
|
Converts the Maybe into either a single value stream or an empty stream.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Stream<Integer> stream = Maybe.maybe(getValue())
|
|
|
|
.stream();
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =boolean isJust()=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Checks if the Maybe is a Just.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final boolean isJust = Maybe.maybe(getValue())
|
|
|
|
.isJust();
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
**** =boolean isNothing()=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Checks if the Maybe is Nothing.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final boolean isNothing = Maybe.maybe(getValue())
|
|
|
|
.isNothing();
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =Optional<T> toOptional()=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Convert the Maybe to an Optional.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Optional<Integer> optional = Maybe.maybe(getValue())
|
|
|
|
.toOptional();
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
** Result
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
Allows handling error conditions without the need to catch exceptions.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
When a =Result= is returned from a method it will contain one of two values.
|
|
|
|
Either the actual result, or an error in the form of an =Exception=. The
|
|
|
|
exception is returned within the =Result= and is not thrown.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
import net.kemitix.mon.result.Result;
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
import java.io.IOException;
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
class ResultExample implements Runnable {
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
public static void main(final String[] args) {
|
|
|
|
new ResultExample().run();
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Result.of(() -> callRiskyMethod())
|
|
|
|
.flatMap(state -> doSomething(state))
|
|
|
|
.match(
|
|
|
|
success -> System.out.println(success),
|
|
|
|
error -> error.printStackTrace()
|
|
|
|
);
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
private String callRiskyMethod() throws IOException {
|
|
|
|
return "I'm fine";
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
private Result<String> doSomething(final String state) {
|
|
|
|
return Result.of(() -> state + ", it's all good.");
|
|
|
|
}
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
}
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 18:52:33 +01:00
|
|
|
In the above example the string ="I'm fine"= is returned by
|
|
|
|
=callRiskyMethod()= within a successful =Result=. The =.flatMap()= call,
|
|
|
|
unwraps that =Result= and, as it is a success, passes the contents to
|
|
|
|
=doSomething()=, which in turn returns a =Result= that the =.flatMap()= call
|
|
|
|
returns. =match()= is called on the =Result= and, being a success, will call
|
|
|
|
the success =Consumer=.
|
|
|
|
|
|
|
|
Had =callRiskyMethod()= thrown an exception it would have been caught by the
|
|
|
|
=Result.of()= method which would have then been an error =Result=. An error
|
|
|
|
Result would have ignored the =flatMap= and skipped to the =match()= when it
|
|
|
|
would have called the error =Consumer=.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
*** =Result= is a Monad
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
package net.kemitix.mon;
|
|
|
|
|
|
|
|
import net.kemitix.mon.result.Result;
|
|
|
|
import org.assertj.core.api.WithAssertions;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
public class ResultMonadTest implements WithAssertions {
|
|
|
|
|
|
|
|
private final int v = 1;
|
|
|
|
private final Function<Integer, Result<Integer>> f = i -> r(i * 2);
|
|
|
|
private final Function<Integer, Result<Integer>> g = i -> r(i + 6);
|
|
|
|
|
|
|
|
private static Result<Integer> r(int v) {
|
|
|
|
return Result.ok(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void leftIdentity() {
|
|
|
|
assertThat(
|
|
|
|
r(v).flatMap(f)
|
|
|
|
).isEqualTo(
|
|
|
|
f.apply(v)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void rightIdentity() {
|
|
|
|
assertThat(
|
|
|
|
r(v).flatMap(x -> r(x))
|
|
|
|
).isEqualTo(
|
|
|
|
r(v)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void associativity() {
|
|
|
|
assertThat(
|
|
|
|
r(v).flatMap(f).flatMap(g)
|
|
|
|
).isEqualTo(
|
|
|
|
r(v).flatMap(x -> f.apply(x).flatMap(g))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
*** Static Constructors
|
|
|
|
|
|
|
|
**** =static <T> Result<T> of(Callable<T> callable)=
|
|
|
|
|
|
|
|
Create a Result for a output of the Callable.
|
|
|
|
|
|
|
|
If the Callable throws and Exception, then the Result will be an error and
|
|
|
|
will contain that exception.
|
|
|
|
|
|
|
|
This will be the main starting point for most Results where the callable
|
|
|
|
could throw an =Exception=.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
2018-07-16 19:07:19 +01:00
|
|
|
final Result<Integer> okay = Result.of(() -> 1);
|
|
|
|
final Result<Integer> error = Result.of(() -> {throw new RuntimeException();});
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
**** =static <T> Result<T> ok(T value)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Create a Result for a success.
|
|
|
|
|
|
|
|
Use this where you have a value that you want to place into the Result context.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<Integer> okay = Result.ok(1);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =static <T> Result<T> error(Throwable error)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Create a Result for an error.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<Integer> error = Result.error(new RuntimeException());
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
*** Static Methods
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
These static methods provide integration with the =Maybe= class.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
2018-07-14 20:12:06 +01:00
|
|
|
#+END_SRC
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =static <T> Maybe<T> toMaybe(Result<T> result)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Creates a =Maybe= from the =Result=, where the =Result= is a success, then
|
|
|
|
the =Maybe= will contain the value. However, if the =Result= is an error
|
|
|
|
then the =Maybe= will be nothing.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<Integer> result = Result.of(() -> getValue());
|
|
|
|
final Maybe<Integer> maybe = Result.toMaybe(result);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =static <T> Result<T> fromMaybe(Maybe<T> maybe, Supplier<Throwable> error)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Creates a =Result= from the =Maybe=, where the =Result= will be an error
|
|
|
|
if the =Maybe= is nothing. Where the =Maybe= is nothing, then the
|
|
|
|
=Supplier<Throwable>= will provide the error for the =Result=.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Integer> maybe = Maybe.maybe(getValue());
|
|
|
|
final Result<Integer> result = Result.fromMaybe(maybe, () -> new NoSuchFileException("filename"));
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =static <T> Result<Maybe<T>> invert(Maybe<Result<T>> maybeResult)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Swaps the =Result= within a =Maybe=, so that =Result= contains a =Maybe=.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Maybe<Result<Integer>> maybe = Maybe.maybe(Result.of(() -> getValue()));
|
|
|
|
final Result<Maybe<Integer>> result = Result.invert(maybe);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =static <T,R> Result<Maybe<R>> flatMapMaybe(Result<Maybe<T>> maybeResult, Function<Maybe<T>,Result<Maybe<R>>> f)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Applies the function to the contents of a Maybe within the Result.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<Maybe<Integer>> result = Result.of(() -> Maybe.maybe(getValue()));
|
|
|
|
final Result<Maybe<Integer>> maybeResult = Result.flatMapMaybe(result, maybe -> Result.of(() -> maybe.map(v -> v * 2)));
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
*** Instance Methods
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =<R> Result<R> map(Function<T,R> f)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Applies the function to the value within the Functor, returning the result
|
|
|
|
within a Functor.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<String> result = Result.of(() -> getValue())
|
|
|
|
.map(v -> String.valueOf(v));
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =<R> Result<R> flatMap(Function<T,Result<R>> f)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Returns a new Result consisting of the result of applying the function to
|
|
|
|
the contents of the Result.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<String> result = Result.of(() -> getValue())
|
|
|
|
.flatMap(v -> Result.of(() -> String.valueOf(v)));
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =<R> Result<R> andThen(Function<T,Callable<R>> f)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Maps a Success Result to another Result using a Callable that is able to
|
|
|
|
throw a checked exception.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<String> result = Result.of(() -> getValue())
|
|
|
|
.andThen(v -> () -> {throw new IOException();});
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =void match(Consumer<T> onSuccess, Consumer<Throwable> onError)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Matches the Result, either success or error, and supplies the appropriate
|
|
|
|
Consumer with the value or error.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
Result.of(() -> getValue())
|
|
|
|
.match(
|
|
|
|
success -> System.out.println(success),
|
|
|
|
error -> System.err.println("error")
|
|
|
|
);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =Result<T> recover(Function<Throwable,Result<T>> f)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Provide a way to attempt to recover from an error state.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<Integer> result = Result.of(() -> getValue())
|
|
|
|
.recover(e -> Result.of(() -> getSafeValue(e)));
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =Result<T> peek(Consumer<T> consumer)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Provide the value within the Result, if it is a success, to the Consumer,
|
|
|
|
and returns this Result.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<Integer> result = Result.of(() -> getValue())
|
|
|
|
.peek(v -> System.out.println(v));
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =Result<T> thenWith(Function<T,WithResultContinuation<T>> f)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Perform the continuation with the current Result value then return the
|
|
|
|
current Result, assuming there was no error in the continuation.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
2018-07-14 20:12:06 +01:00
|
|
|
final Result<Integer> result = Result.of(() -> getValue())
|
2018-07-16 19:07:19 +01:00
|
|
|
.thenWith(v -> () -> System.out.println(v))
|
|
|
|
.thenWith(v -> () -> {throw new IOException();});
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =Result<Maybe<T>> maybe(Predicate<T> predicate)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Wraps the value within the Result in a Maybe, either a Just if the
|
|
|
|
predicate is true, or Nothing.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Result<Maybe<Integer>> result = Result.of(() -> getValue())
|
|
|
|
.maybe(v -> v % 2 == 0);
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =T orElseThrow()=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Extracts the successful value from the result, or throws the error
|
|
|
|
Throwable.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Integer result = Result.of(() -> getValue())
|
|
|
|
.orElseThrow();
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =void onError(Consumer<Throwable> errorConsumer)=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
A handler for error states.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
Result.of(() -> getValue())
|
|
|
|
.onError(e -> handleError(e));
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =boolean isOkay()=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Checks if the Result is a success.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final boolean isOkay = Result.of(() -> getValue())
|
|
|
|
.isOkay();
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
**** =boolean isError()=
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
Checks if the Result is an error.
|
2018-07-14 20:12:06 +01:00
|
|
|
|
2018-07-16 19:07:19 +01:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final boolean isError = Result.of(() -> getValue())
|
|
|
|
.isError();
|
|
|
|
#+END_SRC
|
2018-07-14 20:12:06 +01:00
|
|
|
|
|
|
|
|
2018-07-16 19:31:18 +01:00
|
|
|
** Either
|
|
|
|
|
|
|
|
Allows handling a value that can be one of two types, a left value/type or a
|
|
|
|
right value/type.
|
|
|
|
|
|
|
|
When an =Either= is returned from a method it will contain either a left or a
|
|
|
|
right.
|
|
|
|
|
|
|
|
Where the =Either= is used to represent success/failure, the left case is, by
|
|
|
|
convention, used to indicate the error, and right the success. An alternative
|
|
|
|
is to use the =Result= which more clearly distinguishes success from failure.
|
|
|
|
|
2018-07-16 20:56:56 +01:00
|
|
|
*** =Either= *is not* a Monad.
|
2018-07-16 19:31:18 +01:00
|
|
|
|
|
|
|
*** Static Constructors
|
|
|
|
|
|
|
|
**** =static <L, R> Either<L, R> left(final L l)=
|
|
|
|
|
|
|
|
Create a new Either holding a left value.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Either<Integer, String> left = Either.left(getIntegerValue());
|
|
|
|
#+END_SRC
|
|
|
|
|
2018-07-16 20:56:56 +01:00
|
|
|
|
2018-07-16 19:31:18 +01:00
|
|
|
**** =static <L, R> Either<L, R> right(final R r)=
|
|
|
|
|
|
|
|
Create a new Either holding a right value.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Either<Integer, String> right = Either.right(getStringValue());
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
*** Instance Methods
|
|
|
|
|
|
|
|
**** =boolean isLeft()=
|
|
|
|
|
|
|
|
Checks if the Either holds a left value.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final boolean leftIsLeft = Either.<Integer, String>left(getIntegerValue()).isLeft();
|
|
|
|
final boolean rightIsLeft = Either.<Integer, String>right(getStringValue()).isLeft();
|
|
|
|
#+END_SRC
|
|
|
|
|
2018-07-16 20:56:56 +01:00
|
|
|
|
2018-07-16 19:31:18 +01:00
|
|
|
**** =boolean isRight()=
|
|
|
|
|
|
|
|
Checks if the Either holds a right value.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final boolean leftIsRight = Either.<Integer, String>left(getIntegerValue()).isRight();
|
|
|
|
final boolean rightIsRight = Either.<Integer, String>right(getStringValue()).isRight();
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
|
|
|
|
**** =void match(Consumer<L> onLeft, Consumer<R> onRight)=
|
|
|
|
|
|
|
|
Matches the Either, invoking the correct Consumer.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
Either.<Integer, String>left(getIntegerValue())
|
|
|
|
.match(
|
|
|
|
left -> handleIntegerValue(left),
|
|
|
|
right -> handleStringValue(right)
|
|
|
|
);
|
|
|
|
#+END_SRC
|
|
|
|
|
2018-07-16 20:56:56 +01:00
|
|
|
|
2018-07-16 19:31:18 +01:00
|
|
|
**** =<T> Either<T, R> mapLeft(Function<L, T> f)=
|
|
|
|
|
|
|
|
Map the function across the left value.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Either<Double, String> either = Either.<Integer, String>left(getIntegerValue())
|
|
|
|
.mapLeft(i -> i.doubleValue());
|
|
|
|
#+END_SRC
|
|
|
|
|
2018-07-16 20:56:56 +01:00
|
|
|
|
2018-07-16 19:31:18 +01:00
|
|
|
**** =<T> Either<L, T> mapRight(Function<R, T> f)=
|
|
|
|
|
|
|
|
Map the function across the right value.
|
|
|
|
|
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Either<Integer, String> either = Either.<Integer, String>left(getIntegerValue())
|
|
|
|
.mapRight(s -> s + "x");
|
|
|
|
#+END_SRC
|
2018-07-16 20:56:56 +01:00
|
|
|
|
|
|
|
|