Condition: secondary clauses now use Supplier's to enable short circuiting

This commit is contained in:
Paul Campbell 2018-03-13 17:08:06 +00:00
parent d1ca0b039f
commit efc1705a6c
4 changed files with 88 additions and 58 deletions

View file

@ -21,6 +21,8 @@
package net.kemitix.conditional;
import java.util.function.Supplier;
/**
* If-then-else in a functional-style.
*
@ -35,7 +37,6 @@ public interface Condition {
*
* @return the Condition
*/
@SuppressWarnings("avoidinlineconditionals")
static Condition where(final boolean clause) {
return clause ? TrueCondition.TRUE : FalseCondition.FALSE;
}
@ -58,7 +59,7 @@ public interface Condition {
*
* @return the Condition
*/
Condition and(boolean clause);
Condition and(Supplier<Boolean> clause);
/**
* Logically AND combine the current {@code Condition} with boolean opposite of the clause.
@ -67,8 +68,8 @@ public interface Condition {
*
* @return the Condition
*/
default Condition andNot(final boolean clause) {
return and(!clause);
default Condition andNot(final Supplier<Boolean> clause) {
return and(() -> !clause.get());
}
/**
@ -78,8 +79,7 @@ public interface Condition {
*
* @return the Condition
*/
@SuppressWarnings("PMD.ShortMethodName")
Condition or(boolean clause);
Condition or(Supplier<Boolean> clause);
/**
* Logically OR combine the current {@code Condition} with the boolean opposite of the clause.
@ -88,8 +88,8 @@ public interface Condition {
*
* @return the Condition
*/
default Condition orNot(final boolean clause) {
return or(!clause);
default Condition orNot(final Supplier<Boolean> clause) {
return or(() -> !clause.get());
}
/**
@ -115,8 +115,8 @@ public interface Condition {
*
* @return the Condition
*/
default Condition otherwise(final boolean clause) {
return where(clause);
default Condition otherwise(final Supplier<Boolean> clause) {
return where(clause.get());
}
}

View file

@ -21,6 +21,8 @@
package net.kemitix.conditional;
import java.util.function.Supplier;
/**
* A {@code Condition} that has evaluated to {@code false}.
*
@ -31,14 +33,13 @@ final class FalseCondition implements Condition {
public static final Condition FALSE = new net.kemitix.conditional.FalseCondition();
@Override
public Condition and(final boolean clause) {
public Condition and(final Supplier<Boolean> clause) {
return FALSE;
}
@Override
@SuppressWarnings("PMD.ShortMethodName")
public Condition or(final boolean secondClause) {
return Condition.where(secondClause);
public Condition or(final Supplier<Boolean> secondClause) {
return Condition.where(secondClause.get());
}
@Override

View file

@ -21,6 +21,8 @@
package net.kemitix.conditional;
import java.util.function.Supplier;
/**
* A {@code Condition} that has evaluated to {@code true}.
*
@ -31,13 +33,12 @@ final class TrueCondition implements Condition {
public static final Condition TRUE = new net.kemitix.conditional.TrueCondition();
@Override
public Condition and(final boolean clause) {
return Condition.where(clause);
public Condition and(final Supplier<Boolean> clause) {
return Condition.where(clause.get());
}
@Override
@SuppressWarnings("PMD.ShortMethodName")
public Condition or(final boolean secondClause) {
public Condition or(final Supplier<Boolean> secondClause) {
return TRUE;
}

View file

@ -3,6 +3,8 @@ package net.kemitix.conditional;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
/**
@ -76,9 +78,9 @@ public class ConditionalTest {
public void whereTrueThenDoSomethingAndThenDoSomethingElse() {
//when
Condition.where(true)
.then(thenResponse)
.and(true)
.then(otherwiseResponse);
.then(thenResponse)
.and(() -> true)
.then(otherwiseResponse);
//then
assertThatBothResponsesRun();
}
@ -119,7 +121,7 @@ public class ConditionalTest {
public void whereNotFalseThenRuns() {
//when
Condition.whereNot(false)
.then(thenResponse);
.then(thenResponse);
//then
assertThatTheThenResponseRuns();
}
@ -128,8 +130,8 @@ public class ConditionalTest {
public void whereNotTrueThenOtherwiseRuns() {
//when
Condition.whereNot(true)
.then(thenResponse)
.otherwise(otherwiseResponse);
.then(thenResponse)
.otherwise(otherwiseResponse);
//then
assertThatTheOtherwiseResponseRuns();
}
@ -138,8 +140,8 @@ public class ConditionalTest {
public void whereTrueAndNotFalseThenRuns() {
//when
Condition.where(true)
.andNot(false)
.then(thenResponse);
.andNot(() -> false)
.then(thenResponse);
//then
assertThatTheThenResponseRuns();
}
@ -148,9 +150,9 @@ public class ConditionalTest {
public void whereTrueAndNotTrueThenOtherwiseRuns() {
//when
Condition.where(true)
.andNot(true)
.then(thenResponse)
.otherwise(otherwiseResponse);
.andNot(() -> true)
.then(thenResponse)
.otherwise(otherwiseResponse);
//then
assertThatTheOtherwiseResponseRuns();
}
@ -159,8 +161,8 @@ public class ConditionalTest {
public void whereFalseOrNotFalseThenRuns() {
//when
Condition.where(false)
.orNot(false)
.then(thenResponse);
.orNot(() -> false)
.then(thenResponse);
//then
assertThatTheThenResponseRuns();
}
@ -169,9 +171,9 @@ public class ConditionalTest {
public void whereFalseOrNotTrueThenOtherwiseRuns() {
//when
Condition.where(false)
.orNot(true)
.then(thenResponse)
.otherwise(otherwiseResponse);
.orNot(() -> true)
.then(thenResponse)
.otherwise(otherwiseResponse);
//then
assertThatTheOtherwiseResponseRuns();
}
@ -180,9 +182,9 @@ public class ConditionalTest {
public void whereFalseElseTrueThenOtherwiseRuns() {
//when
Condition.where(false)
.then(thenResponse)
.otherwise(true)
.then(otherwiseResponse);
.then(thenResponse)
.otherwise(() -> true)
.then(otherwiseResponse);
//then
assertThatTheOtherwiseResponseRuns();
}
@ -191,9 +193,9 @@ public class ConditionalTest {
public void whereFalseElseFalseThenNothingRuns() {
//when
Condition.where(false)
.then(thenResponse)
.otherwise(false)
.then(otherwiseResponse);
.then(thenResponse)
.otherwise(() -> false)
.then(otherwiseResponse);
//then
assertThatNoResponseRuns();
}
@ -202,8 +204,8 @@ public class ConditionalTest {
public void whereTrueChainedThensBothRuns() {
//when
Condition.where(true)
.then(thenResponse)
.then(otherwiseResponse);
.then(thenResponse)
.then(otherwiseResponse);
//then
assertThatBothResponsesRun();
}
@ -212,8 +214,8 @@ public class ConditionalTest {
public void whereFalseChainedThensNothingRuns() {
//when
Condition.where(false)
.then(thenResponse)
.then(otherwiseResponse);
.then(thenResponse)
.then(otherwiseResponse);
//then
assertThatNoResponseRuns();
}
@ -235,22 +237,22 @@ public class ConditionalTest {
private void assertThatTheOtherwiseResponseRan() {
assertThat(otherwiseFlag).as("otherwise response runs")
.isTrue();
.isTrue();
}
private void assertThatTheThenResponseRan() {
assertThat(thenFlag).as("then response runs")
.isTrue();
.isTrue();
}
private void assertThatTheOtherwiseResponseDidNotRun() {
assertThat(otherwiseFlag).as("otherwise response does not run")
.isFalse();
.isFalse();
}
private void assertThatTheThenResponseDidNotRun() {
assertThat(thenFlag).as("then response does not run")
.isFalse();
.isFalse();
}
private void assertThatNoResponseRuns() {
@ -260,27 +262,53 @@ public class ConditionalTest {
private void when(final boolean clause) {
Condition.where(clause)
.then(thenResponse)
.otherwise(otherwiseResponse);
.then(thenResponse)
.otherwise(otherwiseResponse);
}
private void when(
final boolean firstClause,
final boolean secondClause
) {
) {
Condition.where(firstClause)
.and(secondClause)
.then(thenResponse)
.otherwise(otherwiseResponse);
.and(() -> secondClause)
.then(thenResponse)
.otherwise(otherwiseResponse);
}
private void whenOr(
final boolean firstClause,
final boolean secondClause
) {
) {
Condition.where(firstClause)
.or(secondClause)
.then(thenResponse)
.otherwise(otherwiseResponse);
.or(() -> secondClause)
.then(thenResponse)
.otherwise(otherwiseResponse);
}
@Test
public void shortCurcuitOr() {
//given
final AtomicInteger atomicInteger = new AtomicInteger();
//when
Condition.where(true)
.or(() -> atomicInteger.compareAndSet(0, 2))
.then(thenResponse);
//then
assertThatTheThenResponseRan();
assertThat(atomicInteger).hasValue(0);
}
@Test
public void shortCurcuitAnd() {
//given
final AtomicInteger atomicInteger = new AtomicInteger();
//when
Condition.where(false)
.and(() -> atomicInteger.compareAndSet(0, 2))
.then(thenResponse);
//then
assertThatTheThenResponseDidNotRun();
assertThat(atomicInteger).hasValue(0);
}
}