Merge branch 'develop' into jenkins

This commit is contained in:
Paul Campbell 2018-03-13 22:04:07 +00:00 committed by GitHub
commit d42f8aa769
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 281 additions and 156 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,8 +213,9 @@ 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())
.otherwise(() -> FALSE); .then(() -> TRUE)
.otherwise(() -> FALSE);
---- ----
### if-not-then-else ### if-not-then-else
@ -231,8 +232,9 @@ if (!isTrue()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>whereNot(isTrue()).then(() -> TRUE) final String result = Value.<String>whereNot(isTrue())
.otherwise(() -> FALSE); .then(() -> TRUE)
.otherwise(() -> FALSE);
---- ----
### if-and-then-else ### if-and-then-else
@ -249,9 +251,10 @@ if (isTrue() && alternativeIsTrue()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>where(isTrue()).and(alternativeIsTrue()) final String result = Value.<String>where(isTrue())
.then(() -> TRUE) .and(() -> alternativeIsTrue())
.otherwise(() -> FALSE); .then(() -> TRUE)
.otherwise(() -> FALSE);
---- ----
### if-and-not-then-else ### if-and-not-then-else
@ -268,9 +271,10 @@ if (isTrue() && !alternativeIsFalse()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>where(isTrue()).andNot(alternativeIsFalse()) final String result = Value.<String>where(isTrue())
.then(() -> TRUE) .andNot(() -> alternativeIsFalse())
.otherwise(() -> FALSE); .then(() -> TRUE)
.otherwise(() -> FALSE);
---- ----
### if-or-then-else ### if-or-then-else
@ -287,9 +291,10 @@ if (isTrue() || alternativeIsTrue()) {
[[source,java]] [[source,java]]
---- ----
final String result = Value.<String>where(isTrue()).or(alternativeIsTrue()) final String result = Value.<String>where(isTrue())
.then(() -> TRUE) .or(() -> alternativeIsTrue())
.otherwise(() -> FALSE); .then(() -> TRUE)
.otherwise(() -> FALSE);
---- ----
### if-or-not-then-else ### if-or-not-then-else
@ -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())
.then(() -> TRUE) .orNot(() -> isFalse())
.otherwise(() -> FALSE); .then(() -> TRUE)
.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

@ -16,9 +16,7 @@
<assertj.version>3.9.1</assertj.version> <assertj.version>3.9.1</assertj.version>
<coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version> <coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
<tiles-maven-plugin.version>2.10</tiles-maven-plugin.version> <tiles-maven-plugin.version>2.10</tiles-maven-plugin.version>
<kemitix-tiles.version>0.7.1</kemitix-tiles.version> <kemitix-tiles.version>0.8.1</kemitix-tiles.version>
<maven-surefire-plugin.version>2.20.1</maven-surefire-plugin.version>
<maven-failsafe-plugin.version>2.20.1</maven-failsafe-plugin.version>
<lombok.version>1.16.20</lombok.version> <lombok.version>1.16.20</lombok.version>
</properties> </properties>

View file

@ -21,6 +21,8 @@
package net.kemitix.conditional; package net.kemitix.conditional;
import java.util.function.Supplier;
/** /**
* If-then-else in a functional-style. * If-then-else in a functional-style.
* *
@ -35,9 +37,11 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
@SuppressWarnings("avoidinlineconditionals")
static Condition where(final boolean clause) { static Condition where(final boolean clause) {
return clause ? TrueCondition.TRUE : FalseCondition.FALSE; if (clause) {
return TrueCondition.TRUE;
}
return FalseCondition.FALSE;
} }
/** /**
@ -58,7 +62,7 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
Condition and(boolean clause); Condition and(Supplier<Boolean> clause);
/** /**
* 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.
@ -67,8 +71,8 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
default Condition andNot(final boolean clause) { default Condition andNot(final Supplier<Boolean> clause) {
return and(!clause); return and(() -> !clause.get());
} }
/** /**
@ -79,7 +83,7 @@ public interface Condition {
* @return the Condition * @return the Condition
*/ */
@SuppressWarnings("PMD.ShortMethodName") @SuppressWarnings("PMD.ShortMethodName")
Condition or(boolean clause); Condition or(Supplier<Boolean> clause);
/** /**
* 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.
@ -88,8 +92,8 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
default Condition orNot(final boolean clause) { default Condition orNot(final Supplier<Boolean> clause) {
return or(!clause); return or(() -> !clause.get());
} }
/** /**
@ -115,8 +119,8 @@ public interface Condition {
* *
* @return the Condition * @return the Condition
*/ */
default Condition otherwise(final boolean clause) { default Condition otherwise(final Supplier<Boolean> clause) {
return where(clause); return where(clause.get());
} }
} }

View file

@ -21,6 +21,8 @@
package net.kemitix.conditional; package net.kemitix.conditional;
import java.util.function.Supplier;
/** /**
* A {@code Condition} that has evaluated to {@code false}. * A {@code Condition} that has evaluated to {@code false}.
* *
@ -31,14 +33,14 @@ final class FalseCondition implements Condition {
public static final Condition FALSE = new net.kemitix.conditional.FalseCondition(); public static final Condition FALSE = new net.kemitix.conditional.FalseCondition();
@Override @Override
public Condition and(final boolean clause) { public Condition and(final Supplier<Boolean> clause) {
return FALSE; return FALSE;
} }
@Override @Override
@SuppressWarnings("PMD.ShortMethodName") @SuppressWarnings("PMD.ShortMethodName")
public Condition or(final boolean secondClause) { public Condition or(final Supplier<Boolean> secondClause) {
return Condition.where(secondClause); return Condition.where(secondClause.get());
} }
@Override @Override

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;
/** /**
@ -32,22 +33,22 @@ import java.util.function.Supplier;
*/ */
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 @Override
public ValueSupplier<T> then(final Supplier<T> trueSupplier) { public ValueSupplier<T> then(final Supplier<T> trueSupplier) {
return new FalseValueSupplier(); return new FalseValueSupplier<>();
} }
@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") @SuppressWarnings("PMD.ShortMethodName")
public Value.ValueClause<T> or(final boolean clause) { public Value.ValueClause<T> or(final Supplier<Boolean> clause) {
return Value.where(clause); return Value.where(clause.get());
} }
/** /**
@ -62,6 +63,11 @@ class FalseValueClause<T> implements Value.ValueClause<T> {
return falseSupplier.get(); return falseSupplier.get();
} }
@Override
public Optional<T> optional() {
return Optional.empty();
}
} }
} }

View file

@ -21,6 +21,8 @@
package net.kemitix.conditional; package net.kemitix.conditional;
import java.util.function.Supplier;
/** /**
* A {@code Condition} that has evaluated to {@code true}. * A {@code Condition} that has evaluated to {@code true}.
* *
@ -31,13 +33,13 @@ final class TrueCondition implements Condition {
public static final Condition TRUE = new net.kemitix.conditional.TrueCondition(); public static final Condition TRUE = new net.kemitix.conditional.TrueCondition();
@Override @Override
public Condition and(final boolean clause) { public Condition and(final Supplier<Boolean> clause) {
return Condition.where(clause); return Condition.where(clause.get());
} }
@Override @Override
@SuppressWarnings("PMD.ShortMethodName") @SuppressWarnings("PMD.ShortMethodName")
public Condition or(final boolean secondClause) { public Condition or(final Supplier<Boolean> secondClause) {
return TRUE; return TRUE;
} }

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;
/** /**
@ -34,7 +35,7 @@ import java.util.function.Supplier;
*/ */
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 @Override
public ValueSupplier<T> then(final Supplier<T> trueSupplier) { public ValueSupplier<T> then(final Supplier<T> trueSupplier) {
@ -42,13 +43,13 @@ 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") @SuppressWarnings("PMD.ShortMethodName")
public Value.ValueClause<T> or(final boolean clause) { public Value.ValueClause<T> or(final Supplier<Boolean> clause) {
return this; return this;
} }
@ -68,6 +69,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

@ -43,9 +43,9 @@ public interface Value {
*/ */
@SuppressWarnings("PMD.LawOfDemeter") @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 +61,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,9 +75,12 @@ public interface Value {
* *
* @return a true or false value clause * @return a true or false value clause
*/ */
@SuppressWarnings({"unchecked", "avoidinlineconditionals"}) @SuppressWarnings("unchecked")
static <T> ValueClause<T> where(final boolean clause) { static <T> ValueClause<T> where(final boolean clause) {
return (ValueClause<T>) (clause ? TrueValueClause.TRUE : FalseValueClause.FALSE); if (clause) {
return (ValueClause<T>) TrueValueClause.TRUE;
}
return (ValueClause<T>) FalseValueClause.FALSE;
} }
/** /**
@ -88,7 +91,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 +118,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.
@ -125,7 +128,7 @@ public interface Value {
* @return a true or false value clause * @return a true or false value clause
*/ */
@SuppressWarnings("PMD.ShortMethodName") @SuppressWarnings("PMD.ShortMethodName")
ValueClause<T> or(boolean clause); ValueClause<T> or(Supplier<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 +137,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 +148,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 +168,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

@ -3,6 +3,8 @@ package net.kemitix.conditional;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -76,9 +78,9 @@ public class ConditionalTest {
public void whereTrueThenDoSomethingAndThenDoSomethingElse() { public void whereTrueThenDoSomethingAndThenDoSomethingElse() {
//when //when
Condition.where(true) Condition.where(true)
.then(thenResponse) .then(thenResponse)
.and(true) .and(() -> true)
.then(otherwiseResponse); .then(otherwiseResponse);
//then //then
assertThatBothResponsesRun(); assertThatBothResponsesRun();
} }
@ -119,7 +121,7 @@ public class ConditionalTest {
public void whereNotFalseThenRuns() { public void whereNotFalseThenRuns() {
//when //when
Condition.whereNot(false) Condition.whereNot(false)
.then(thenResponse); .then(thenResponse);
//then //then
assertThatTheThenResponseRuns(); assertThatTheThenResponseRuns();
} }
@ -128,8 +130,8 @@ public class ConditionalTest {
public void whereNotTrueThenOtherwiseRuns() { public void whereNotTrueThenOtherwiseRuns() {
//when //when
Condition.whereNot(true) Condition.whereNot(true)
.then(thenResponse) .then(thenResponse)
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@ -138,8 +140,8 @@ public class ConditionalTest {
public void whereTrueAndNotFalseThenRuns() { public void whereTrueAndNotFalseThenRuns() {
//when //when
Condition.where(true) Condition.where(true)
.andNot(false) .andNot(() -> false)
.then(thenResponse); .then(thenResponse);
//then //then
assertThatTheThenResponseRuns(); assertThatTheThenResponseRuns();
} }
@ -148,9 +150,9 @@ public class ConditionalTest {
public void whereTrueAndNotTrueThenOtherwiseRuns() { public void whereTrueAndNotTrueThenOtherwiseRuns() {
//when //when
Condition.where(true) Condition.where(true)
.andNot(true) .andNot(() -> true)
.then(thenResponse) .then(thenResponse)
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@ -159,8 +161,8 @@ public class ConditionalTest {
public void whereFalseOrNotFalseThenRuns() { public void whereFalseOrNotFalseThenRuns() {
//when //when
Condition.where(false) Condition.where(false)
.orNot(false) .orNot(() -> false)
.then(thenResponse); .then(thenResponse);
//then //then
assertThatTheThenResponseRuns(); assertThatTheThenResponseRuns();
} }
@ -169,9 +171,9 @@ public class ConditionalTest {
public void whereFalseOrNotTrueThenOtherwiseRuns() { public void whereFalseOrNotTrueThenOtherwiseRuns() {
//when //when
Condition.where(false) Condition.where(false)
.orNot(true) .orNot(() -> true)
.then(thenResponse) .then(thenResponse)
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@ -180,9 +182,9 @@ public class ConditionalTest {
public void whereFalseElseTrueThenOtherwiseRuns() { public void whereFalseElseTrueThenOtherwiseRuns() {
//when //when
Condition.where(false) Condition.where(false)
.then(thenResponse) .then(thenResponse)
.otherwise(true) .otherwise(() -> true)
.then(otherwiseResponse); .then(otherwiseResponse);
//then //then
assertThatTheOtherwiseResponseRuns(); assertThatTheOtherwiseResponseRuns();
} }
@ -191,9 +193,9 @@ public class ConditionalTest {
public void whereFalseElseFalseThenNothingRuns() { public void whereFalseElseFalseThenNothingRuns() {
//when //when
Condition.where(false) Condition.where(false)
.then(thenResponse) .then(thenResponse)
.otherwise(false) .otherwise(() -> false)
.then(otherwiseResponse); .then(otherwiseResponse);
//then //then
assertThatNoResponseRuns(); assertThatNoResponseRuns();
} }
@ -202,8 +204,8 @@ public class ConditionalTest {
public void whereTrueChainedThensBothRuns() { public void whereTrueChainedThensBothRuns() {
//when //when
Condition.where(true) Condition.where(true)
.then(thenResponse) .then(thenResponse)
.then(otherwiseResponse); .then(otherwiseResponse);
//then //then
assertThatBothResponsesRun(); assertThatBothResponsesRun();
} }
@ -212,8 +214,8 @@ public class ConditionalTest {
public void whereFalseChainedThensNothingRuns() { public void whereFalseChainedThensNothingRuns() {
//when //when
Condition.where(false) Condition.where(false)
.then(thenResponse) .then(thenResponse)
.then(otherwiseResponse); .then(otherwiseResponse);
//then //then
assertThatNoResponseRuns(); assertThatNoResponseRuns();
} }
@ -235,22 +237,22 @@ public class ConditionalTest {
private void assertThatTheOtherwiseResponseRan() { private void assertThatTheOtherwiseResponseRan() {
assertThat(otherwiseFlag).as("otherwise response runs") assertThat(otherwiseFlag).as("otherwise response runs")
.isTrue(); .isTrue();
} }
private void assertThatTheThenResponseRan() { private void assertThatTheThenResponseRan() {
assertThat(thenFlag).as("then response runs") assertThat(thenFlag).as("then response runs")
.isTrue(); .isTrue();
} }
private void assertThatTheOtherwiseResponseDidNotRun() { private void assertThatTheOtherwiseResponseDidNotRun() {
assertThat(otherwiseFlag).as("otherwise response does not run") assertThat(otherwiseFlag).as("otherwise response does not run")
.isFalse(); .isFalse();
} }
private void assertThatTheThenResponseDidNotRun() { private void assertThatTheThenResponseDidNotRun() {
assertThat(thenFlag).as("then response does not run") assertThat(thenFlag).as("then response does not run")
.isFalse(); .isFalse();
} }
private void assertThatNoResponseRuns() { private void assertThatNoResponseRuns() {
@ -260,27 +262,53 @@ public class ConditionalTest {
private void when(final boolean clause) { private void when(final boolean clause) {
Condition.where(clause) Condition.where(clause)
.then(thenResponse) .then(thenResponse)
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
} }
private void when( private void when(
final boolean firstClause, final boolean firstClause,
final boolean secondClause final boolean secondClause
) { ) {
Condition.where(firstClause) Condition.where(firstClause)
.and(secondClause) .and(() -> secondClause)
.then(thenResponse) .then(thenResponse)
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
} }
private void whenOr( private void whenOr(
final boolean firstClause, final boolean firstClause,
final boolean secondClause final boolean secondClause
) { ) {
Condition.where(firstClause) Condition.where(firstClause)
.or(secondClause) .or(() -> secondClause)
.then(thenResponse) .then(thenResponse)
.otherwise(otherwiseResponse); .otherwise(otherwiseResponse);
}
@Test
public void shortCurcuitOr() {
//given
final AtomicInteger atomicInteger = new AtomicInteger();
//when
Condition.where(true)
.or(() -> atomicInteger.compareAndSet(0, 2))
.then(thenResponse);
//then
assertThatTheThenResponseRan();
assertThat(atomicInteger).hasValue(0);
}
@Test
public void shortCurcuitAnd() {
//given
final AtomicInteger atomicInteger = new AtomicInteger();
//when
Condition.where(false)
.and(() -> atomicInteger.compareAndSet(0, 2))
.then(thenResponse);
//then
assertThatTheThenResponseDidNotRun();
assertThat(atomicInteger).hasValue(0);
} }
} }

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;
@ -52,7 +53,7 @@ public class ValueTest {
public void valueWhereClauseIsTrue() { 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);
//then //then
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -61,7 +62,7 @@ public class ValueTest {
public void valueWhereClauseIsFalse() { 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);
//then //then
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -69,9 +70,9 @@ 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
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -79,9 +80,9 @@ 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
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -89,9 +90,9 @@ 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
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -99,9 +100,9 @@ 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
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -109,9 +110,9 @@ 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
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -119,9 +120,9 @@ 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
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -129,9 +130,9 @@ 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
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -139,9 +140,9 @@ 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
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -150,7 +151,7 @@ public class ValueTest {
public void valueWhereNotTrueIsFalse() { public void valueWhereNotTrueIsFalse() {
//when //when
val result = Value.<String>whereNot(true).then(() -> TRUE) val result = Value.<String>whereNot(true).then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -159,7 +160,7 @@ public class ValueTest {
public void valueWhereNotFalseIsTrue() { public void valueWhereNotFalseIsTrue() {
//when //when
val result = Value.<String>whereNot(false).then(() -> TRUE) val result = Value.<String>whereNot(false).then(() -> TRUE)
.otherwise(() -> FALSE); .otherwise(() -> FALSE);
//then //then
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -167,9 +168,9 @@ 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
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -177,9 +178,9 @@ 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
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -187,9 +188,9 @@ 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
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -197,9 +198,9 @@ 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
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -207,9 +208,9 @@ 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
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -217,9 +218,9 @@ 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
assertThat(result).isEqualTo(TRUE); assertThat(result).isEqualTo(TRUE);
} }
@ -227,9 +228,9 @@ 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
assertThat(result).isEqualTo(FALSE); assertThat(result).isEqualTo(FALSE);
} }
@ -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);
}
} }