From a1d9f17faf18282159c46615b916238e1770944b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 26 Aug 2017 22:58:29 +0100 Subject: [PATCH 1/2] Value: add where() with Supplier parameters * Add ` Value.where(boolean, Supplier, Supplier)` * Add `Optional Value.where(boolean, Supplier)` --- CHANGELOG | 2 + README.adoc | 10 +++++ .../java/net/kemitix/conditional/Value.java | 17 +++++++++ .../net/kemitix/conditional/ValueTest.java | 38 ++++++++++++++++++- 4 files changed, 65 insertions(+), 2 deletions(-) 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..f438e25 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; /** @@ -55,6 +56,22 @@ public interface Value { return where(!clause); } + static T where( + boolean clause, + Supplier trueSupplier, + Supplier falseSupplier + ) { + return Value.where(clause).then(trueSupplier) + .otherwise(falseSupplier); + } + + static Optional where( + boolean clause, + Supplier trueSupplier + ) { + return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null)); + } + /** * An intermediate state in determining the final {@link Value}. * 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); From d9e3401658830cd3ec6494c1f13cb4a309a08dc7 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 26 Aug 2017 23:11:10 +0100 Subject: [PATCH 2/2] Value: add missing javadoc --- .../java/net/kemitix/conditional/Value.java | 51 +++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index f438e25..6345564 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -31,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. * @@ -56,22 +91,6 @@ public interface Value { return where(!clause); } - static T where( - boolean clause, - Supplier trueSupplier, - Supplier falseSupplier - ) { - return Value.where(clause).then(trueSupplier) - .otherwise(falseSupplier); - } - - static Optional where( - boolean clause, - Supplier trueSupplier - ) { - return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null)); - } - /** * An intermediate state in determining the final {@link Value}. *