Condition: add whereNot(), andNot() and orNot()

This commit is contained in:
Paul Campbell 2017-04-21 12:16:54 +01:00
parent 0fbb4f62b6
commit fe355a3850
2 changed files with 63 additions and 1 deletions

View file

@ -29,7 +29,7 @@ package net.kemitix.conditional;
public interface Condition {
/**
* Create a new {@code Condition}.
* Create a new {@code Condition} for the clause.
*
* @param clause the condition to test
*
@ -42,6 +42,17 @@ public interface Condition {
return new FalseCondition();
}
/**
* Create a new {@code Condition} for the boolean opposite of the clause.
*
* @param clause the condition to test
*
* @return the Condition
*/
static Condition whereNot(boolean clause) {
return where(!clause);
}
/**
* Logically AND combine the current {@code Condition} with the clause.
*
@ -51,6 +62,17 @@ public interface Condition {
*/
Condition and(boolean clause);
/**
* Logically AND combine the current {@code Condition} with boolean opposite of the clause.
*
* @param clause the condition to test
*
* @return the Condition
*/
default Condition andNot(boolean clause) {
return and(!clause);
}
/**
* Logically OR combine the current {@code Condition} with the clause.
*
@ -60,6 +82,17 @@ public interface Condition {
*/
Condition or(boolean clause);
/**
* Logically OR combine the current {@code Condition} with the boolean opposite of the clause.
*
* @param clause the condition to test
*
* @return the Condition
*/
default Condition orNot(boolean clause) {
return or(!clause);
}
/**
* Perform this response if the {@code Condition} is {@code true}.
*

View file

@ -115,6 +115,35 @@ public class ConditionalTest {
thenTheOtherwiseResponseRuns();
}
@Test
public void whereNotFalseThenRuns() {
//when
Condition.whereNot(false)
.then(thenResponse);
//then
thenTheThenResponseRuns();
}
@Test
public void whereTrueAndNotFalseThenRuns() {
//when
Condition.where(true)
.andNot(false)
.then(thenResponse);
//then
thenTheThenResponseRuns();
}
@Test
public void whereFalseOrNotFalseThenRuns() {
//when
Condition.where(false)
.orNot(false)
.then(thenResponse);
//then
thenTheThenResponseRuns();
}
private void whenOr(final boolean firstClause, final boolean secondClause) {
Condition.where(firstClause)
.or(secondClause)