Condition: overload otherwise to allow if-then-else-if behaviour
This commit is contained in:
parent
cf404b716b
commit
29232d4e71
3 changed files with 57 additions and 0 deletions
19
README.adoc
19
README.adoc
|
@ -131,6 +131,25 @@ Condition.where(isFalse())
|
|||
.otherwise(() -> doSomethingElse());
|
||||
----
|
||||
|
||||
### if-then-else-if
|
||||
|
||||
[[source,java]]
|
||||
----
|
||||
if (isFalse()) {
|
||||
doSomething();
|
||||
} else if (isTrue()) {
|
||||
doSomethingElse();
|
||||
}
|
||||
----
|
||||
|
||||
[[source,java]]
|
||||
----
|
||||
Condition.where(isFalse())
|
||||
.then(() -> doSomething())
|
||||
.otherwise(isTrue)
|
||||
.then(() -> doSomethingElse());
|
||||
----
|
||||
|
||||
### if-then-if-then
|
||||
|
||||
[[source,java]]
|
||||
|
|
|
@ -109,6 +109,17 @@ public interface Condition {
|
|||
*/
|
||||
void otherwise(Runnable response);
|
||||
|
||||
/**
|
||||
* Create a new {@code Condition} for the clause as a continuation to an existing {@code Condition}.
|
||||
*
|
||||
* @param clause the condition to test
|
||||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
default Condition otherwise(boolean clause) {
|
||||
return where(clause);
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@code Condition} that has evaluated to {@code true}.
|
||||
*/
|
||||
|
|
|
@ -144,6 +144,28 @@ public class ConditionalTest {
|
|||
thenTheThenResponseRuns();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereFalseElseTrueThenOtherwiseRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
.then(thenResponse)
|
||||
.otherwise(true)
|
||||
.then(otherwiseResponse);
|
||||
//then
|
||||
thenTheOtherwiseResponseRuns();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereFalseElseFalseThenNothingRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
.then(thenResponse)
|
||||
.otherwise(false)
|
||||
.then(otherwiseResponse);
|
||||
//then
|
||||
thenNoResponseRuns();
|
||||
}
|
||||
|
||||
private void whenOr(final boolean firstClause, final boolean secondClause) {
|
||||
Condition.where(firstClause)
|
||||
.or(secondClause)
|
||||
|
@ -172,6 +194,11 @@ public class ConditionalTest {
|
|||
assertThat(otherwiseFlag).isTrue();
|
||||
}
|
||||
|
||||
private void thenNoResponseRuns() {
|
||||
assertThat(thenFlag).isFalse();
|
||||
assertThat(otherwiseFlag).isFalse();
|
||||
}
|
||||
|
||||
private void when(final boolean firstClause, final boolean secondClause) {
|
||||
Condition.where(firstClause)
|
||||
.and(secondClause)
|
||||
|
|
Loading…
Reference in a new issue