diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java new file mode 100644 index 0000000..7c7d201 --- /dev/null +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -0,0 +1,56 @@ +package net.kemitix.conditional; + +import java.util.function.Supplier; + +/** + * @author Paul Campbell (pcampbell@kemitix.net). + */ +public class Value { + + private boolean clause; + + private Supplier trueSupplier; + + private Value(final boolean clause) { + this.clause = clause; + } + + static Value where(final boolean clause) { + return new Value<>(clause); + } + + static Value whereNot(final boolean clause) { + return where(!clause); + } + + Value then(final Supplier trueSupplier) { + this.trueSupplier = trueSupplier; + return this; + } + + T otherwise(final Supplier falseSupplier) { + if (clause) { + return trueSupplier.get(); + } + return falseSupplier.get(); + } + + Value and(final boolean clause) { + this.clause = this.clause && clause; + return this; + } + + Value or(final boolean clause) { + this.clause = this.clause || clause; + return this; + } + + Value andNot(final boolean clause) { + return and(!clause); + } + + Value orNot(final boolean clause) { + return or(!clause); + } + +} diff --git a/src/test/java/net/kemitix/conditional/ValueTest.java b/src/test/java/net/kemitix/conditional/ValueTest.java new file mode 100644 index 0000000..66513a8 --- /dev/null +++ b/src/test/java/net/kemitix/conditional/ValueTest.java @@ -0,0 +1,228 @@ +package net.kemitix.conditional; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Paul Campbell (pcampbell@kemitix.net). + */ +public class ValueTest { + + private static final String TRUE = "true"; + + private static final String FALSE = "false"; + + @Test + public void valueWhereTrueIsTrue() { + //when + final String result = Value.where(true).then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereFalseIsFalse() { + //when + final String result = Value.where(false).then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereTrueAndTrueIsTrue() { + //when + final String result = Value.where(true).and(true) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereTrueAndFalseIsFalse() { + //when + final String result = Value.where(true).and(false) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereFalseAndTrueIsFalse() { + //when + final String result = Value.where(false).and(true) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereFalseAndFalseIsFalse() { + //when + final String result = Value.where(false).and(false) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereTrueOrTrueIsTrue() { + //when + final String result = Value.where(true).or(true) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereTrueOrFalseIsTrue() { + //when + final String result = Value.where(true).or(false) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereFalseOrTrueIsTrue() { + //when + final String result = Value.where(false).or(true) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereFalseOrFalseIsFalse() { + //when + final String result = Value.where(false).or(false) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereNotTrueIsFalse() { + //when + final String result = Value.whereNot(true).then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereNotFalseIsTrue() { + //when + final String result = Value.whereNot(false).then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereTrueAndNotTrueIsFalse() { + //when + final String result = Value.where(true).andNot(true) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereTrueAndNotFalseIsTrue() { + //when + final String result = Value.where(true).andNot(false) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereFalseAndNotTrueIsFalse() { + //when + final String result = Value.where(false).andNot(true) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereFalseAndNotFalseIsFalse() { + //when + final String result = Value.where(false).andNot(false) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereTrueOrNotTrueIsTrue() { + //when + final String result = Value.where(true).orNot(true) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereTrueOrNotFalseIsTrue() { + //when + final String result = Value.where(true).orNot(false) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + @Test + public void valueWhereFalseOrNotTrueIsFalse() { + //when + final String result = Value.where(false).orNot(true) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsFalse(result); + } + + @Test + public void valueWhereFalseOrNotFalseIsTrue() { + //when + final String result = Value.where(false).orNot(false) + .then(this::isTrue) + .otherwise(this::isFalse); + //then + thenIsTrue(result); + } + + private String isTrue() { + return TRUE; + } + + private String isFalse() { + return FALSE; + } + + private void thenIsFalse(final String result) { + assertThat(result).isEqualTo(isFalse()); + } + + private void thenIsTrue(final String result) { + assertThat(result).isEqualTo(TRUE); + } + +}