2017-04-21 12:17:18 +01:00
|
|
|
# Conditional
|
|
|
|
|
2017-08-26 12:51:37 +01:00
|
|
|
image:https://img.shields.io/github/release/kemitix/conditional.svg["Releases", link="https://github.com/kemitix/conditional/releases"]
|
|
|
|
image:https://api.codacy.com/project/badge/Grade/1188742d676e457da91415d2b3a5faf1["Codacy code quality", link="https://www.codacy.com/app/kemitix/conditional"]
|
2017-04-23 21:37:22 +01:00
|
|
|
image:https://travis-ci.org/kemitix/conditional.svg?branch=master["Build Status", link="https://travis-ci.org/kemitix/conditional"]
|
2017-04-24 07:02:07 +01:00
|
|
|
image:https://coveralls.io/repos/github/kemitix/conditional/badge.svg?branch=master["Coverage Status", link="https://coveralls.io/github/kemitix/conditional?branch=master"]
|
2017-04-23 21:37:22 +01:00
|
|
|
|
2017-04-24 07:02:07 +01:00
|
|
|
* link:#condition[Condition]
|
|
|
|
* link:#value[Value]
|
2017-04-21 12:17:18 +01:00
|
|
|
|
2017-04-23 21:37:22 +01:00
|
|
|
## Condition
|
|
|
|
|
|
|
|
If-then-else in a functional-style.
|
2017-04-21 12:17:18 +01:00
|
|
|
|
|
|
|
### if-then
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (isTrue()) {
|
|
|
|
doSomething();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.where(isTrue())
|
|
|
|
.then(() -> doSomething());
|
|
|
|
----
|
|
|
|
|
|
|
|
### if-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (isTrue()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.where(isTrue())
|
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
----
|
|
|
|
|
|
|
|
### if-and-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (isTrue() && isAlsoTrue()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.where(isTrue())
|
2018-03-13 17:53:16 +00:00
|
|
|
.and(() -> isAlsoTrue())
|
2017-04-21 12:17:18 +01:00
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
----
|
|
|
|
|
|
|
|
### if-or-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (isTrue() || alternativeIsTrue()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.where(isTrue())
|
2018-03-13 17:53:16 +00:00
|
|
|
.or(() -> alternativeIsTrue())
|
2017-04-21 12:17:18 +01:00
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
----
|
|
|
|
|
|
|
|
### if-not-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (!isFalse()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.whereNot(isFalse())
|
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
----
|
|
|
|
|
|
|
|
### if-and-not-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (isTrue() || !isFalse()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.where(isTrue())
|
2018-03-13 17:53:16 +00:00
|
|
|
.andNot(() -> isFalse())
|
2017-04-21 12:17:18 +01:00
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
----
|
|
|
|
|
|
|
|
### if-or-not-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (isFalse() || !isAlsoFalse()) {
|
|
|
|
doSomething();
|
|
|
|
} else {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.where(isFalse())
|
2018-03-13 17:53:16 +00:00
|
|
|
.orNot(() -> isAlsoFalse())
|
2017-04-21 12:17:18 +01:00
|
|
|
.then(() -> doSomething())
|
|
|
|
.otherwise(() -> doSomethingElse());
|
|
|
|
----
|
|
|
|
|
2017-04-21 14:28:54 +01:00
|
|
|
### if-then-else-if
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (isFalse()) {
|
|
|
|
doSomething();
|
|
|
|
} else if (isTrue()) {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.where(isFalse())
|
|
|
|
.then(() -> doSomething())
|
2018-03-13 17:53:16 +00:00
|
|
|
.otherwise(() -> isTrue())
|
2017-04-21 14:28:54 +01:00
|
|
|
.then(() -> doSomethingElse());
|
|
|
|
----
|
|
|
|
|
2017-04-21 12:17:18 +01:00
|
|
|
### if-then-if-then
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
if (isTrue()) {
|
|
|
|
doSomething();
|
|
|
|
if (isAlsoTrue()) {
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
Condition.where(isTrue())
|
|
|
|
.then(() -> doSomething())
|
2018-03-13 17:53:16 +00:00
|
|
|
.and(() -> isAlsoTrue())
|
2017-04-21 12:17:18 +01:00
|
|
|
.then(() -> doSomethingElse());
|
|
|
|
----
|
2017-04-23 21:37:22 +01:00
|
|
|
|
|
|
|
## Value
|
|
|
|
|
|
|
|
Values from an if-then-else in a functional-style.
|
|
|
|
|
|
|
|
Functional, and verbose, alternative to the ternary operator (`?:`).
|
|
|
|
|
|
|
|
### if-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
String result;
|
|
|
|
if (isTrue()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
String result = isTrue() ? TRUE : FALSE;
|
|
|
|
----
|
|
|
|
|
2017-08-26 22:58:29 +01:00
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
final String result = Value.where(isTrue(), () -> TRUE, () -> FALSE);
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
final Optional<String> result = Value.where(isTrue(), () -> TRUE);
|
|
|
|
----
|
|
|
|
|
2017-04-23 21:37:22 +01:00
|
|
|
[[source,java]]
|
|
|
|
----
|
2018-03-13 17:53:16 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
2017-04-23 21:37:22 +01:00
|
|
|
----
|
|
|
|
|
|
|
|
### if-not-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
String result;
|
|
|
|
if (!isTrue()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
2018-03-13 17:53:16 +00:00
|
|
|
final String result = Value.<String>whereNot(isTrue())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
2017-04-23 21:37:22 +01:00
|
|
|
----
|
|
|
|
|
|
|
|
### if-and-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
String result;
|
|
|
|
if (isTrue() && alternativeIsTrue()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
2018-03-13 17:53:16 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.and(() -> alternativeIsTrue())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
2017-04-23 21:37:22 +01:00
|
|
|
----
|
|
|
|
|
|
|
|
### if-and-not-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
String result;
|
|
|
|
if (isTrue() && !alternativeIsFalse()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
2018-03-13 17:53:16 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.andNot(() -> alternativeIsFalse())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
2017-04-23 21:37:22 +01:00
|
|
|
----
|
|
|
|
|
|
|
|
### if-or-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
String result;
|
|
|
|
if (isTrue() || alternativeIsTrue()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
2018-03-13 17:53:16 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.or(() -> alternativeIsTrue())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
2017-04-23 21:37:22 +01:00
|
|
|
----
|
|
|
|
|
|
|
|
### if-or-not-then-else
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
|
|
|
String result;
|
|
|
|
if (isTrue() || !isFalse()) {
|
|
|
|
result = TRUE;
|
|
|
|
} else {
|
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
----
|
2018-03-13 17:53:16 +00:00
|
|
|
final String result = Value.<String>where(isTrue())
|
|
|
|
.orNot(() -> isFalse())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.otherwise(() -> FALSE);
|
2017-04-23 21:37:22 +01:00
|
|
|
----
|
2018-03-13 17:53:16 +00:00
|
|
|
|
|
|
|
### if-then
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
-----
|
|
|
|
Optional<String> result;
|
|
|
|
if (isTrue()) {
|
|
|
|
result = Optional.of(TRUE);
|
|
|
|
} else {
|
|
|
|
result = Optional.empty();
|
|
|
|
}
|
|
|
|
-----
|
|
|
|
|
|
|
|
[[source,java]]
|
|
|
|
-----
|
|
|
|
final Optional<String> result = Value.<String>where(isTrue())
|
|
|
|
.then(() -> TRUE)
|
|
|
|
.optional();
|
|
|
|
-----
|