Functional-style if-then-else
Find a file
dependabot[bot] d56649f41c Bump kemitix-parent from 5.1.1 to 5.2.0 (#39)
* Bump kemitix-parent from 5.1.1 to 5.2.0

Bumps [kemitix-parent](https://gitlab.com/kemitix/kemitix-parent) from 5.1.1 to 5.2.0.
- [Release notes](https://gitlab.com/kemitix/kemitix-parent/tags)
- [Commits](https://gitlab.com/kemitix/kemitix-parent/commits/master)

Signed-off-by: dependabot[bot] <support@dependabot.com>

* [changelog] update
2018-10-10 22:52:47 +01:00
src Remove deprecated methods 2018-08-25 15:39:43 +01:00
.gitignore Initial commit 2017-04-21 10:55:11 +01:00
CHANGELOG Bump kemitix-parent from 5.1.1 to 5.2.0 (#39) 2018-10-10 22:52:47 +01:00
Jenkinsfile.groovy Bump kemitix-maven-tiles from 0.9.0 to 1.3.1 (#40) 2018-10-10 22:43:07 +01:00
LICENSE.txt Initial commit 2017-04-21 10:55:11 +01:00
lombok.config lombok.config: added to prevent generated annotation 2018-03-04 18:18:00 +00:00
pom.xml Bump kemitix-parent from 5.1.1 to 5.2.0 (#39) 2018-10-10 22:52:47 +01:00
README.org README: update Value examples for 0.6.0 2018-07-28 20:23:17 +01:00

Conditional

Maven

<dependency>
    <groupId>net.kemitix</groupId>
    <artifactId>conditional</artifactId>
    <version>RELEASE</version>
</dependency>

The latest version should be shown above with the nexus and maven-central badges or can be found on Maven Central.

Condition

The `if … then` and `if … then … else` constructs can't be used in a lambda without being wrapped in a code block.

Using `Condition` allows the use of an if-style expression without needing to wrap it in a code block

if-then

if (isTrue()) {
    doSomething();
}

Condition.where(isTrue())
         .then(() -> doSomething());

if-then-else

if (isTrue()) {
    doSomething();
} else {
    doSomethingElse();
}

Condition.where(isTrue())
         .then(() -> doSomething())
         .otherwise(() -> doSomethingElse());

isTrue() / isFalse() / not()

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());

flatMap(Function<Boolean, Condition>)

final Condition condition = Condition.where(isTrue())
                                     .flatMap(b -> Condition.where(b));

if-and-then-else

if (isTrue() && isAlsoTrue()) {
    doSomething();
} else {
    doSomethingElse();
}

Condition.where(isTrue())
         .and(() -> isAlsoTrue())
         .then(() -> doSomething())
         .otherwise(() -> doSomethingElse());

if-or-then-else

if (isTrue() || alternativeIsTrue()) {
    doSomething();
} else {
    doSomethingElse();
}

Condition.where(isTrue())
         .or(() -> alternativeIsTrue())
         .then(() -> doSomething())
         .otherwise(() -> doSomethingElse());

if-then-else-if

if (isFalse()) {
    doSomething();
} else if (isTrue()) {
    doSomethingElse();
}

Condition.where(isFalse())
         .then(() -> doSomething())
         .otherwise(() -> isTrue())
         .then(() -> doSomethingElse());

if-then-if-then

if (isTrue()) {
    doSomething();
    if (isAlsoTrue()) {
        doSomethingElse();
    }
}

Condition.where(isTrue())
         .then(() -> doSomething())
         .and(() -> isAlsoTrue())
         .then(() -> doSomethingElse());

Value

Values from an if-then-else in a functional-style.

Functional, and verbose, alternative to the ternary operator (?:).

if-then-else

String result;
if (isTrue()) {
    result = TRUE;
} else {
    result = FALSE;
}

String result = isTrue() ? TRUE : FALSE;

final String result = Value.where(isTrue(), () -> TRUE, () -> FALSE);

final String result = Value.where(Condition.where(isTrue), () -> TRUE, () -> FALSE);

final Optional<String> result = Value.where(isTrue(), () -> TRUE);

final String result = Value.<String>where(isTrue())
                           .not()
                           .then(() -> FALSE)
                           .otherwise(() -> TRUE);

if-and-then-else

String result;
if (isTrue() && alternativeIsTrue()) {
    result = TRUE;
} else {
    result = FALSE;
}

final String result = Value.<String>where(isTrue())
                           .and(() -> alternativeIsTrue())
                           .then(() -> TRUE)
                           .otherwise(() -> FALSE);

if-or-then-else

String result;
if (isTrue() || alternativeIsTrue()) {
    result = TRUE;
} else {
    result = FALSE;
}

final String result = Value.<String>where(isTrue())
                           .or(() -> alternativeIsTrue())
                           .then(() -> TRUE)
                           .otherwise(() -> FALSE);

if-or-not-then-else

String result;
if (isTrue() || !isFalse()) {
result = TRUE;
} else {
result = FALSE;
}

final String result = Value.<String>where(isTrue())
                           .orNot(() -> isFalse())
                           .then(() -> TRUE)
                           .otherwise(() -> FALSE);

if-then

Optional<String> result;
if (isTrue()) {
    result = Optional.of(TRUE);
} else {
    result = Optional.empty();
}

final Optional<String> result = Value.<String>where(isTrue())
                                     .then(() -> TRUE)
                                     .optional();