Merge pull request #32 from kemitix/bind

Make Condition a monad and accept Condition as parameter
This commit is contained in:
Paul Campbell 2018-07-28 17:36:14 +01:00 committed by GitHub
commit 516d6daf33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 364 additions and 80 deletions

3
.gitmodules vendored
View file

@ -1,3 +0,0 @@
[submodule ".travis-support"]
path = .travis-support
url = https://github.com/kemitix/kemitix-travis-support.git

@ -1 +0,0 @@
Subproject commit b8593e541ba9a11447fa9559a83e5f99097ca4d2

View file

@ -5,14 +5,4 @@ cache:
directories: directories:
- "$HOME/.m2" - "$HOME/.m2"
install: true install: true
script: "./mvnw -B -U clean install" script: "mvn -B -U clean install"
after_success:
- sh .travis-support/coveralls.sh
- bash <(curl -s https://codecov.io/bash)
deploy:
provider: script
script: sh .travis-support/deploy.sh
on:
branch: master
env:
global:

View file

@ -1,6 +1,16 @@
CHANGELOG CHANGELOG
========= =========
0.6.0
-----
* Add `Condition.and(Condition)`
* Add `Condition.or(Condition)`
* Add `Condition.not()`
* Deprecate `Condition.whereNot(boolean)`
* Deprecate `Condition.andNot(boolean)`
* Deprecate `Condition.orNot(boolean)`
0.5.0 0.5.0
----- -----

View file

@ -21,6 +21,7 @@
package net.kemitix.conditional; package net.kemitix.conditional;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -35,7 +36,6 @@ public interface Condition {
* Create a new {@code Condition} for the clause. * Create a new {@code Condition} for the clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return the Condition * @return the Condition
*/ */
static Condition where(final boolean clause) { static Condition where(final boolean clause) {
@ -49,59 +49,102 @@ public interface Condition {
* Create a new {@code Condition} for the boolean opposite of the clause. * Create a new {@code Condition} for the boolean opposite of the clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return the Condition * @return the Condition
* @deprecated use {@link #not()}
*/ */
@Deprecated
static Condition whereNot(final boolean clause) { static Condition whereNot(final boolean clause) {
return where(!clause); return where(clause).not();
}
/**
* Checks if the Condition is true or not.
*
* @return true if the Condition is true
*/
boolean isTrue();
/**
* Checks of the Condition is false or not.
*
* @return true if the Condition is false
*/
boolean isFalse();
/**
* Negates the Condtion.
*
* @return a false Condition if the Condition is true, or a true Condition if the Condition is false.
*/
default Condition not() {
return Condition.where(isFalse());
} }
/** /**
* Logically AND combine the current {@code Condition} with the clause. * Logically AND combine the current {@code Condition} with the clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return the Condition * @return the Condition
*/ */
Condition and(Supplier<Boolean> clause); Condition and(Supplier<Boolean> clause);
/**
* Logicaly OR current {@code Condition} with the other {@code Condition}.
*
* @param other the other Condition
* @return true if both Conditions are true
*/
default Condition and(Condition other) {
return Condition.where(isTrue()).and(other::isTrue);
}
/** /**
* Logically AND combine the current {@code Condition} with boolean opposite of the clause. * Logically AND combine the current {@code Condition} with boolean opposite of the clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return the Condition * @return the Condition
* @deprecated use {@link #not()}
*/ */
@Deprecated
default Condition andNot(final Supplier<Boolean> clause) { default Condition andNot(final Supplier<Boolean> clause) {
return and(() -> !clause.get()); return and(clause).not();
} }
/** /**
* Logically OR combine the current {@code Condition} with the clause. * Logically OR combine the current {@code Condition} with the clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return the Condition * @return the Condition
*/ */
@SuppressWarnings("PMD.ShortMethodName") @SuppressWarnings("PMD.ShortMethodName")
Condition or(Supplier<Boolean> clause); Condition or(Supplier<Boolean> clause);
/**
* Logically OR the current {@code Condition} with the other {@code Condition}.
*
* @param other the other Condition
* @return true if either Condition is true
*/
default Condition or(Condition other) {
return where(isTrue()).or(other::isTrue);
}
/** /**
* Logically OR combine the current {@code Condition} with the boolean opposite of the clause. * Logically OR combine the current {@code Condition} with the boolean opposite of the clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return the Condition * @return the Condition
* @deprecated use {@link #not()}
*/ */
@Deprecated
default Condition orNot(final Supplier<Boolean> clause) { default Condition orNot(final Supplier<Boolean> clause) {
return or(() -> !clause.get()); return or(clause).not();
} }
/** /**
* Perform this response if the {@code Condition} is {@code true}. * Perform this response if the {@code Condition} is {@code true}.
* *
* @param response the response to perform * @param response the response to perform
*
* @return the Condition * @return the Condition
*/ */
Condition then(Action response); Condition then(Action response);
@ -117,7 +160,6 @@ public interface Condition {
* Create a new {@code Condition} for the clause as a continuation to an existing {@code Condition}. * Create a new {@code Condition} for the clause as a continuation to an existing {@code Condition}.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return the Condition * @return the Condition
*/ */
default Condition otherwise(final Supplier<Boolean> clause) { default Condition otherwise(final Supplier<Boolean> clause) {
@ -141,4 +183,14 @@ public interface Condition {
*/ */
@SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS)
void otherwiseThrow(Exception exception) throws Exception; void otherwiseThrow(Exception exception) throws Exception;
/**
* Apply the function to the Condtion, resulting an another Condition.
*
* @param f the function to apply
* @return a new Condition
*/
default Condition flatMap(final Function<Boolean, Condition> f) {
return f.apply(isTrue());
}
} }

View file

@ -62,4 +62,15 @@ final class FalseCondition implements Condition {
public void otherwiseThrow(final Exception exception) throws Exception { public void otherwiseThrow(final Exception exception) throws Exception {
throw exception; throw exception;
} }
@Override
public boolean isTrue() {
return false;
}
@Override
public boolean isFalse() {
return true;
}
} }

View file

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

View file

@ -64,4 +64,14 @@ final class TrueCondition implements Condition {
// do nothing // do nothing
} }
@Override
public boolean isTrue() {
return true;
}
@Override
public boolean isFalse() {
return false;
}
} }

View file

@ -30,13 +30,17 @@ import java.util.function.Supplier;
* An intermediate state where the clause has evaluated to true. * An intermediate state where the clause has evaluated to true.
* *
* @param <T> the type of the value * @param <T> the type of the value
*
* @author Paul Campbell (pcampbell@kemitix.net). * @author Paul Campbell (pcampbell@kemitix.net).
*/ */
class TrueValueClause<T> implements Value.ValueClause<T> { class TrueValueClause<T> implements Value.ValueClause<T> {
protected static final Value.ValueClause<?> TRUE = new TrueValueClause<>(); protected static final Value.ValueClause<?> TRUE = new TrueValueClause<>();
@Override
public Value.ValueClause<T> not() {
return Value.where(false);
}
@Override @Override
public ValueSupplier<T> then(final Supplier<T> trueSupplier) { public ValueSupplier<T> then(final Supplier<T> trueSupplier) {
return new TrueValueSupplier<>(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 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 falseSupplier The supplier to provide the value when the clause is false
* @param <T> The type of the value * @param <T> The type of the value
*
* @return the value from either the trueSupplier or the falseSupplier * @return the value from either the trueSupplier or the falseSupplier
*/ */
@SuppressWarnings("PMD.LawOfDemeter")
static <T> T where( static <T> T where(
final boolean clause, final boolean clause,
final Supplier<T> trueSupplier, final Supplier<T> trueSupplier,
final Supplier<T> falseSupplier final Supplier<T> falseSupplier
) { ) {
return Value.<T>where(clause).then(trueSupplier) return Value.<T>where(clause)
.otherwise(falseSupplier); .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. * Return an Optional either containing a value, if the clause is true, or empty.
* *
* @param clause The deciding clause * @param clause The deciding clause
* @param trueSupplier The supplier to provide the value when the clause is true * @param trueSupplier The supplier to provide the value when the clause is true
* @param <T> The type of the value * @param <T> The type of the value
*
* @return an Optional either containing the value from the trueSupplier or empty * @return an Optional either containing the value from the trueSupplier or empty
*/ */
static <T> Optional<T> where( static <T> Optional<T> where(
final boolean clause, final boolean clause,
final Supplier<T> trueSupplier final Supplier<T> trueSupplier
) { ) {
return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null)); 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. * Create a new {@link ValueClause} for the clause.
* *
* @param clause the condition to test * @param clause the condition to test
* @param <T> the type of the value * @param <T> the type of the value
*
* @return a true or false value clause * @return a true or false value clause
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -83,14 +112,27 @@ public interface Value {
return (ValueClause<T>) FalseValueClause.FALSE; 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. * Create a new {@link ValueClause} for the boolean opposite of the clause.
* *
* @param clause the condition to test * @param clause the condition to test
* @param <T> the type of the value * @param <T> the type of the value
*
* @return a true or false value clause * @return a true or false value clause
* @deprecated use {@link #where(boolean)}.{@link ValueClause#not()}
*/ */
@Deprecated
static <T> ValueClause<T> whereNot(final boolean clause) { static <T> ValueClause<T> whereNot(final boolean clause) {
return where(!clause); return where(!clause);
} }
@ -102,11 +144,17 @@ public interface Value {
*/ */
/* default */ interface ValueClause<T> { /* 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. * Create a {@link ValueSupplier} with the {@link Supplier} should the {@link ValueClause} be true.
* *
* @param trueSupplier the Supplier for the true value * @param trueSupplier the Supplier for the true value
*
* @return the value supplier * @return the value supplier
*/ */
ValueSupplier<T> then(Supplier<T> trueSupplier); ValueSupplier<T> then(Supplier<T> trueSupplier);
@ -115,7 +163,6 @@ public interface Value {
* Logically AND combine the current {@link ValueClause} with clause. * Logically AND combine the current {@link ValueClause} with clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return a true or false value clause * @return a true or false value clause
*/ */
ValueClause<T> and(Supplier<Boolean> clause); ValueClause<T> and(Supplier<Boolean> clause);
@ -124,7 +171,6 @@ public interface Value {
* Logically OR combine the current {@link ValueClause} with clause. * Logically OR combine the current {@link ValueClause} with clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return a true or false value clause * @return a true or false value clause
*/ */
@SuppressWarnings("PMD.ShortMethodName") @SuppressWarnings("PMD.ShortMethodName")
@ -134,7 +180,6 @@ public interface Value {
* Logically AND combine the current {@link ValueClause} with boolean opposite of the clause. * Logically AND combine the current {@link ValueClause} with boolean opposite of the clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return a true or false value clause * @return a true or false value clause
*/ */
default ValueClause<T> andNot(final Supplier<Boolean> 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. * Logically OR combine the current {@link ValueClause} with boolean opposite of the clause.
* *
* @param clause the condition to test * @param clause the condition to test
*
* @return a true or false value clause * @return a true or false value clause
*/ */
default ValueClause<T> orNot(final Supplier<Boolean> 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. * Determine the value by whether the {@link ValueClause} was true or false.
* *
* @param falseSupplier the Supplier for the false value * @param falseSupplier the Supplier for the false value
*
* @return the value * @return the value
*/ */
T otherwise(Supplier<T> falseSupplier); T otherwise(Supplier<T> falseSupplier);

View file

@ -0,0 +1,45 @@
package net.kemitix.conditional;
import org.assertj.core.api.WithAssertions;
import org.junit.Test;
import java.util.function.Function;
public class ConditionMonadTest implements WithAssertions {
private final boolean v = true;
private final Function<Boolean, Condition> f = i -> r(true);
private final Function<Boolean, Condition> g = i -> r(i);
private static Condition r(boolean v) {
return Condition.where(v);
}
@Test
public void leftIdentity() {
assertThat(
r(v).flatMap(f)
).isEqualTo(
f.apply(v)
);
}
@Test
public void rightIdentity() {
assertThat(
r(v).flatMap(x -> r(x))
).isEqualTo(
r(v)
);
}
@Test
public void associativity() {
assertThat(
r(v).flatMap(f).flatMap(g)
).isEqualTo(
r(v).flatMap(x -> f.apply(x).flatMap(g))
);
}
}

View file

@ -13,12 +13,13 @@ import java.util.concurrent.atomic.AtomicInteger;
public class ConditionalTest implements WithAssertions { public class ConditionalTest implements WithAssertions {
private Action thenResponse; private Action thenResponse;
private Action otherwiseResponse; private Action otherwiseResponse;
private boolean thenFlag; private boolean thenFlag;
private boolean otherwiseFlag; private boolean otherwiseFlag;
private final org.assertj.core.api.Condition<? super Condition> trueCondition =
new org.assertj.core.api.Condition<>(Condition::isTrue, "is true");
private final org.assertj.core.api.Condition<? super Condition> falseCondition =
new org.assertj.core.api.Condition<>(Condition::isFalse, "is false");
@Before @Before
public void setUp() { public void setUp() {
@ -343,4 +344,102 @@ public class ConditionalTest implements WithAssertions {
.otherwiseThrow(new IOException())) .otherwiseThrow(new IOException()))
.doesNotThrowAnyException(); .doesNotThrowAnyException();
} }
@Test
public void whereTrueConditionAndTrueConditionThenTrueCondition() {
//given
final Condition condition1 = Condition.where(true);
final Condition condition2 = Condition.where(true);
//when
final Condition result = condition1.and(condition2);
//then
assertThat(result).is(trueCondition);
}
@Test
public void whereTrueConditionAndFalseConditionThenFalseCondition() {
//given
final Condition condition1 = Condition.where(true);
final Condition condition2 = Condition.where(false);
//when
final Condition result = condition1.and(condition2);
//then
assertThat(result).is(falseCondition);
}
@Test
public void whereFalseConditionAndTrueConditionThenFalseCondition() {
//given
final Condition condition1 = Condition.where(false);
final Condition condition2 = Condition.where(true);
//when
final Condition result = condition1.and(condition2);
//then
assertThat(result).is(falseCondition);
}
@Test
public void whereFalseConditionAndFalseConditionThenFalseCondition() {
//given
final Condition condition1 = Condition.where(false);
final Condition condition2 = Condition.where(false);
//when
final Condition result = condition1.and(condition2);
//then
assertThat(result).is(falseCondition);
}
@Test
public void whereTrueConditionOrTrueConditionThenTrueCondition() {
//given
final Condition condition1 = Condition.where(true);
final Condition condition2 = Condition.where(true);
//when
final Condition result = condition1.or(condition2);
//then
assertThat(result).is(trueCondition);
}
@Test
public void whereTrueConditionOrFalseConditionThenTrueCondition() {
//given
final Condition condition1 = Condition.where(true);
final Condition condition2 = Condition.where(false);
//when
final Condition result = condition1.or(condition2);
//then
assertThat(result).is(trueCondition);
}
@Test
public void whereFalseConditionOrTrueConditionThenTrueCondition() {
//given
final Condition condition1 = Condition.where(false);
final Condition condition2 = Condition.where(true);
//when
final Condition result = condition1.or(condition2);
//then
assertThat(result).is(trueCondition);
}
@Test
public void whereFalseConditionOrFalseConditionThenFalseCondition() {
//given
final Condition condition1 = Condition.where(false);
final Condition condition2 = Condition.where(false);
//when
final Condition result = condition1.or(condition2);
//then
assertThat(result).is(falseCondition);
}
@Test
public void whereTrueWhenNotThenFalse() {
assertThat(Condition.where(true).not()).is(falseCondition);
}
@Test
public void whereFalseWhenNotThenTriue() {
assertThat(Condition.where(false).not()).is(trueCondition);
}
} }

View file

@ -1,26 +1,27 @@
package net.kemitix.conditional; package net.kemitix.conditional;
import lombok.val; import lombok.val;
import org.assertj.core.api.WithAssertions;
import org.junit.Test; import org.junit.Test;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Paul Campbell (pcampbell@kemitix.net). * @author Paul Campbell (pcampbell@kemitix.net).
*/ */
public class ValueTest { public class ValueTest implements WithAssertions {
private static final String TRUE = "true"; private static final String TRUE = "true";
private static final String FALSE = "false"; 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 @Test
public void valueWhereClauseIsTrueTypeSafe() { public void valueWhereClauseIsTrueTypeSafe() {
//when //when
final String result = Value.where(true, () -> TRUE, () -> FALSE); final String result = Value.where(TRUE_CONDITION, () -> TRUE, () -> FALSE);
//then //then
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -28,7 +29,7 @@ public class ValueTest {
@Test @Test
public void valueWhereClauseIsFalseTypeSafe() { public void valueWhereClauseIsFalseTypeSafe() {
//when //when
final String result = Value.where(false, () -> TRUE, () -> FALSE); final String result = Value.where(FALSE_CONDITION, () -> TRUE, () -> FALSE);
//then //then
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -36,7 +37,7 @@ public class ValueTest {
@Test @Test
public void valueWhereClauseIsTrueIsOptional() { public void valueWhereClauseIsTrueIsOptional() {
//when //when
final Optional<String> result = Value.where(true, () -> TRUE); final Optional<String> result = Value.where(TRUE_CONDITION, () -> TRUE);
//then //then
assertThat(result).contains(TRUE); assertThat(result).contains(TRUE);
} }
@ -44,7 +45,7 @@ public class ValueTest {
@Test @Test
public void valueWhereClauseIsFalseIsEmptyOptional() { public void valueWhereClauseIsFalseIsEmptyOptional() {
//when //when
final Optional<String> result = Value.where(false, () -> TRUE); final Optional<String> result = Value.where(FALSE_CONDITION, () -> TRUE);
//then //then
assertThat(result).isEmpty(); assertThat(result).isEmpty();
} }
@ -52,7 +53,7 @@ public class ValueTest {
@Test @Test
public void valueWhereClauseIsTrue() { public void valueWhereClauseIsTrue() {
//when //when
val result = Value.<String>where(true).then(() -> TRUE) val result = Value.<String>where(TRUE_CONDITION).then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
@ -61,7 +62,7 @@ public class ValueTest {
@Test @Test
public void valueWhereClauseIsFalse() { public void valueWhereClauseIsFalse() {
//when //when
val result = Value.<String>where(false).then(() -> TRUE) val result = Value.<String>where(FALSE_CONDITION).then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
@ -70,7 +71,7 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueAndTrueIsTrue() { public void valueWhereTrueAndTrueIsTrue() {
//when //when
val result = Value.<String>where(true).and(() -> true) val result = Value.<String>where(TRUE_CONDITION).and(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -80,7 +81,7 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueAndFalseIsFalse() { public void valueWhereTrueAndFalseIsFalse() {
//when //when
val result = Value.<String>where(true).and(() -> false) val result = Value.<String>where(TRUE_CONDITION).and(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -90,7 +91,7 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseAndTrueIsFalse() { public void valueWhereFalseAndTrueIsFalse() {
//when //when
val result = Value.<String>where(false).and(() -> true) val result = Value.<String>where(FALSE_CONDITION).and(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -100,7 +101,7 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseAndFalseIsFalse() { public void valueWhereFalseAndFalseIsFalse() {
//when //when
val result = Value.<String>where(false).and(() -> false) val result = Value.<String>where(FALSE_CONDITION).and(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -110,7 +111,7 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueOrTrueIsTrue() { public void valueWhereTrueOrTrueIsTrue() {
//when //when
val result = Value.<String>where(true).or(() -> true) val result = Value.<String>where(TRUE_CONDITION).or(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -120,7 +121,7 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueOrFalseIsTrue() { public void valueWhereTrueOrFalseIsTrue() {
//when //when
val result = Value.<String>where(true).or(() -> false) val result = Value.<String>where(TRUE_CONDITION).or(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -130,7 +131,7 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseOrTrueIsTrue() { public void valueWhereFalseOrTrueIsTrue() {
//when //when
val result = Value.<String>where(false).or(() -> true) val result = Value.<String>where(FALSE_CONDITION).or(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -140,7 +141,7 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseOrFalseIsFalse() { public void valueWhereFalseOrFalseIsFalse() {
//when //when
val result = Value.<String>where(false).or(() -> false) val result = Value.<String>where(FALSE_CONDITION).or(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -157,7 +158,7 @@ public class ValueTest {
} }
@Test @Test
public void valueWhereNotFalseIsTrue() { public void deprecatedValueWhereNotFalseIsTrue() {
//when //when
val result = Value.<String>whereNot(false).then(() -> TRUE) val result = Value.<String>whereNot(false).then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
@ -165,10 +166,19 @@ public class ValueTest {
assertThat(result).isEqualTo(TRUE); 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 @Test
public void valueWhereTrueAndNotTrueIsFalse() { public void valueWhereTrueAndNotTrueIsFalse() {
//when //when
val result = Value.<String>where(true).andNot(() -> true) val result = Value.<String>where(TRUE_CONDITION).andNot(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -178,7 +188,7 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueAndNotFalseIsTrue() { public void valueWhereTrueAndNotFalseIsTrue() {
//when //when
val result = Value.<String>where(true).andNot(() -> false) val result = Value.<String>where(TRUE_CONDITION).andNot(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -188,7 +198,7 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseAndNotTrueIsFalse() { public void valueWhereFalseAndNotTrueIsFalse() {
//when //when
val result = Value.<String>where(false).andNot(() -> true) val result = Value.<String>where(FALSE_CONDITION).and(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -198,7 +208,7 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseAndNotFalseIsFalse() { public void valueWhereFalseAndNotFalseIsFalse() {
//when //when
val result = Value.<String>where(false).andNot(() -> false) val result = Value.<String>where(FALSE_CONDITION).and(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -218,17 +228,27 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueOrNotFalseIsTrue() { public void valueWhereTrueOrNotFalseIsTrue() {
//when //when
val result = Value.<String>where(true).orNot(() -> false) val result = Value.<String>where(TRUE_CONDITION).orNot(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@Test
public void deprecatedValueWhereFalseOrNotTrueIsFalse() {
//when
val result = Value.<String>where(FALSE_CONDITION).orNot(() -> true)
.then(() -> TRUE)
.otherwise(() -> FALSE);
//then
assertThat(result).isEqualTo(FALSE);
}
@Test @Test
public void valueWhereFalseOrNotTrueIsFalse() { public void valueWhereFalseOrNotTrueIsFalse() {
//when //when
val result = Value.<String>where(false).orNot(() -> true) val result = Value.<String>where(FALSE_CONDITION).or(() -> true).not()
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -248,7 +268,7 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueThenIsNotEmpty() { public void valueWhereTrueThenIsNotEmpty() {
//given //given
final Optional<Object> result = Value.where(true).then(() -> "value").optional(); final Optional<Object> result = Value.where(TRUE_CONDITION).then(() -> "value").optional();
//then //then
assertThat(result).contains("value"); assertThat(result).contains("value");
} }
@ -256,17 +276,17 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseThenIsEmpty() { public void valueWhereFalseThenIsEmpty() {
//given //given
final Optional<Object> result = Value.where(false).then(() -> "value").optional(); final Optional<Object> result = Value.where(FALSE_CONDITION).then(() -> "value").optional();
//when //when
assertThat(result).isEmpty(); assertThat(result).isEmpty();
} }
@Test @Test
public void shortCurcuitOr() { public void shortCircuitOr() {
//given //given
final AtomicInteger atomicInteger = new AtomicInteger(); final AtomicInteger atomicInteger = new AtomicInteger();
//when //when
final Optional<String> result = Value.<String>where(true) final Optional<String> result = Value.<String>where(TRUE_CONDITION)
.or(() -> atomicInteger.compareAndSet(0, 2)) .or(() -> atomicInteger.compareAndSet(0, 2))
.then(() -> "Pass") .then(() -> "Pass")
.optional(); .optional();
@ -276,11 +296,11 @@ public class ValueTest {
} }
@Test @Test
public void shortCurcuitAnd() { public void shortCircuitAnd() {
//given //given
final AtomicInteger atomicInteger = new AtomicInteger(); final AtomicInteger atomicInteger = new AtomicInteger();
//when //when
final Optional<String> result = Value.<String>where(false) final Optional<String> result = Value.<String>where(FALSE_CONDITION)
.and(() -> atomicInteger.compareAndSet(0, 2)) .and(() -> atomicInteger.compareAndSet(0, 2))
.then(() -> "Pass") .then(() -> "Pass")
.optional(); .optional();