Value: secondary clauses now use Supplier's to enable short circuiting

This commit is contained in:
Paul Campbell 2018-03-13 17:53:16 +00:00
parent efc1705a6c
commit ac3e253bf2
5 changed files with 180 additions and 98 deletions

View file

@ -59,7 +59,7 @@ if (isTrue() && isAlsoTrue()) {
[[source,java]] [[source,java]]
---- ----
Condition.where(isTrue()) Condition.where(isTrue())
.and(isAlsoTrue()) .and(() -> isAlsoTrue())
.then(() -> doSomething()) .then(() -> doSomething())
.otherwise(() -> doSomethingElse()); .otherwise(() -> doSomethingElse());
---- ----
@ -78,7 +78,7 @@ if (isTrue() || alternativeIsTrue()) {
[[source,java]] [[source,java]]
---- ----
Condition.where(isTrue()) Condition.where(isTrue())
.or(alternativeIsTrue()) .or(() -> alternativeIsTrue())
.then(() -> doSomething()) .then(() -> doSomething())
.otherwise(() -> doSomethingElse()); .otherwise(() -> doSomethingElse());
---- ----
@ -115,7 +115,7 @@ if (isTrue() || !isFalse()) {
[[source,java]] [[source,java]]
---- ----
Condition.where(isTrue()) Condition.where(isTrue())
.andNot(isFalse()) .andNot(() -> isFalse())
.then(() -> doSomething()) .then(() -> doSomething())
.otherwise(() -> doSomethingElse()); .otherwise(() -> doSomethingElse());
---- ----
@ -134,7 +134,7 @@ if (isFalse() || !isAlsoFalse()) {
[[source,java]] [[source,java]]
---- ----
Condition.where(isFalse()) Condition.where(isFalse())
.orNot(isAlsoFalse()) .orNot(() -> isAlsoFalse())
.then(() -> doSomething()) .then(() -> doSomething())
.otherwise(() -> doSomethingElse()); .otherwise(() -> doSomethingElse());
---- ----
@ -154,7 +154,7 @@ if (isFalse()) {
---- ----
Condition.where(isFalse()) Condition.where(isFalse())
.then(() -> doSomething()) .then(() -> doSomething())
.otherwise(isTrue()) .otherwise(() -> isTrue())
.then(() -> doSomethingElse()); .then(() -> doSomethingElse());
---- ----
@ -174,7 +174,7 @@ if (isTrue()) {
---- ----
Condition.where(isTrue()) Condition.where(isTrue())
.then(() -> doSomething()) .then(() -> doSomething())
.and(isAlsoTrue()) .and(() -> isAlsoTrue())
.then(() -> doSomethingElse()); .then(() -> doSomethingElse());
---- ----
@ -213,7 +213,8 @@ 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)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
---- ----
@ -231,7 +232,8 @@ if (!isTrue()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>whereNot(isTrue()).then(() -> TRUE) final String result = Value.<String>whereNot(isTrue())
.then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
---- ----
@ -249,7 +251,8 @@ if (isTrue() && alternativeIsTrue()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>where(isTrue()).and(alternativeIsTrue()) final String result = Value.<String>where(isTrue())
.and(() -> alternativeIsTrue())
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
---- ----
@ -268,7 +271,8 @@ if (isTrue() && !alternativeIsFalse()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>where(isTrue()).andNot(alternativeIsFalse()) final String result = Value.<String>where(isTrue())
.andNot(() -> alternativeIsFalse())
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
---- ----
@ -287,7 +291,8 @@ if (isTrue() || alternativeIsTrue()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>where(isTrue()).or(alternativeIsTrue()) final String result = Value.<String>where(isTrue())
.or(() -> alternativeIsTrue())
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
---- ----
@ -306,7 +311,27 @@ if (isTrue() || !isFalse()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>where(isTrue()).orNot(isFalse()) final String result = Value.<String>where(isTrue())
.orNot(() -> isFalse())
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
---- ----
### if-then
[[source,java]]
-----
Optional<String> result;
if (isTrue()) {
result = Optional.of(TRUE);
} else {
result = Optional.empty();
}
-----
[[source,java]]
-----
final Optional<String> result = Value.<String>where(isTrue())
.then(() -> TRUE)
.optional();
-----

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;
/** /**
@ -40,14 +41,13 @@ class FalseValueClause<T> implements Value.ValueClause<T> {
} }
@Override @Override
public Value.ValueClause<T> and(final boolean clause) { public Value.ValueClause<T> and(final Supplier<Boolean> clause) {
return this; return this;
} }
@Override @Override
@SuppressWarnings("PMD.ShortMethodName") public Value.ValueClause<T> or(final Supplier<Boolean> clause) {
public Value.ValueClause<T> or(final boolean clause) { return Value.where(clause.get());
return Value.where(clause);
} }
/** /**
@ -62,6 +62,11 @@ class FalseValueClause<T> implements Value.ValueClause<T> {
return falseSupplier.get(); return falseSupplier.get();
} }
@Override
public Optional<T> optional() {
return Optional.empty();
}
} }
} }

View file

@ -23,6 +23,7 @@ package net.kemitix.conditional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -42,13 +43,12 @@ class TrueValueClause<T> implements Value.ValueClause<T> {
} }
@Override @Override
public Value.ValueClause<T> and(final boolean clause) { public Value.ValueClause<T> and(final Supplier<Boolean> clause) {
return Value.where(clause); return Value.where(clause.get());
} }
@Override @Override
@SuppressWarnings("PMD.ShortMethodName") public Value.ValueClause<T> or(final Supplier<Boolean> clause) {
public Value.ValueClause<T> or(final boolean clause) {
return this; return this;
} }
@ -60,7 +60,6 @@ class TrueValueClause<T> implements Value.ValueClause<T> {
@RequiredArgsConstructor @RequiredArgsConstructor
private static final class TrueValueSupplier<T> implements ValueSupplier<T> { private static final class TrueValueSupplier<T> implements ValueSupplier<T> {
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
private final Supplier<T> valueSupplier; private final Supplier<T> valueSupplier;
@Override @Override
@ -68,6 +67,11 @@ class TrueValueClause<T> implements Value.ValueClause<T> {
return valueSupplier.get(); return valueSupplier.get();
} }
@Override
public Optional<T> optional() {
return Optional.ofNullable(valueSupplier.get());
}
} }
} }

View file

@ -41,11 +41,10 @@ public interface 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(
boolean clause, final boolean clause,
Supplier<T> trueSupplier, final Supplier<T> trueSupplier,
Supplier<T> falseSupplier final Supplier<T> falseSupplier
) { ) {
return Value.<T>where(clause).then(trueSupplier) return Value.<T>where(clause).then(trueSupplier)
.otherwise(falseSupplier); .otherwise(falseSupplier);
@ -61,8 +60,8 @@ public interface 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(
boolean clause, final boolean clause,
Supplier<T> trueSupplier final Supplier<T> trueSupplier
) { ) {
return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null)); return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null));
} }
@ -75,7 +74,6 @@ public interface Value {
* *
* @return a true or false value clause * @return a true or false value clause
*/ */
@SuppressWarnings({"unchecked", "avoidinlineconditionals"})
static <T> ValueClause<T> where(final boolean clause) { static <T> ValueClause<T> where(final boolean clause) {
return (ValueClause<T>) (clause ? TrueValueClause.TRUE : FalseValueClause.FALSE); return (ValueClause<T>) (clause ? TrueValueClause.TRUE : FalseValueClause.FALSE);
} }
@ -88,7 +86,7 @@ public interface Value {
* *
* @return a true or false value clause * @return a true or false value clause
*/ */
static <T> ValueClause<T> whereNot(boolean clause) { static <T> ValueClause<T> whereNot(final boolean clause) {
return where(!clause); return where(!clause);
} }
@ -115,7 +113,7 @@ public interface Value {
* *
* @return a true or false value clause * @return a true or false value clause
*/ */
ValueClause<T> and(boolean clause); ValueClause<T> and(Supplier<Boolean> clause);
/** /**
* Logically OR combine the current {@link ValueClause} with clause. * Logically OR combine the current {@link ValueClause} with clause.
@ -124,8 +122,7 @@ public interface Value {
* *
* @return a true or false value clause * @return a true or false value clause
*/ */
@SuppressWarnings("PMD.ShortMethodName") ValueClause<T> or(Supplier<Boolean> clause);
ValueClause<T> or(boolean clause);
/** /**
* 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.
@ -134,8 +131,8 @@ public interface Value {
* *
* @return a true or false value clause * @return a true or false value clause
*/ */
default ValueClause<T> andNot(final boolean clause) { default ValueClause<T> andNot(final Supplier<Boolean> clause) {
return and(!clause); return and(() -> !clause.get());
} }
/** /**
@ -145,8 +142,8 @@ public interface Value {
* *
* @return a true or false value clause * @return a true or false value clause
*/ */
default ValueClause<T> orNot(boolean clause) { default ValueClause<T> orNot(final Supplier<Boolean> clause) {
return or(!clause); return or(() -> !clause.get());
} }
/** /**
@ -165,6 +162,12 @@ public interface Value {
*/ */
T otherwise(Supplier<T> falseSupplier); T otherwise(Supplier<T> falseSupplier);
/**
* Returns the value in an Optional if the {@link ValueClause} is true, or an empty Optional if it is false.
*
* @return an Optional, possibly containing the value
*/
Optional<T> optional();
} }
} }

View file

@ -4,6 +4,7 @@ import lombok.val;
import org.junit.Test; import org.junit.Test;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -69,7 +70,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).and(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -79,7 +80,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).and(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -89,7 +90,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).and(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -99,7 +100,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).and(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -109,7 +110,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).or(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -119,7 +120,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).or(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -129,7 +130,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).or(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -139,7 +140,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).or(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -167,7 +168,7 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueAndNotTrueIsFalse() { public void valueWhereTrueAndNotTrueIsFalse() {
//when //when
val result = Value.<String>where(true).andNot(true) val result = Value.<String>where(true).andNot(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -177,7 +178,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).andNot(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -187,7 +188,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).andNot(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -197,7 +198,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).andNot(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -207,7 +208,7 @@ public class ValueTest {
@Test @Test
public void valueWhereTrueOrNotTrueIsTrue() { public void valueWhereTrueOrNotTrueIsTrue() {
//when //when
val result = Value.<String>where(true).orNot(true) val result = Value.<String>where(true).orNot(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -217,7 +218,7 @@ 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).orNot(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -227,7 +228,7 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseOrNotTrueIsFalse() { public void valueWhereFalseOrNotTrueIsFalse() {
//when //when
val result = Value.<String>where(false).orNot(true) val result = Value.<String>where(false).orNot(() -> true)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
@ -237,10 +238,54 @@ public class ValueTest {
@Test @Test
public void valueWhereFalseOrNotFalseIsTrue() { public void valueWhereFalseOrNotFalseIsTrue() {
//when //when
val result = Value.<String>where(false).orNot(false) val result = Value.<String>where(false).orNot(() -> false)
.then(() -> TRUE) .then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@Test
public void valueWhereTrueThenIsNotEmpty() {
//given
final Optional<Object> result = Value.where(true).then(() -> "value").optional();
//then
assertThat(result).contains("value");
}
@Test
public void valueWhereFalseThenIsEmpty() {
//given
final Optional<Object> result = Value.where(false).then(() -> "value").optional();
//when
assertThat(result).isEmpty();
}
@Test
public void shortCurcuitOr() {
//given
final AtomicInteger atomicInteger = new AtomicInteger();
//when
final Optional<String> result = Value.<String>where(true)
.or(() -> atomicInteger.compareAndSet(0, 2))
.then(() -> "Pass")
.optional();
//then
assertThat(result).contains("Pass");
assertThat(atomicInteger).hasValue(0);
}
@Test
public void shortCurcuitAnd() {
//given
final AtomicInteger atomicInteger = new AtomicInteger();
//when
final Optional<String> result = Value.<String>where(false)
.and(() -> atomicInteger.compareAndSet(0, 2))
.then(() -> "Pass")
.optional();
//then
assertThat(result).isEmpty();
assertThat(atomicInteger).hasValue(0);
}
} }