Condition.{and(Condition),or(Condition),not()}: added
Deprecate the whereNot(), andNot() and orNot() methods infavour of these new methods, which they all not use internally..
This commit is contained in:
parent
d56d864ef2
commit
80e813e933
5 changed files with 206 additions and 32 deletions
10
CHANGELOG
10
CHANGELOG
|
@ -1,6 +1,16 @@
|
|||
CHANGELOG
|
||||
=========
|
||||
|
||||
0.6.0
|
||||
-----
|
||||
|
||||
* Add `Condition.and(Condition)`
|
||||
* Add `Condition.or(Condition)`
|
||||
* Add `Condition.not()`
|
||||
* Deprecate `Condition.whereNot(boolean)`
|
||||
* Deprecate `Condition.andNot(boolean)`
|
||||
* Deprecate `Condition.orNot(boolean)`
|
||||
|
||||
0.5.0
|
||||
-----
|
||||
|
||||
|
|
|
@ -35,7 +35,6 @@ public interface Condition {
|
|||
* Create a new {@code Condition} for the clause.
|
||||
*
|
||||
* @param clause the condition to test
|
||||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
static Condition where(final boolean clause) {
|
||||
|
@ -49,59 +48,102 @@ public interface Condition {
|
|||
* Create a new {@code Condition} for the boolean opposite of the clause.
|
||||
*
|
||||
* @param clause the condition to test
|
||||
*
|
||||
* @return the Condition
|
||||
* @deprecated use {@link #not()}
|
||||
*/
|
||||
@Deprecated
|
||||
static Condition whereNot(final boolean clause) {
|
||||
return where(!clause);
|
||||
return where(clause).not();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the Condition is true or not.
|
||||
*
|
||||
* @return true if the Condition is true
|
||||
*/
|
||||
boolean isTrue();
|
||||
|
||||
/**
|
||||
* Checks of the Condition is false or not.
|
||||
*
|
||||
* @return true if the Condition is false
|
||||
*/
|
||||
boolean isFalse();
|
||||
|
||||
/**
|
||||
* Negates the Condtion.
|
||||
*
|
||||
* @return a false Condition if the Condition is true, or a true Condition if the Condition is false.
|
||||
*/
|
||||
default Condition not() {
|
||||
return Condition.where(isFalse());
|
||||
}
|
||||
|
||||
/**
|
||||
* Logically AND combine the current {@code Condition} with the clause.
|
||||
*
|
||||
* @param clause the condition to test
|
||||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
Condition and(Supplier<Boolean> clause);
|
||||
|
||||
/**
|
||||
* Logicaly OR current {@code Condition} with the other {@code Condition}.
|
||||
*
|
||||
* @param other the other Condition
|
||||
* @return true if both Conditions are true
|
||||
*/
|
||||
default Condition and(Condition other) {
|
||||
return Condition.where(isTrue()).and(other::isTrue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logically AND combine the current {@code Condition} with boolean opposite of the clause.
|
||||
*
|
||||
* @param clause the condition to test
|
||||
*
|
||||
* @return the Condition
|
||||
* @deprecated use {@link #not()}
|
||||
*/
|
||||
@Deprecated
|
||||
default Condition andNot(final Supplier<Boolean> clause) {
|
||||
return and(() -> !clause.get());
|
||||
return and(clause).not();
|
||||
}
|
||||
|
||||
/**
|
||||
* Logically OR combine the current {@code Condition} with the clause.
|
||||
*
|
||||
* @param clause the condition to test
|
||||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
@SuppressWarnings("PMD.ShortMethodName")
|
||||
Condition or(Supplier<Boolean> clause);
|
||||
|
||||
/**
|
||||
* Logically OR the current {@code Condition} with the other {@code Condition}.
|
||||
*
|
||||
* @param other the other Condition
|
||||
* @return true if either Condition is true
|
||||
*/
|
||||
default Condition or(Condition other) {
|
||||
return where(isTrue()).or(other::isTrue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logically OR combine the current {@code Condition} with the boolean opposite of the clause.
|
||||
*
|
||||
* @param clause the condition to test
|
||||
*
|
||||
* @return the Condition
|
||||
* @deprecated use {@link #not()}
|
||||
*/
|
||||
@Deprecated
|
||||
default Condition orNot(final Supplier<Boolean> clause) {
|
||||
return or(() -> !clause.get());
|
||||
return or(clause).not();
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform this response if the {@code Condition} is {@code true}.
|
||||
*
|
||||
* @param response the response to perform
|
||||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
Condition then(Action response);
|
||||
|
@ -117,7 +159,6 @@ public interface Condition {
|
|||
* 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(final Supplier<Boolean> clause) {
|
||||
|
@ -141,4 +182,5 @@ public interface Condition {
|
|||
*/
|
||||
@SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS)
|
||||
void otherwiseThrow(Exception exception) throws Exception;
|
||||
|
||||
}
|
||||
|
|
|
@ -62,4 +62,15 @@ final class FalseCondition implements Condition {
|
|||
public void otherwiseThrow(final Exception exception) throws Exception {
|
||||
throw exception;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrue() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFalse() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -64,4 +64,14 @@ final class TrueCondition implements Condition {
|
|||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFalse() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,18 +7,21 @@ import org.junit.Test;
|
|||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static net.kemitix.conditional.Condition.where;
|
||||
|
||||
/**
|
||||
* @author Paul Campbell (pcampbell@kemitix.net).
|
||||
*/
|
||||
public class ConditionalTest implements WithAssertions {
|
||||
|
||||
private Action thenResponse;
|
||||
|
||||
private Action otherwiseResponse;
|
||||
|
||||
private boolean thenFlag;
|
||||
|
||||
private boolean otherwiseFlag;
|
||||
private final org.assertj.core.api.Condition<? super Condition> trueCondition =
|
||||
new org.assertj.core.api.Condition<>(Condition::isTrue, "is true");
|
||||
private final org.assertj.core.api.Condition<? super Condition> falseCondition =
|
||||
new org.assertj.core.api.Condition<>(Condition::isFalse, "is false");
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
@ -77,7 +80,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereTrueThenDoSomethingAndThenDoSomethingElse() {
|
||||
//when
|
||||
Condition.where(true)
|
||||
where(true)
|
||||
.then(thenResponse)
|
||||
.and(() -> true)
|
||||
.then(otherwiseResponse);
|
||||
|
@ -139,7 +142,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereTrueAndNotFalseThenRuns() {
|
||||
//when
|
||||
Condition.where(true)
|
||||
where(true)
|
||||
.andNot(() -> false)
|
||||
.then(thenResponse);
|
||||
//then
|
||||
|
@ -149,7 +152,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereTrueAndNotTrueThenOtherwiseRuns() {
|
||||
//when
|
||||
Condition.where(true)
|
||||
where(true)
|
||||
.andNot(() -> true)
|
||||
.then(thenResponse)
|
||||
.otherwise(otherwiseResponse);
|
||||
|
@ -160,7 +163,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereFalseOrNotFalseThenRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
where(false)
|
||||
.orNot(() -> false)
|
||||
.then(thenResponse);
|
||||
//then
|
||||
|
@ -170,7 +173,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereFalseOrNotTrueThenOtherwiseRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
where(false)
|
||||
.orNot(() -> true)
|
||||
.then(thenResponse)
|
||||
.otherwise(otherwiseResponse);
|
||||
|
@ -181,7 +184,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereFalseElseTrueThenOtherwiseRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
where(false)
|
||||
.then(thenResponse)
|
||||
.otherwise(() -> true)
|
||||
.then(otherwiseResponse);
|
||||
|
@ -192,7 +195,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereFalseElseFalseThenNothingRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
where(false)
|
||||
.then(thenResponse)
|
||||
.otherwise(() -> false)
|
||||
.then(otherwiseResponse);
|
||||
|
@ -203,7 +206,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereTrueChainedThensBothRuns() {
|
||||
//when
|
||||
Condition.where(true)
|
||||
where(true)
|
||||
.then(thenResponse)
|
||||
.then(otherwiseResponse);
|
||||
//then
|
||||
|
@ -213,7 +216,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
@Test
|
||||
public void whereFalseChainedThensNothingRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
where(false)
|
||||
.then(thenResponse)
|
||||
.then(otherwiseResponse);
|
||||
//then
|
||||
|
@ -261,7 +264,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
}
|
||||
|
||||
private void when(final boolean clause) {
|
||||
Condition.where(clause)
|
||||
where(clause)
|
||||
.then(thenResponse)
|
||||
.otherwise(otherwiseResponse);
|
||||
}
|
||||
|
@ -270,7 +273,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
final boolean firstClause,
|
||||
final boolean secondClause
|
||||
) {
|
||||
Condition.where(firstClause)
|
||||
where(firstClause)
|
||||
.and(() -> secondClause)
|
||||
.then(thenResponse)
|
||||
.otherwise(otherwiseResponse);
|
||||
|
@ -280,7 +283,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
final boolean firstClause,
|
||||
final boolean secondClause
|
||||
) {
|
||||
Condition.where(firstClause)
|
||||
where(firstClause)
|
||||
.or(() -> secondClause)
|
||||
.then(thenResponse)
|
||||
.otherwise(otherwiseResponse);
|
||||
|
@ -291,7 +294,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
//given
|
||||
final AtomicInteger atomicInteger = new AtomicInteger();
|
||||
//when
|
||||
Condition.where(true)
|
||||
where(true)
|
||||
.or(() -> atomicInteger.compareAndSet(0, 2))
|
||||
.then(thenResponse);
|
||||
//then
|
||||
|
@ -304,7 +307,7 @@ public class ConditionalTest implements WithAssertions {
|
|||
//given
|
||||
final AtomicInteger atomicInteger = new AtomicInteger();
|
||||
//when
|
||||
Condition.where(false)
|
||||
where(false)
|
||||
.and(() -> atomicInteger.compareAndSet(0, 2))
|
||||
.then(thenResponse);
|
||||
//then
|
||||
|
@ -316,14 +319,14 @@ public class ConditionalTest implements WithAssertions {
|
|||
public void whereTrueThenThrowException() {
|
||||
//given
|
||||
assertThatExceptionOfType(IOException.class)
|
||||
.isThrownBy(() -> Condition.where(true)
|
||||
.isThrownBy(() -> where(true)
|
||||
.thenThrow(new IOException()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereFalseThenDoNotThrowException() throws Exception {
|
||||
assertThatCode(() ->
|
||||
Condition.where(false)
|
||||
where(false)
|
||||
.thenThrow(new IOException()))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
@ -332,15 +335,113 @@ public class ConditionalTest implements WithAssertions {
|
|||
public void whereFalseOtherwiseThenThrowException() {
|
||||
//given
|
||||
assertThatExceptionOfType(IOException.class)
|
||||
.isThrownBy(() -> Condition.where(false)
|
||||
.isThrownBy(() -> where(false)
|
||||
.otherwiseThrow(new IOException()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereTrueOtherwiseThenDoNotThrowException() throws Exception {
|
||||
assertThatCode(() ->
|
||||
Condition.where(true)
|
||||
where(true)
|
||||
.otherwiseThrow(new IOException()))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereTrueConditionAndTrueConditionThenTrueCondition() {
|
||||
//given
|
||||
final Condition condition1 = Condition.where(true);
|
||||
final Condition condition2 = Condition.where(true);
|
||||
//when
|
||||
final Condition result = condition1.and(condition2);
|
||||
//then
|
||||
assertThat(result).is(trueCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereTrueConditionAndFalseConditionThenFalseCondition() {
|
||||
//given
|
||||
final Condition condition1 = Condition.where(true);
|
||||
final Condition condition2 = Condition.where(false);
|
||||
//when
|
||||
final Condition result = condition1.and(condition2);
|
||||
//then
|
||||
assertThat(result).is(falseCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereFalseConditionAndTrueConditionThenFalseCondition() {
|
||||
//given
|
||||
final Condition condition1 = Condition.where(false);
|
||||
final Condition condition2 = Condition.where(true);
|
||||
//when
|
||||
final Condition result = condition1.and(condition2);
|
||||
//then
|
||||
assertThat(result).is(falseCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereFalseConditionAndFalseConditionThenFalseCondition() {
|
||||
//given
|
||||
final Condition condition1 = Condition.where(false);
|
||||
final Condition condition2 = Condition.where(false);
|
||||
//when
|
||||
final Condition result = condition1.and(condition2);
|
||||
//then
|
||||
assertThat(result).is(falseCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereTrueConditionOrTrueConditionThenTrueCondition() {
|
||||
//given
|
||||
final Condition condition1 = Condition.where(true);
|
||||
final Condition condition2 = Condition.where(true);
|
||||
//when
|
||||
final Condition result = condition1.or(condition2);
|
||||
//then
|
||||
assertThat(result).is(trueCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereTrueConditionOrFalseConditionThenTrueCondition() {
|
||||
//given
|
||||
final Condition condition1 = Condition.where(true);
|
||||
final Condition condition2 = Condition.where(false);
|
||||
//when
|
||||
final Condition result = condition1.or(condition2);
|
||||
//then
|
||||
assertThat(result).is(trueCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereFalseConditionOrTrueConditionThenTrueCondition() {
|
||||
//given
|
||||
final Condition condition1 = Condition.where(false);
|
||||
final Condition condition2 = Condition.where(true);
|
||||
//when
|
||||
final Condition result = condition1.or(condition2);
|
||||
//then
|
||||
assertThat(result).is(trueCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereFalseConditionOrFalseConditionThenFalseCondition() {
|
||||
//given
|
||||
final Condition condition1 = Condition.where(false);
|
||||
final Condition condition2 = Condition.where(false);
|
||||
//when
|
||||
final Condition result = condition1.or(condition2);
|
||||
//then
|
||||
assertThat(result).is(falseCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereTrueWhenNotThenFalse() {
|
||||
assertThat(Condition.where(true).not()).is(falseCondition);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whereFalseWhenNotThenTriue() {
|
||||
assertThat(Condition.where(false).not()).is(trueCondition);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue