diff --git a/CHANGELOG b/CHANGELOG index 9eb5da8..accb642 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,8 @@ CHANGELOG 0.3.0 ----- +* Add ` Value.where(boolean, Supplier, Supplier)` +* Add `Optional Value.where(boolean, Supplier)` * Add `.travis-support` * Avoid danger of JVM-level deadlock during `Value` initialisation * Avoid danger of JVM-level deadlock during `Condition` initialisation diff --git a/README.adoc b/README.adoc index 6dd1db4..d4bebd6 100644 --- a/README.adoc +++ b/README.adoc @@ -201,6 +201,16 @@ if (isTrue()) { String result = isTrue() ? TRUE : FALSE; ---- +[[source,java]] +---- +final String result = Value.where(isTrue(), () -> TRUE, () -> FALSE); +---- + +[[source,java]] +---- +final Optional result = Value.where(isTrue(), () -> TRUE); +---- + [[source,java]] ---- final String result = Value.where(isTrue()).then(() -> TRUE) diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index 24908c2..6345564 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -21,6 +21,7 @@ package net.kemitix.conditional; +import java.util.Optional; import java.util.function.Supplier; /** @@ -30,6 +31,41 @@ import java.util.function.Supplier; */ public interface Value { + /** + * Return one of two values depending on the value of a clause. + * + * @param clause The deciding clause + * @param trueSupplier The supplier to provide the value when the clause is true + * @param falseSupplier The supplier to provide the value when the clause is false + * @param The type of the value + * + * @return the value from either the trueSupplier or the falseSupplier + */ + static T where( + boolean clause, + Supplier trueSupplier, + Supplier falseSupplier + ) { + return Value.where(clause).then(trueSupplier) + .otherwise(falseSupplier); + } + + /** + * Return an Optional either containing a value, if the clause is true, or empty. + * + * @param clause The deciding clause + * @param trueSupplier The supplier to provide the value when the clause is true + * @param The type of the value + * + * @return an Optional either containing the value from the trueSupplier or empty + */ + static Optional where( + boolean clause, + Supplier trueSupplier + ) { + return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null)); + } + /** * Create a new {@link ValueClause} for the clause. * diff --git a/src/test/java/net/kemitix/conditional/ValueTest.java b/src/test/java/net/kemitix/conditional/ValueTest.java index 2b7b790..d76011b 100644 --- a/src/test/java/net/kemitix/conditional/ValueTest.java +++ b/src/test/java/net/kemitix/conditional/ValueTest.java @@ -3,6 +3,8 @@ package net.kemitix.conditional; import lombok.val; import org.junit.Test; +import java.util.Optional; + import static org.assertj.core.api.Assertions.assertThat; /** @@ -15,7 +17,39 @@ public class ValueTest { private static final String FALSE = "false"; @Test - public void valueWhereTrueIsTrue() { + public void valueWhereClauseIsTrueTypeSafe() { + //when + final String result = Value.where(true, () -> TRUE, () -> FALSE); + //then + assertThat(result).isEqualTo(TRUE); + } + + @Test + public void valueWhereClauseIsFalseTypeSafe() { + //when + final String result = Value.where(false, () -> TRUE, () -> FALSE); + //then + assertThat(result).isEqualTo(FALSE); + } + + @Test + public void valueWhereClauseIsTrueIsOptional() { + //when + final Optional result = Value.where(true, () -> TRUE); + //then + assertThat(result).contains(TRUE); + } + + @Test + public void valueWhereClauseIsFalseIsEmptyOptional() { + //when + final Optional result = Value.where(false, () -> TRUE); + //then + assertThat(result).isEmpty(); + } + + @Test + public void valueWhereClauseIsTrue() { //when val result = Value.where(true).then(() -> TRUE) .otherwise(() -> FALSE); @@ -24,7 +58,7 @@ public class ValueTest { } @Test - public void valueWhereFalseIsFalse() { + public void valueWhereClauseIsFalse() { //when val result = Value.where(false).then(() -> TRUE) .otherwise(() -> FALSE);