Value.where(Condition,...), Value.not() added

This commit is contained in:
Paul Campbell 2018-07-28 16:36:33 +01:00
parent e853894382
commit 0324cb8929
4 changed files with 120 additions and 48 deletions

View file

@ -28,13 +28,17 @@ import java.util.function.Supplier;
* An intermediate state where the clause has evaluated to false.
*
* @param <T> the type of the value
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
class FalseValueClause<T> implements Value.ValueClause<T> {
protected static final Value.ValueClause<?> FALSE = new FalseValueClause<>();
@Override
public Value.ValueClause<T> not() {
return Value.where(true);
}
@Override
public ValueSupplier<T> then(final Supplier<T> trueSupplier) {
return new FalseValueSupplier<>();

View file

@ -30,13 +30,17 @@ import java.util.function.Supplier;
* An intermediate state where the clause has evaluated to true.
*
* @param <T> the type of the value
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
class TrueValueClause<T> implements Value.ValueClause<T> {
protected static final Value.ValueClause<?> TRUE = new TrueValueClause<>();
@Override
public Value.ValueClause<T> not() {
return Value.where(false);
}
@Override
public ValueSupplier<T> then(final Supplier<T> trueSupplier) {
return new TrueValueSupplier<>(trueSupplier);

View file

@ -38,41 +38,70 @@ public interface Value {
* @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
*/
@SuppressWarnings("PMD.LawOfDemeter")
static <T> T where(
final boolean clause,
final Supplier<T> trueSupplier,
final Supplier<T> falseSupplier
) {
return Value.<T>where(clause).then(trueSupplier)
.otherwise(falseSupplier);
) {
return Value.<T>where(clause)
.then(trueSupplier)
.otherwise(falseSupplier);
}
/**
* 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(
final Condition clause,
final Supplier<T> trueSupplier,
final Supplier<T> falseSupplier
) {
return Value.<T>where(clause.isTrue(), trueSupplier, 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
*
* @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(
final boolean clause,
final Supplier<T> trueSupplier
) {
) {
return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null));
}
/**
* 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(
final Condition clause,
final Supplier<T> trueSupplier
) {
return Value.where(clause.isTrue(), trueSupplier);
}
/**
* Create a new {@link ValueClause} for the clause.
*
* @param clause the condition to test
* @param <T> the type of the value
*
* @return a true or false value clause
*/
@SuppressWarnings("unchecked")
@ -83,14 +112,27 @@ public interface Value {
return (ValueClause<T>) FalseValueClause.FALSE;
}
/**
* Create a new {@link ValueClause} for the clause.
*
* @param clause the condition to test
* @param <T> the type of the value
* @return a true or false value clause
*/
@SuppressWarnings("unchecked")
static <T> ValueClause<T> where(final Condition clause) {
return Value.where(clause.isTrue());
}
/**
* Create a new {@link ValueClause} for the boolean opposite of the clause.
*
* @param clause the condition to test
* @param <T> the type of the value
*
* @return a true or false value clause
* @deprecated use {@link #where(boolean)}.{@link ValueClause#not()}
*/
@Deprecated
static <T> ValueClause<T> whereNot(final boolean clause) {
return where(!clause);
}
@ -102,11 +144,17 @@ public interface Value {
*/
/* default */ interface ValueClause<T> {
/**
* Negate the Value.
*
* @return a new ValueClause with a negated value
*/
ValueClause<T> not();
/**
* Create a {@link ValueSupplier} with the {@link Supplier} should the {@link ValueClause} be true.
*
* @param trueSupplier the Supplier for the true value
*
* @return the value supplier
*/
ValueSupplier<T> then(Supplier<T> trueSupplier);
@ -115,7 +163,6 @@ public interface Value {
* Logically AND combine the current {@link ValueClause} with clause.
*
* @param clause the condition to test
*
* @return a true or false value clause
*/
ValueClause<T> and(Supplier<Boolean> clause);
@ -124,7 +171,6 @@ public interface Value {
* Logically OR combine the current {@link ValueClause} with clause.
*
* @param clause the condition to test
*
* @return a true or false value clause
*/
@SuppressWarnings("PMD.ShortMethodName")
@ -134,7 +180,6 @@ public interface Value {
* Logically AND combine the current {@link ValueClause} with boolean opposite of the clause.
*
* @param clause the condition to test
*
* @return a true or false value clause
*/
default ValueClause<T> andNot(final Supplier<Boolean> clause) {
@ -145,7 +190,6 @@ public interface Value {
* Logically OR combine the current {@link ValueClause} with boolean opposite of the clause.
*
* @param clause the condition to test
*
* @return a true or false value clause
*/
default ValueClause<T> orNot(final Supplier<Boolean> clause) {
@ -163,7 +207,6 @@ public interface Value {
* Determine the value by whether the {@link ValueClause} was true or false.
*
* @param falseSupplier the Supplier for the false value
*
* @return the value
*/
T otherwise(Supplier<T> falseSupplier);

View file

@ -14,13 +14,15 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ValueTest {
private static final String TRUE = "true";
private static final String FALSE = "false";
private static final Condition TRUE_CONDITION = Condition.where(true);
private static final Condition FALSE_CONDITION = Condition.where(false);
@Test
public void valueWhereClauseIsTrueTypeSafe() {
//when
final String result = Value.where(true, () -> TRUE, () -> FALSE);
final String result = Value.where(TRUE_CONDITION, () -> TRUE, () -> FALSE);
//then
assertThat(result).isEqualTo(TRUE);
}
@ -28,7 +30,7 @@ public class ValueTest {
@Test
public void valueWhereClauseIsFalseTypeSafe() {
//when
final String result = Value.where(false, () -> TRUE, () -> FALSE);
final String result = Value.where(FALSE_CONDITION, () -> TRUE, () -> FALSE);
//then
assertThat(result).isEqualTo(FALSE);
}
@ -36,7 +38,7 @@ public class ValueTest {
@Test
public void valueWhereClauseIsTrueIsOptional() {
//when
final Optional<String> result = Value.where(true, () -> TRUE);
final Optional<String> result = Value.where(TRUE_CONDITION, () -> TRUE);
//then
assertThat(result).contains(TRUE);
}
@ -44,7 +46,7 @@ public class ValueTest {
@Test
public void valueWhereClauseIsFalseIsEmptyOptional() {
//when
final Optional<String> result = Value.where(false, () -> TRUE);
final Optional<String> result = Value.where(FALSE_CONDITION, () -> TRUE);
//then
assertThat(result).isEmpty();
}
@ -52,7 +54,7 @@ public class ValueTest {
@Test
public void valueWhereClauseIsTrue() {
//when
val result = Value.<String>where(true).then(() -> TRUE)
val result = Value.<String>where(TRUE_CONDITION).then(() -> TRUE)
.otherwise(() -> FALSE);
//then
assertThat(result).isEqualTo(TRUE);
@ -61,7 +63,7 @@ public class ValueTest {
@Test
public void valueWhereClauseIsFalse() {
//when
val result = Value.<String>where(false).then(() -> TRUE)
val result = Value.<String>where(FALSE_CONDITION).then(() -> TRUE)
.otherwise(() -> FALSE);
//then
assertThat(result).isEqualTo(FALSE);
@ -70,7 +72,7 @@ public class ValueTest {
@Test
public void valueWhereTrueAndTrueIsTrue() {
//when
val result = Value.<String>where(true).and(() -> true)
val result = Value.<String>where(TRUE_CONDITION).and(() -> true)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -80,7 +82,7 @@ public class ValueTest {
@Test
public void valueWhereTrueAndFalseIsFalse() {
//when
val result = Value.<String>where(true).and(() -> false)
val result = Value.<String>where(TRUE_CONDITION).and(() -> false)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -90,7 +92,7 @@ public class ValueTest {
@Test
public void valueWhereFalseAndTrueIsFalse() {
//when
val result = Value.<String>where(false).and(() -> true)
val result = Value.<String>where(FALSE_CONDITION).and(() -> true)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -100,7 +102,7 @@ public class ValueTest {
@Test
public void valueWhereFalseAndFalseIsFalse() {
//when
val result = Value.<String>where(false).and(() -> false)
val result = Value.<String>where(FALSE_CONDITION).and(() -> false)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -110,7 +112,7 @@ public class ValueTest {
@Test
public void valueWhereTrueOrTrueIsTrue() {
//when
val result = Value.<String>where(true).or(() -> true)
val result = Value.<String>where(TRUE_CONDITION).or(() -> true)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -120,7 +122,7 @@ public class ValueTest {
@Test
public void valueWhereTrueOrFalseIsTrue() {
//when
val result = Value.<String>where(true).or(() -> false)
val result = Value.<String>where(TRUE_CONDITION).or(() -> false)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -130,7 +132,7 @@ public class ValueTest {
@Test
public void valueWhereFalseOrTrueIsTrue() {
//when
val result = Value.<String>where(false).or(() -> true)
val result = Value.<String>where(FALSE_CONDITION).or(() -> true)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -140,7 +142,7 @@ public class ValueTest {
@Test
public void valueWhereFalseOrFalseIsFalse() {
//when
val result = Value.<String>where(false).or(() -> false)
val result = Value.<String>where(FALSE_CONDITION).or(() -> false)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -157,7 +159,7 @@ public class ValueTest {
}
@Test
public void valueWhereNotFalseIsTrue() {
public void valueWhereNotFalseIsTrue_deprecated() {
//when
val result = Value.<String>whereNot(false).then(() -> TRUE)
.otherwise(() -> FALSE);
@ -165,10 +167,19 @@ public class ValueTest {
assertThat(result).isEqualTo(TRUE);
}
@Test
public void valueWhereNotFalseIsTrue() {
//when
val result = Value.<String>where(false).not().then(() -> TRUE)
.otherwise(() -> FALSE);
//then
assertThat(result).isEqualTo(TRUE);
}
@Test
public void valueWhereTrueAndNotTrueIsFalse() {
//when
val result = Value.<String>where(true).andNot(() -> true)
val result = Value.<String>where(TRUE_CONDITION).andNot(() -> true)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -178,7 +189,7 @@ public class ValueTest {
@Test
public void valueWhereTrueAndNotFalseIsTrue() {
//when
val result = Value.<String>where(true).andNot(() -> false)
val result = Value.<String>where(TRUE_CONDITION).andNot(() -> false)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -188,7 +199,7 @@ public class ValueTest {
@Test
public void valueWhereFalseAndNotTrueIsFalse() {
//when
val result = Value.<String>where(false).andNot(() -> true)
val result = Value.<String>where(FALSE_CONDITION).and(() -> true)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -198,7 +209,7 @@ public class ValueTest {
@Test
public void valueWhereFalseAndNotFalseIsFalse() {
//when
val result = Value.<String>where(false).andNot(() -> false)
val result = Value.<String>where(FALSE_CONDITION).and(() -> false)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -218,17 +229,27 @@ public class ValueTest {
@Test
public void valueWhereTrueOrNotFalseIsTrue() {
//when
val result = Value.<String>where(true).orNot(() -> false)
val result = Value.<String>where(TRUE_CONDITION).orNot(() -> false)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
assertThat(result).isEqualTo(TRUE);
}
@Test
public void valueWhereFalseOrNotTrueIsFalse_deprecated() {
//when
val result = Value.<String>where(FALSE_CONDITION).orNot(() -> true)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
assertThat(result).isEqualTo(FALSE);
}
@Test
public void valueWhereFalseOrNotTrueIsFalse() {
//when
val result = Value.<String>where(false).orNot(() -> true)
val result = Value.<String>where(FALSE_CONDITION).or(() -> true).not()
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
@ -248,7 +269,7 @@ public class ValueTest {
@Test
public void valueWhereTrueThenIsNotEmpty() {
//given
final Optional<Object> result = Value.where(true).then(() -> "value").optional();
final Optional<Object> result = Value.where(TRUE_CONDITION).then(() -> "value").optional();
//then
assertThat(result).contains("value");
}
@ -256,17 +277,17 @@ public class ValueTest {
@Test
public void valueWhereFalseThenIsEmpty() {
//given
final Optional<Object> result = Value.where(false).then(() -> "value").optional();
final Optional<Object> result = Value.where(FALSE_CONDITION).then(() -> "value").optional();
//when
assertThat(result).isEmpty();
}
@Test
public void shortCurcuitOr() {
public void shortCircuitOr() {
//given
final AtomicInteger atomicInteger = new AtomicInteger();
//when
final Optional<String> result = Value.<String>where(true)
final Optional<String> result = Value.<String>where(TRUE_CONDITION)
.or(() -> atomicInteger.compareAndSet(0, 2))
.then(() -> "Pass")
.optional();
@ -276,11 +297,11 @@ public class ValueTest {
}
@Test
public void shortCurcuitAnd() {
public void shortCircuitAnd() {
//given
final AtomicInteger atomicInteger = new AtomicInteger();
//when
final Optional<String> result = Value.<String>where(false)
final Optional<String> result = Value.<String>where(FALSE_CONDITION)
.and(() -> atomicInteger.compareAndSet(0, 2))
.then(() -> "Pass")
.optional();