Merge pull request #7 from kemitix/value-with-suppliers
Value: add where() with Supplier<T> parameters
This commit is contained in:
commit
91bb2adc0f
4 changed files with 84 additions and 2 deletions
|
@ -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
|
||||||
|
|
10
README.adoc
10
README.adoc
|
@ -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)
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,6 +31,41 @@ import java.util.function.Supplier;
|
||||||
*/
|
*/
|
||||||
public interface Value {
|
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 <T> The type of the value
|
||||||
|
*
|
||||||
|
* @return the value from either the trueSupplier or the falseSupplier
|
||||||
|
*/
|
||||||
|
static <T> T where(
|
||||||
|
boolean clause,
|
||||||
|
Supplier<T> trueSupplier,
|
||||||
|
Supplier<T> falseSupplier
|
||||||
|
) {
|
||||||
|
return Value.<T>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 <T> The type of the value
|
||||||
|
*
|
||||||
|
* @return an Optional either containing the value from the trueSupplier or empty
|
||||||
|
*/
|
||||||
|
static <T> Optional<T> where(
|
||||||
|
boolean clause,
|
||||||
|
Supplier<T> trueSupplier
|
||||||
|
) {
|
||||||
|
return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@link ValueClause} for the clause.
|
* Create a new {@link ValueClause} for the clause.
|
||||||
*
|
*
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue