From efc1705a6c1cf40825d3a255cbe02ac5bc49054a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Mar 2018 17:08:06 +0000 Subject: [PATCH] Condition: secondary clauses now use Supplier's to enable short circuiting --- .../net/kemitix/conditional/Condition.java | 20 ++-- .../kemitix/conditional/FalseCondition.java | 9 +- .../kemitix/conditional/TrueCondition.java | 9 +- .../kemitix/conditional/ConditionalTest.java | 108 +++++++++++------- 4 files changed, 88 insertions(+), 58 deletions(-) diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index 000268c..3f385eb 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -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 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 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 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 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 clause) { + return where(clause.get()); } } diff --git a/src/main/java/net/kemitix/conditional/FalseCondition.java b/src/main/java/net/kemitix/conditional/FalseCondition.java index 07881c7..cd382ca 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -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 clause) { return FALSE; } @Override - @SuppressWarnings("PMD.ShortMethodName") - public Condition or(final boolean secondClause) { - return Condition.where(secondClause); + public Condition or(final Supplier secondClause) { + return Condition.where(secondClause.get()); } @Override diff --git a/src/main/java/net/kemitix/conditional/TrueCondition.java b/src/main/java/net/kemitix/conditional/TrueCondition.java index 06f041b..987496c 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -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 clause) { + return Condition.where(clause.get()); } @Override - @SuppressWarnings("PMD.ShortMethodName") - public Condition or(final boolean secondClause) { + public Condition or(final Supplier secondClause) { return TRUE; } diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 525c534..5124b7f 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -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); } }