2018-07-28 19:42:35 +01:00
|
|
|
* Conditional
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
* Functional Condition and Value.
|
2018-07-28 19:53:31 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
[[https://oss.sonatype.org/content/repositories/releases/net/kemitix/conditional][file:https://img.shields.io/nexus/r/https/oss.sonatype.org/net.kemitix/conditional.svg?style=for-the-badge]]
|
|
|
|
[[https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.kemitix%22%20AND%20a%3A%22conditional%22][file:https://img.shields.io/maven-central/v/net.kemitix/conditional.svg?style=for-the-badge]]
|
|
|
|
[[http://i.jpeek.org/net.kemitix/conditional/index.html][file:http://i.jpeek.org/net.kemitix/conditional/badge.svg]]
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
* Maven
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC xml
|
|
|
|
<dependency>
|
|
|
|
<groupId>net.kemitix</groupId>
|
|
|
|
<artifactId>conditional</artifactId>
|
|
|
|
<version>RELEASE</version>
|
|
|
|
</dependency>
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
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-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
* Condition
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
The `if ... then` and `if ... then ... else` constructs can't be used in a
|
|
|
|
lambda without being wrapped in a code block.
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Using `Condition` allows the use of an if-style expression without needing to
|
|
|
|
wrap it in a code block
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-then
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
if (isTrue()) {
|
|
|
|
doSomething();
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Condition.where(isTrue())
|
|
|
|
.then(() -> doSomething());
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-then-else
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
if (isTrue()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Condition.where(isTrue())
|
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** isTrue() / isFalse() / not()
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Condition condition = Condition.where(isTrue());
|
|
|
|
final boolean isTrue = condition.isTrue();
|
|
|
|
final boolean isFalse = condition.isFalse();
|
|
|
|
final Condition not = condition.not();
|
|
|
|
final Condition andCondition1 = condition.and(Condition.where(isAlsoTrue()));
|
|
|
|
final Condition andCondition2 = condition.and(isAlsoTrue());
|
|
|
|
final Condition orCondition1 = condition.or(Condition.where(isAlsoTrue()));
|
|
|
|
final Condition orCondition2 = condition.or(isAlsoTrue());
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** flatMap(Function<Boolean, Condition>)
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
final Condition condition = Condition.where(isTrue())
|
|
|
|
.flatMap(b -> Condition.where(b));
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-and-then-else
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
if (isTrue() && isAlsoTrue()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Condition.where(isTrue())
|
|
|
|
.and(() -> isAlsoTrue())
|
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-or-then-else
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
if (isTrue() || alternativeIsTrue()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Condition.where(isTrue())
|
|
|
|
.or(() -> alternativeIsTrue())
|
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-then-else-if
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
if (isFalse()) {
|
|
|
|
doSomething();
|
|
|
|
} else if (isTrue()) {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Condition.where(isFalse())
|
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> isTrue())
|
|
|
|
.then(() -> doSomethingElse());
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-then-if-then
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
if (isTrue()) {
|
|
|
|
doSomething();
|
|
|
|
if (isAlsoTrue()) {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Condition.where(isTrue())
|
|
|
|
.then(() -> doSomething())
|
|
|
|
.and(() -> isAlsoTrue())
|
|
|
|
.then(() -> doSomethingElse());
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
* Value
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Values from an if-then-else in a functional-style.
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
Functional, and verbose, alternative to the ternary operator (=?:=).
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-then-else
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
String result;
|
|
|
|
if (isTrue()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
String result = isTrue() ? TRUE : FALSE;
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
final String result = Value.where(isTrue(), () -> TRUE, () -> FALSE);
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
final String result = Value.where(Condition.where(isTrue), () -> TRUE, () -> FALSE);
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
final Optional<String> result = Value.where(isTrue(), () -> TRUE);
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.not()
|
|
|
|
.then(() -> FALSE)
|
|
|
|
.otherwise(() -> TRUE);
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-and-then-else
|
2018-07-28 20:23:17 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
String result;
|
|
|
|
if (isTrue() && alternativeIsTrue()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.and(() -> alternativeIsTrue())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-or-then-else
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
String result;
|
|
|
|
if (isTrue() || alternativeIsTrue()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.or(() -> alternativeIsTrue())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-or-not-then-else
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
String result;
|
|
|
|
if (isTrue() || !isFalse()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.orNot(() -> isFalse())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
** if-then
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
#+BEGIN_SRC java
|
|
|
|
Optional<String> result;
|
|
|
|
if (isTrue()) {
|
|
|
|
result = Optional.of(TRUE);
|
|
|
|
} else {
|
|
|
|
result = Optional.empty();
|
|
|
|
}
|
2018-07-28 19:42:35 +01:00
|
|
|
|
2019-01-05 13:01:30 +00:00
|
|
|
final Optional<String> result = Value.<String>where(isTrue())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.optional();
|
|
|
|
#+END_SRC
|
2018-07-28 19:42:35 +01:00
|
|
|
|