Value: add where() with Supplier<T> parameters

* Add `<T> Value.where(boolean, Supplier<T>, Supplier<T>)`
* Add `Optional<T> Value.where(boolean, Supplier<T>)`
This commit is contained in:
Paul Campbell 2017-08-26 22:58:29 +01:00
parent 03eb1398bd
commit a1d9f17faf
4 changed files with 65 additions and 2 deletions

View file

@ -4,6 +4,8 @@ CHANGELOG
0.3.0 0.3.0
----- -----
* Add `<T> Value.where(boolean, Supplier<T>, Supplier<T>)`
* Add `Optional<T> Value.where(boolean, Supplier<T>)`
* Add `.travis-support` * Add `.travis-support`
* Avoid danger of JVM-level deadlock during `Value` initialisation * Avoid danger of JVM-level deadlock during `Value` initialisation
* Avoid danger of JVM-level deadlock during `Condition` initialisation * Avoid danger of JVM-level deadlock during `Condition` initialisation

View file

@ -201,6 +201,16 @@ if (isTrue()) {
String result = isTrue() ? TRUE : FALSE; String result = isTrue() ? TRUE : FALSE;
---- ----
[[source,java]]
----
final String result = Value.where(isTrue(), () -> TRUE, () -> FALSE);
----
[[source,java]]
----
final Optional<String> result = Value.where(isTrue(), () -> TRUE);
----
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>where(isTrue()).then(() -> TRUE) final String result = Value.<String>where(isTrue()).then(() -> TRUE)

View file

@ -21,6 +21,7 @@
package net.kemitix.conditional; package net.kemitix.conditional;
import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -55,6 +56,22 @@ public interface Value {
return where(!clause); return where(!clause);
} }
static <T> T where(
boolean clause,
Supplier<T> trueSupplier,
Supplier<T> falseSupplier
) {
return Value.<T>where(clause).then(trueSupplier)
.otherwise(falseSupplier);
}
static <T> Optional<T> where(
boolean clause,
Supplier<T> trueSupplier
) {
return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null));
}
/** /**
* An intermediate state in determining the final {@link Value}. * An intermediate state in determining the final {@link Value}.
* *

View file

@ -3,6 +3,8 @@ package net.kemitix.conditional;
import lombok.val; import lombok.val;
import org.junit.Test; import org.junit.Test;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -15,7 +17,39 @@ public class ValueTest {
private static final String FALSE = "false"; private static final String FALSE = "false";
@Test @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<String> result = Value.where(true, () -> TRUE);
//then
assertThat(result).contains(TRUE);
}
@Test
public void valueWhereClauseIsFalseIsEmptyOptional() {
//when
final Optional<String> result = Value.where(false, () -> TRUE);
//then
assertThat(result).isEmpty();
}
@Test
public void valueWhereClauseIsTrue() {
//when //when
val result = Value.<String>where(true).then(() -> TRUE) val result = Value.<String>where(true).then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
@ -24,7 +58,7 @@ public class ValueTest {
} }
@Test @Test
public void valueWhereFalseIsFalse() { public void valueWhereClauseIsFalse() {
//when //when
val result = Value.<String>where(false).then(() -> TRUE) val result = Value.<String>where(false).then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);