From 779a64427eb10798eca7312d89a7a51c8c2ac4f1 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 12 Mar 2018 20:08:04 +0000 Subject: [PATCH 01/10] Add missing generic types --- .../java/net/kemitix/conditional/TrueValueClause.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/kemitix/conditional/TrueValueClause.java b/src/main/java/net/kemitix/conditional/TrueValueClause.java index 9d3fbcc..c8e7143 100644 --- a/src/main/java/net/kemitix/conditional/TrueValueClause.java +++ b/src/main/java/net/kemitix/conditional/TrueValueClause.java @@ -34,11 +34,11 @@ import java.util.function.Supplier; */ class TrueValueClause implements Value.ValueClause { - protected static final Value.ValueClause TRUE = new TrueValueClause(); + protected static final Value.ValueClause TRUE = new TrueValueClause<>(); @Override public ValueSupplier then(final Supplier trueSupplier) { - return new TrueValueSupplier(trueSupplier); + return new TrueValueSupplier<>(trueSupplier); } @Override @@ -54,11 +54,13 @@ class TrueValueClause implements Value.ValueClause { /** * An intermediate result of the {@link Value} where the clause has evaluated to true. + * + * @param the type of the value */ @RequiredArgsConstructor - private class TrueValueSupplier implements ValueSupplier { + private static final class TrueValueSupplier implements ValueSupplier { - private final transient Supplier valueSupplier; + private final Supplier valueSupplier; @Override public T otherwise(final Supplier falseSupplier) { From ab47d4cad789e5984064fdc9c5d98e308779c324 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 12 Mar 2018 20:08:25 +0000 Subject: [PATCH 02/10] Suppress false-positive PMD error --- src/main/java/net/kemitix/conditional/TrueValueClause.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/kemitix/conditional/TrueValueClause.java b/src/main/java/net/kemitix/conditional/TrueValueClause.java index c8e7143..8179e27 100644 --- a/src/main/java/net/kemitix/conditional/TrueValueClause.java +++ b/src/main/java/net/kemitix/conditional/TrueValueClause.java @@ -60,6 +60,7 @@ class TrueValueClause implements Value.ValueClause { @RequiredArgsConstructor private static final class TrueValueSupplier implements ValueSupplier { + @SuppressWarnings("PMD.BeanMembersShouldSerialize") private final Supplier valueSupplier; @Override From d1ca0b039f3fd0d85a64ef17096ce1f40abe95fb Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 12 Mar 2018 20:09:34 +0000 Subject: [PATCH 03/10] Add missing generic types --- src/main/java/net/kemitix/conditional/FalseValueClause.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/kemitix/conditional/FalseValueClause.java b/src/main/java/net/kemitix/conditional/FalseValueClause.java index 672d285..3f6be53 100644 --- a/src/main/java/net/kemitix/conditional/FalseValueClause.java +++ b/src/main/java/net/kemitix/conditional/FalseValueClause.java @@ -52,8 +52,10 @@ class FalseValueClause implements Value.ValueClause { /** * An intermediate result of the {@link Value} where the clause has evaluated to false. + * + * @param the type of the value */ - private class FalseValueSupplier implements ValueSupplier { + private static final class FalseValueSupplier implements ValueSupplier { @Override public T otherwise(final Supplier falseSupplier) { From efc1705a6c1cf40825d3a255cbe02ac5bc49054a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Mar 2018 17:08:06 +0000 Subject: [PATCH 04/10] Condition: secondary clauses now use Supplier's to enable short circuiting --- .../net/kemitix/conditional/Condition.java | 20 ++-- .../kemitix/conditional/FalseCondition.java | 9 +- .../kemitix/conditional/TrueCondition.java | 9 +- .../kemitix/conditional/ConditionalTest.java | 108 +++++++++++------- 4 files changed, 88 insertions(+), 58 deletions(-) diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index 000268c..3f385eb 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -21,6 +21,8 @@ package net.kemitix.conditional; +import java.util.function.Supplier; + /** * If-then-else in a functional-style. * @@ -35,7 +37,6 @@ public interface Condition { * * @return the Condition */ - @SuppressWarnings("avoidinlineconditionals") static Condition where(final boolean clause) { return clause ? TrueCondition.TRUE : FalseCondition.FALSE; } @@ -58,7 +59,7 @@ public interface Condition { * * @return the Condition */ - Condition and(boolean clause); + Condition and(Supplier clause); /** * Logically AND combine the current {@code Condition} with boolean opposite of the clause. @@ -67,8 +68,8 @@ public interface Condition { * * @return the Condition */ - default Condition andNot(final boolean clause) { - return and(!clause); + default Condition andNot(final Supplier clause) { + return and(() -> !clause.get()); } /** @@ -78,8 +79,7 @@ public interface Condition { * * @return the Condition */ - @SuppressWarnings("PMD.ShortMethodName") - Condition or(boolean clause); + Condition or(Supplier clause); /** * Logically OR combine the current {@code Condition} with the boolean opposite of the clause. @@ -88,8 +88,8 @@ public interface Condition { * * @return the Condition */ - default Condition orNot(final boolean clause) { - return or(!clause); + default Condition orNot(final Supplier clause) { + return or(() -> !clause.get()); } /** @@ -115,8 +115,8 @@ public interface Condition { * * @return the Condition */ - default Condition otherwise(final boolean clause) { - return where(clause); + default Condition otherwise(final Supplier clause) { + return where(clause.get()); } } diff --git a/src/main/java/net/kemitix/conditional/FalseCondition.java b/src/main/java/net/kemitix/conditional/FalseCondition.java index 07881c7..cd382ca 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -21,6 +21,8 @@ package net.kemitix.conditional; +import java.util.function.Supplier; + /** * A {@code Condition} that has evaluated to {@code false}. * @@ -31,14 +33,13 @@ final class FalseCondition implements Condition { public static final Condition FALSE = new net.kemitix.conditional.FalseCondition(); @Override - public Condition and(final boolean clause) { + public Condition and(final Supplier clause) { return FALSE; } @Override - @SuppressWarnings("PMD.ShortMethodName") - public Condition or(final boolean secondClause) { - return Condition.where(secondClause); + public Condition or(final Supplier secondClause) { + return Condition.where(secondClause.get()); } @Override diff --git a/src/main/java/net/kemitix/conditional/TrueCondition.java b/src/main/java/net/kemitix/conditional/TrueCondition.java index 06f041b..987496c 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -21,6 +21,8 @@ package net.kemitix.conditional; +import java.util.function.Supplier; + /** * A {@code Condition} that has evaluated to {@code true}. * @@ -31,13 +33,12 @@ final class TrueCondition implements Condition { public static final Condition TRUE = new net.kemitix.conditional.TrueCondition(); @Override - public Condition and(final boolean clause) { - return Condition.where(clause); + public Condition and(final Supplier clause) { + return Condition.where(clause.get()); } @Override - @SuppressWarnings("PMD.ShortMethodName") - public Condition or(final boolean secondClause) { + public Condition or(final Supplier secondClause) { return TRUE; } diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 525c534..5124b7f 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -3,6 +3,8 @@ package net.kemitix.conditional; import org.junit.Before; import org.junit.Test; +import java.util.concurrent.atomic.AtomicInteger; + import static org.assertj.core.api.Assertions.assertThat; /** @@ -76,9 +78,9 @@ public class ConditionalTest { public void whereTrueThenDoSomethingAndThenDoSomethingElse() { //when Condition.where(true) - .then(thenResponse) - .and(true) - .then(otherwiseResponse); + .then(thenResponse) + .and(() -> true) + .then(otherwiseResponse); //then assertThatBothResponsesRun(); } @@ -119,7 +121,7 @@ public class ConditionalTest { public void whereNotFalseThenRuns() { //when Condition.whereNot(false) - .then(thenResponse); + .then(thenResponse); //then assertThatTheThenResponseRuns(); } @@ -128,8 +130,8 @@ public class ConditionalTest { public void whereNotTrueThenOtherwiseRuns() { //when Condition.whereNot(true) - .then(thenResponse) - .otherwise(otherwiseResponse); + .then(thenResponse) + .otherwise(otherwiseResponse); //then assertThatTheOtherwiseResponseRuns(); } @@ -138,8 +140,8 @@ public class ConditionalTest { public void whereTrueAndNotFalseThenRuns() { //when Condition.where(true) - .andNot(false) - .then(thenResponse); + .andNot(() -> false) + .then(thenResponse); //then assertThatTheThenResponseRuns(); } @@ -148,9 +150,9 @@ public class ConditionalTest { public void whereTrueAndNotTrueThenOtherwiseRuns() { //when Condition.where(true) - .andNot(true) - .then(thenResponse) - .otherwise(otherwiseResponse); + .andNot(() -> true) + .then(thenResponse) + .otherwise(otherwiseResponse); //then assertThatTheOtherwiseResponseRuns(); } @@ -159,8 +161,8 @@ public class ConditionalTest { public void whereFalseOrNotFalseThenRuns() { //when Condition.where(false) - .orNot(false) - .then(thenResponse); + .orNot(() -> false) + .then(thenResponse); //then assertThatTheThenResponseRuns(); } @@ -169,9 +171,9 @@ public class ConditionalTest { public void whereFalseOrNotTrueThenOtherwiseRuns() { //when Condition.where(false) - .orNot(true) - .then(thenResponse) - .otherwise(otherwiseResponse); + .orNot(() -> true) + .then(thenResponse) + .otherwise(otherwiseResponse); //then assertThatTheOtherwiseResponseRuns(); } @@ -180,9 +182,9 @@ public class ConditionalTest { public void whereFalseElseTrueThenOtherwiseRuns() { //when Condition.where(false) - .then(thenResponse) - .otherwise(true) - .then(otherwiseResponse); + .then(thenResponse) + .otherwise(() -> true) + .then(otherwiseResponse); //then assertThatTheOtherwiseResponseRuns(); } @@ -191,9 +193,9 @@ public class ConditionalTest { public void whereFalseElseFalseThenNothingRuns() { //when Condition.where(false) - .then(thenResponse) - .otherwise(false) - .then(otherwiseResponse); + .then(thenResponse) + .otherwise(() -> false) + .then(otherwiseResponse); //then assertThatNoResponseRuns(); } @@ -202,8 +204,8 @@ public class ConditionalTest { public void whereTrueChainedThensBothRuns() { //when Condition.where(true) - .then(thenResponse) - .then(otherwiseResponse); + .then(thenResponse) + .then(otherwiseResponse); //then assertThatBothResponsesRun(); } @@ -212,8 +214,8 @@ public class ConditionalTest { public void whereFalseChainedThensNothingRuns() { //when Condition.where(false) - .then(thenResponse) - .then(otherwiseResponse); + .then(thenResponse) + .then(otherwiseResponse); //then assertThatNoResponseRuns(); } @@ -235,22 +237,22 @@ public class ConditionalTest { private void assertThatTheOtherwiseResponseRan() { assertThat(otherwiseFlag).as("otherwise response runs") - .isTrue(); + .isTrue(); } private void assertThatTheThenResponseRan() { assertThat(thenFlag).as("then response runs") - .isTrue(); + .isTrue(); } private void assertThatTheOtherwiseResponseDidNotRun() { assertThat(otherwiseFlag).as("otherwise response does not run") - .isFalse(); + .isFalse(); } private void assertThatTheThenResponseDidNotRun() { assertThat(thenFlag).as("then response does not run") - .isFalse(); + .isFalse(); } private void assertThatNoResponseRuns() { @@ -260,27 +262,53 @@ public class ConditionalTest { private void when(final boolean clause) { Condition.where(clause) - .then(thenResponse) - .otherwise(otherwiseResponse); + .then(thenResponse) + .otherwise(otherwiseResponse); } private void when( final boolean firstClause, final boolean secondClause - ) { + ) { Condition.where(firstClause) - .and(secondClause) - .then(thenResponse) - .otherwise(otherwiseResponse); + .and(() -> secondClause) + .then(thenResponse) + .otherwise(otherwiseResponse); } private void whenOr( final boolean firstClause, final boolean secondClause - ) { + ) { Condition.where(firstClause) - .or(secondClause) - .then(thenResponse) - .otherwise(otherwiseResponse); + .or(() -> secondClause) + .then(thenResponse) + .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); } } From ac3e253bf2a0fe5fc878ff4abc79e3140e805d8e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Mar 2018 17:53:16 +0000 Subject: [PATCH 05/10] Value: secondary clauses now use Supplier's to enable short circuiting --- README.adoc | 69 +++++--- .../kemitix/conditional/FalseValueClause.java | 13 +- .../kemitix/conditional/TrueValueClause.java | 14 +- .../java/net/kemitix/conditional/Value.java | 33 ++-- .../net/kemitix/conditional/ValueTest.java | 149 ++++++++++++------ 5 files changed, 180 insertions(+), 98 deletions(-) diff --git a/README.adoc b/README.adoc index d4bebd6..5eadee4 100644 --- a/README.adoc +++ b/README.adoc @@ -59,7 +59,7 @@ if (isTrue() && isAlsoTrue()) { [[source,java]] ---- Condition.where(isTrue()) - .and(isAlsoTrue()) + .and(() -> isAlsoTrue()) .then(() -> doSomething()) .otherwise(() -> doSomethingElse()); ---- @@ -78,7 +78,7 @@ if (isTrue() || alternativeIsTrue()) { [[source,java]] ---- Condition.where(isTrue()) - .or(alternativeIsTrue()) + .or(() -> alternativeIsTrue()) .then(() -> doSomething()) .otherwise(() -> doSomethingElse()); ---- @@ -115,7 +115,7 @@ if (isTrue() || !isFalse()) { [[source,java]] ---- Condition.where(isTrue()) - .andNot(isFalse()) + .andNot(() -> isFalse()) .then(() -> doSomething()) .otherwise(() -> doSomethingElse()); ---- @@ -134,7 +134,7 @@ if (isFalse() || !isAlsoFalse()) { [[source,java]] ---- Condition.where(isFalse()) - .orNot(isAlsoFalse()) + .orNot(() -> isAlsoFalse()) .then(() -> doSomething()) .otherwise(() -> doSomethingElse()); ---- @@ -154,7 +154,7 @@ if (isFalse()) { ---- Condition.where(isFalse()) .then(() -> doSomething()) - .otherwise(isTrue()) + .otherwise(() -> isTrue()) .then(() -> doSomethingElse()); ---- @@ -174,7 +174,7 @@ if (isTrue()) { ---- Condition.where(isTrue()) .then(() -> doSomething()) - .and(isAlsoTrue()) + .and(() -> isAlsoTrue()) .then(() -> doSomethingElse()); ---- @@ -213,8 +213,9 @@ final Optional result = Value.where(isTrue(), () -> TRUE); [[source,java]] ---- -final String result = Value.where(isTrue()).then(() -> TRUE) - .otherwise(() -> FALSE); +final String result = Value.where(isTrue()) + .then(() -> TRUE) + .otherwise(() -> FALSE); ---- ### if-not-then-else @@ -231,8 +232,9 @@ if (!isTrue()) { [[source,java]] ---- -final String result = Value.whereNot(isTrue()).then(() -> TRUE) - .otherwise(() -> FALSE); +final String result = Value.whereNot(isTrue()) + .then(() -> TRUE) + .otherwise(() -> FALSE); ---- ### if-and-then-else @@ -249,9 +251,10 @@ if (isTrue() && alternativeIsTrue()) { [[source,java]] ---- -final String result = Value.where(isTrue()).and(alternativeIsTrue()) - .then(() -> TRUE) - .otherwise(() -> FALSE); +final String result = Value.where(isTrue()) + .and(() -> alternativeIsTrue()) + .then(() -> TRUE) + .otherwise(() -> FALSE); ---- ### if-and-not-then-else @@ -268,9 +271,10 @@ if (isTrue() && !alternativeIsFalse()) { [[source,java]] ---- -final String result = Value.where(isTrue()).andNot(alternativeIsFalse()) - .then(() -> TRUE) - .otherwise(() -> FALSE); +final String result = Value.where(isTrue()) + .andNot(() -> alternativeIsFalse()) + .then(() -> TRUE) + .otherwise(() -> FALSE); ---- ### if-or-then-else @@ -287,9 +291,10 @@ if (isTrue() || alternativeIsTrue()) { [[source,java]] ---- -final String result = Value.where(isTrue()).or(alternativeIsTrue()) - .then(() -> TRUE) - .otherwise(() -> FALSE); +final String result = Value.where(isTrue()) + .or(() -> alternativeIsTrue()) + .then(() -> TRUE) + .otherwise(() -> FALSE); ---- ### if-or-not-then-else @@ -306,7 +311,27 @@ if (isTrue() || !isFalse()) { [[source,java]] ---- -final String result = Value.where(isTrue()).orNot(isFalse()) - .then(() -> TRUE) - .otherwise(() -> FALSE); +final String result = Value.where(isTrue()) + .orNot(() -> isFalse()) + .then(() -> TRUE) + .otherwise(() -> FALSE); ---- + +### if-then + +[[source,java]] +----- +Optional result; +if (isTrue()) { + result = Optional.of(TRUE); +} else { + result = Optional.empty(); +} +----- + +[[source,java]] +----- +final Optional result = Value.where(isTrue()) + .then(() -> TRUE) + .optional(); +----- diff --git a/src/main/java/net/kemitix/conditional/FalseValueClause.java b/src/main/java/net/kemitix/conditional/FalseValueClause.java index 3f6be53..94775f2 100644 --- a/src/main/java/net/kemitix/conditional/FalseValueClause.java +++ b/src/main/java/net/kemitix/conditional/FalseValueClause.java @@ -21,6 +21,7 @@ package net.kemitix.conditional; +import java.util.Optional; import java.util.function.Supplier; /** @@ -40,14 +41,13 @@ class FalseValueClause implements Value.ValueClause { } @Override - public Value.ValueClause and(final boolean clause) { + public Value.ValueClause and(final Supplier clause) { return this; } @Override - @SuppressWarnings("PMD.ShortMethodName") - public Value.ValueClause or(final boolean clause) { - return Value.where(clause); + public Value.ValueClause or(final Supplier clause) { + return Value.where(clause.get()); } /** @@ -62,6 +62,11 @@ class FalseValueClause implements Value.ValueClause { return falseSupplier.get(); } + @Override + public Optional optional() { + return Optional.empty(); + } + } } diff --git a/src/main/java/net/kemitix/conditional/TrueValueClause.java b/src/main/java/net/kemitix/conditional/TrueValueClause.java index 8179e27..c5c04ad 100644 --- a/src/main/java/net/kemitix/conditional/TrueValueClause.java +++ b/src/main/java/net/kemitix/conditional/TrueValueClause.java @@ -23,6 +23,7 @@ package net.kemitix.conditional; import lombok.RequiredArgsConstructor; +import java.util.Optional; import java.util.function.Supplier; /** @@ -42,13 +43,12 @@ class TrueValueClause implements Value.ValueClause { } @Override - public Value.ValueClause and(final boolean clause) { - return Value.where(clause); + public Value.ValueClause and(final Supplier clause) { + return Value.where(clause.get()); } @Override - @SuppressWarnings("PMD.ShortMethodName") - public Value.ValueClause or(final boolean clause) { + public Value.ValueClause or(final Supplier clause) { return this; } @@ -60,7 +60,6 @@ class TrueValueClause implements Value.ValueClause { @RequiredArgsConstructor private static final class TrueValueSupplier implements ValueSupplier { - @SuppressWarnings("PMD.BeanMembersShouldSerialize") private final Supplier valueSupplier; @Override @@ -68,6 +67,11 @@ class TrueValueClause implements Value.ValueClause { return valueSupplier.get(); } + @Override + public Optional optional() { + return Optional.ofNullable(valueSupplier.get()); + } + } } diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index 9b1c3b6..55a0e68 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -41,11 +41,10 @@ public interface Value { * * @return the value from either the trueSupplier or the falseSupplier */ - @SuppressWarnings("PMD.LawOfDemeter") static T where( - boolean clause, - Supplier trueSupplier, - Supplier falseSupplier + final boolean clause, + final Supplier trueSupplier, + final Supplier falseSupplier ) { return Value.where(clause).then(trueSupplier) .otherwise(falseSupplier); @@ -61,8 +60,8 @@ public interface Value { * @return an Optional either containing the value from the trueSupplier or empty */ static Optional where( - boolean clause, - Supplier trueSupplier + final boolean clause, + final Supplier trueSupplier ) { return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null)); } @@ -75,7 +74,6 @@ public interface Value { * * @return a true or false value clause */ - @SuppressWarnings({"unchecked", "avoidinlineconditionals"}) static ValueClause where(final boolean clause) { return (ValueClause) (clause ? TrueValueClause.TRUE : FalseValueClause.FALSE); } @@ -88,7 +86,7 @@ public interface Value { * * @return a true or false value clause */ - static ValueClause whereNot(boolean clause) { + static ValueClause whereNot(final boolean clause) { return where(!clause); } @@ -115,7 +113,7 @@ public interface Value { * * @return a true or false value clause */ - ValueClause and(boolean clause); + ValueClause and(Supplier clause); /** * Logically OR combine the current {@link ValueClause} with clause. @@ -124,8 +122,7 @@ public interface Value { * * @return a true or false value clause */ - @SuppressWarnings("PMD.ShortMethodName") - ValueClause or(boolean clause); + ValueClause or(Supplier 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 */ - default ValueClause andNot(final boolean clause) { - return and(!clause); + default ValueClause andNot(final Supplier clause) { + return and(() -> !clause.get()); } /** @@ -145,8 +142,8 @@ public interface Value { * * @return a true or false value clause */ - default ValueClause orNot(boolean clause) { - return or(!clause); + default ValueClause orNot(final Supplier clause) { + return or(() -> !clause.get()); } /** @@ -165,6 +162,12 @@ public interface Value { */ T otherwise(Supplier 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 optional(); } } diff --git a/src/test/java/net/kemitix/conditional/ValueTest.java b/src/test/java/net/kemitix/conditional/ValueTest.java index d76011b..3c72c6e 100644 --- a/src/test/java/net/kemitix/conditional/ValueTest.java +++ b/src/test/java/net/kemitix/conditional/ValueTest.java @@ -4,6 +4,7 @@ import lombok.val; import org.junit.Test; import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; import static org.assertj.core.api.Assertions.assertThat; @@ -52,7 +53,7 @@ public class ValueTest { public void valueWhereClauseIsTrue() { //when val result = Value.where(true).then(() -> TRUE) - .otherwise(() -> FALSE); + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -61,7 +62,7 @@ public class ValueTest { public void valueWhereClauseIsFalse() { //when val result = Value.where(false).then(() -> TRUE) - .otherwise(() -> FALSE); + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -69,9 +70,9 @@ public class ValueTest { @Test public void valueWhereTrueAndTrueIsTrue() { //when - val result = Value.where(true).and(true) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(true).and(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -79,9 +80,9 @@ public class ValueTest { @Test public void valueWhereTrueAndFalseIsFalse() { //when - val result = Value.where(true).and(false) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(true).and(() -> false) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -89,9 +90,9 @@ public class ValueTest { @Test public void valueWhereFalseAndTrueIsFalse() { //when - val result = Value.where(false).and(true) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(false).and(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -99,9 +100,9 @@ public class ValueTest { @Test public void valueWhereFalseAndFalseIsFalse() { //when - val result = Value.where(false).and(false) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(false).and(() -> false) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -109,9 +110,9 @@ public class ValueTest { @Test public void valueWhereTrueOrTrueIsTrue() { //when - val result = Value.where(true).or(true) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(true).or(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -119,9 +120,9 @@ public class ValueTest { @Test public void valueWhereTrueOrFalseIsTrue() { //when - val result = Value.where(true).or(false) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(true).or(() -> false) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -129,9 +130,9 @@ public class ValueTest { @Test public void valueWhereFalseOrTrueIsTrue() { //when - val result = Value.where(false).or(true) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(false).or(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -139,9 +140,9 @@ public class ValueTest { @Test public void valueWhereFalseOrFalseIsFalse() { //when - val result = Value.where(false).or(false) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(false).or(() -> false) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -150,7 +151,7 @@ public class ValueTest { public void valueWhereNotTrueIsFalse() { //when val result = Value.whereNot(true).then(() -> TRUE) - .otherwise(() -> FALSE); + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -159,7 +160,7 @@ public class ValueTest { public void valueWhereNotFalseIsTrue() { //when val result = Value.whereNot(false).then(() -> TRUE) - .otherwise(() -> FALSE); + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -167,9 +168,9 @@ public class ValueTest { @Test public void valueWhereTrueAndNotTrueIsFalse() { //when - val result = Value.where(true).andNot(true) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(true).andNot(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -177,9 +178,9 @@ public class ValueTest { @Test public void valueWhereTrueAndNotFalseIsTrue() { //when - val result = Value.where(true).andNot(false) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(true).andNot(() -> false) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -187,9 +188,9 @@ public class ValueTest { @Test public void valueWhereFalseAndNotTrueIsFalse() { //when - val result = Value.where(false).andNot(true) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(false).andNot(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -197,9 +198,9 @@ public class ValueTest { @Test public void valueWhereFalseAndNotFalseIsFalse() { //when - val result = Value.where(false).andNot(false) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(false).andNot(() -> false) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -207,9 +208,9 @@ public class ValueTest { @Test public void valueWhereTrueOrNotTrueIsTrue() { //when - val result = Value.where(true).orNot(true) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(true).orNot(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -217,9 +218,9 @@ public class ValueTest { @Test public void valueWhereTrueOrNotFalseIsTrue() { //when - val result = Value.where(true).orNot(false) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(true).orNot(() -> false) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } @@ -227,9 +228,9 @@ public class ValueTest { @Test public void valueWhereFalseOrNotTrueIsFalse() { //when - val result = Value.where(false).orNot(true) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(false).orNot(() -> true) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(FALSE); } @@ -237,10 +238,54 @@ public class ValueTest { @Test public void valueWhereFalseOrNotFalseIsTrue() { //when - val result = Value.where(false).orNot(false) - .then(() -> TRUE) - .otherwise(() -> FALSE); + val result = Value.where(false).orNot(() -> false) + .then(() -> TRUE) + .otherwise(() -> FALSE); //then assertThat(result).isEqualTo(TRUE); } + + @Test + public void valueWhereTrueThenIsNotEmpty() { + //given + final Optional result = Value.where(true).then(() -> "value").optional(); + //then + assertThat(result).contains("value"); + } + + @Test + public void valueWhereFalseThenIsEmpty() { + //given + final Optional result = Value.where(false).then(() -> "value").optional(); + //when + assertThat(result).isEmpty(); + } + + @Test + public void shortCurcuitOr() { + //given + final AtomicInteger atomicInteger = new AtomicInteger(); + //when + final Optional result = Value.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 result = Value.where(false) + .and(() -> atomicInteger.compareAndSet(0, 2)) + .then(() -> "Pass") + .optional(); + //then + assertThat(result).isEmpty(); + assertThat(atomicInteger).hasValue(0); + } } From 66a0222253c94b8addf319a467218d28cb9fb476 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Mar 2018 18:00:59 +0000 Subject: [PATCH 06/10] Suppress PMD.ShortMethodName for or() methods --- src/main/java/net/kemitix/conditional/Condition.java | 1 + src/main/java/net/kemitix/conditional/Value.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index 3f385eb..33c0a31 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -79,6 +79,7 @@ public interface Condition { * * @return the Condition */ + @SuppressWarnings("PMD.ShortMethodName") Condition or(Supplier clause); /** diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index 55a0e68..fe2b182 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -122,6 +122,7 @@ public interface Value { * * @return a true or false value clause */ + @SuppressWarnings("PMD.ShortMethodName") ValueClause or(Supplier clause); /** From e427f14b70ef9952974275571adbca41f4beb410 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Mar 2018 18:18:38 +0000 Subject: [PATCH 07/10] Upgrade kemitix-maven-tiles to 0.8.1 --- pom.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 18f42b7..38800de 100644 --- a/pom.xml +++ b/pom.xml @@ -16,9 +16,7 @@ 3.9.1 4.3.0 2.10 - 0.7.1 - 2.20.1 - 2.20.1 + 0.8.1 1.16.20 From 3a1e51aa9f811359445b8a6e4ff021941a64289b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Mar 2018 18:29:41 +0000 Subject: [PATCH 08/10] Apply PMD and Checkstyle updates --- src/main/java/net/kemitix/conditional/Condition.java | 5 ++++- src/main/java/net/kemitix/conditional/FalseCondition.java | 1 + src/main/java/net/kemitix/conditional/FalseValueClause.java | 1 + src/main/java/net/kemitix/conditional/TrueCondition.java | 1 + src/main/java/net/kemitix/conditional/TrueValueClause.java | 2 ++ src/main/java/net/kemitix/conditional/Value.java | 6 +++++- 6 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index 33c0a31..c87a74f 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -38,7 +38,10 @@ public interface Condition { * @return the Condition */ static Condition where(final boolean clause) { - return clause ? TrueCondition.TRUE : FalseCondition.FALSE; + if (clause) { + return TrueCondition.TRUE; + } + return FalseCondition.FALSE; } /** diff --git a/src/main/java/net/kemitix/conditional/FalseCondition.java b/src/main/java/net/kemitix/conditional/FalseCondition.java index cd382ca..3ffdbd2 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -38,6 +38,7 @@ final class FalseCondition implements Condition { } @Override + @SuppressWarnings("PMD.ShortMethodName") public Condition or(final Supplier secondClause) { return Condition.where(secondClause.get()); } diff --git a/src/main/java/net/kemitix/conditional/FalseValueClause.java b/src/main/java/net/kemitix/conditional/FalseValueClause.java index 94775f2..b609428 100644 --- a/src/main/java/net/kemitix/conditional/FalseValueClause.java +++ b/src/main/java/net/kemitix/conditional/FalseValueClause.java @@ -46,6 +46,7 @@ class FalseValueClause implements Value.ValueClause { } @Override + @SuppressWarnings("PMD.ShortMethodName") public Value.ValueClause or(final Supplier clause) { return Value.where(clause.get()); } diff --git a/src/main/java/net/kemitix/conditional/TrueCondition.java b/src/main/java/net/kemitix/conditional/TrueCondition.java index 987496c..0691433 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -38,6 +38,7 @@ final class TrueCondition implements Condition { } @Override + @SuppressWarnings("PMD.ShortMethodName") public Condition or(final Supplier secondClause) { return TRUE; } diff --git a/src/main/java/net/kemitix/conditional/TrueValueClause.java b/src/main/java/net/kemitix/conditional/TrueValueClause.java index c5c04ad..41707c3 100644 --- a/src/main/java/net/kemitix/conditional/TrueValueClause.java +++ b/src/main/java/net/kemitix/conditional/TrueValueClause.java @@ -48,6 +48,7 @@ class TrueValueClause implements Value.ValueClause { } @Override + @SuppressWarnings("PMD.ShortMethodName") public Value.ValueClause or(final Supplier clause) { return this; } @@ -60,6 +61,7 @@ class TrueValueClause implements Value.ValueClause { @RequiredArgsConstructor private static final class TrueValueSupplier implements ValueSupplier { + @SuppressWarnings("PMD.BeanMembersShouldSerialize") private final Supplier valueSupplier; @Override diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index fe2b182..d41b481 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -41,6 +41,7 @@ public interface Value { * * @return the value from either the trueSupplier or the falseSupplier */ + @SuppressWarnings("PMD.LawOfDemeter") static T where( final boolean clause, final Supplier trueSupplier, @@ -75,7 +76,10 @@ public interface Value { * @return a true or false value clause */ static ValueClause where(final boolean clause) { - return (ValueClause) (clause ? TrueValueClause.TRUE : FalseValueClause.FALSE); + if (clause) { + return TrueValueClause.TRUE; + } + return FalseValueClause.FALSE; } /** From ca83e1c24447be628f232887d541ce7a8e1a9c16 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Mar 2018 18:34:15 +0000 Subject: [PATCH 09/10] jenkins: update template --- Jenkinsfile.groovy | 62 +++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/Jenkinsfile.groovy b/Jenkinsfile.groovy index e799493..9207dae 100644 --- a/Jenkinsfile.groovy +++ b/Jenkinsfile.groovy @@ -3,49 +3,64 @@ final String mvn = "mvn --batch-mode --update-snapshots" pipeline { agent any stages { + stage('Environment') { + steps { + sh 'set' + } + } stage('no SNAPSHOT in master') { - // checks that the pom version is not a snapshot when the current branch is master - // TODO: also check for SNAPSHOT when is a pull request with master as the target branch + // checks that the pom version is not a snapshot when the current or target branch is master when { expression { - (env.GIT_BRANCH == 'master') && - (readMavenPom(file: 'pom.xml').version).contains("SNAPSHOT") } + (env.GIT_BRANCH == 'master' || env.CHANGE_TARGET == 'master') && + (readMavenPom(file: 'pom.xml').version).contains("SNAPSHOT") + } } steps { error("Build failed because SNAPSHOT version") } } stage('Static Code Analysis') { + when { expression { findFiles(glob: '**/src/main/java/**/*.java').length > 0 } } steps { - withMaven(maven: 'maven 3.5.2', jdk: 'JDK 1.8') { - sh "${mvn} compile checkstyle:checkstyle pmd:pmd" + withMaven(maven: 'maven', jdk: 'JDK LTS') { + sh "${mvn} compile" + sh "${mvn} checkstyle:checkstyle" + sh "${mvn} pmd:pmd" + pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: '' } - pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: '' } } - stage('Build') { - parallel { - stage('Java 8') { - steps { - withMaven(maven: 'maven 3.5.2', jdk: 'JDK 1.8') { - sh "${mvn} clean install" - } + stage('SonarQube (develop only)') { + when { expression { env.GIT_BRANCH == 'develop' && env.GIT_URL.startsWith('https://') } } + steps { + withSonarQubeEnv('sonarqube') { + withMaven(maven: 'maven', jdk: 'JDK LTS') { + sh "${mvn} org.sonarsource.scanner.maven:sonar-maven-plugin:3.4.0.905:sonar" } } - stage('Java 9') { - steps { - withMaven(maven: 'maven 3.5.2', jdk: 'JDK 9') { - sh "${mvn} clean install" - } - } + } + } + stage('Build Java Next') { + steps { + withMaven(maven: 'maven', jdk: 'JDK Next') { + sh "${mvn} clean install -Djava.version=9" + } + } + } + stage('Build Java LTS') { + steps { + withMaven(maven: 'maven', jdk: 'JDK LTS') { + sh "${mvn} clean install" } } } stage('Test Results') { + when { expression { findFiles(glob: '**/target/surefire-reports/*.xml').length > 0 } } steps { junit '**/target/surefire-reports/*.xml' jacoco exclusionPattern: '**/*{Test|IT|Main|Application|Immutable}.class' - withMaven(maven: 'maven 3.5.2', jdk: 'JDK 1.8') { + withMaven(maven: 'maven', jdk: 'JDK LTS') { sh "${mvn} com.gavinmogan:codacy-maven-plugin:coverage " + "-DcoverageReportFile=target/site/jacoco/jacoco.xml " + "-DprojectToken=`$JENKINS_HOME/codacy/token` " + @@ -55,14 +70,15 @@ pipeline { } } stage('Archiving') { + when { expression { findFiles(glob: '**/target/*.jar').length > 0 } } steps { archiveArtifacts '**/target/*.jar' } } stage('Deploy') { - when { expression { (env.GIT_BRANCH == 'master') } } + when { expression { (env.GIT_BRANCH == 'master' && env.GIT_URL.startsWith('https://')) } } steps { - withMaven(maven: 'maven 3.5.2', jdk: 'JDK 1.8') { + withMaven(maven: 'maven', jdk: 'JDK LTS') { sh "${mvn} deploy --activate-profiles release -DskipTests=true" } } From 1992db49f12641b8658a277137634f579041e5d8 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 13 Mar 2018 18:46:44 +0000 Subject: [PATCH 10/10] Suppress unchecked cast --- src/main/java/net/kemitix/conditional/FalseValueClause.java | 4 ++-- src/main/java/net/kemitix/conditional/TrueValueClause.java | 2 +- src/main/java/net/kemitix/conditional/Value.java | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/kemitix/conditional/FalseValueClause.java b/src/main/java/net/kemitix/conditional/FalseValueClause.java index b609428..f4283c3 100644 --- a/src/main/java/net/kemitix/conditional/FalseValueClause.java +++ b/src/main/java/net/kemitix/conditional/FalseValueClause.java @@ -33,11 +33,11 @@ import java.util.function.Supplier; */ class FalseValueClause implements Value.ValueClause { - protected static final Value.ValueClause FALSE = new FalseValueClause(); + protected static final Value.ValueClause FALSE = new FalseValueClause<>(); @Override public ValueSupplier then(final Supplier trueSupplier) { - return new FalseValueSupplier(); + return new FalseValueSupplier<>(); } @Override diff --git a/src/main/java/net/kemitix/conditional/TrueValueClause.java b/src/main/java/net/kemitix/conditional/TrueValueClause.java index 41707c3..46903c5 100644 --- a/src/main/java/net/kemitix/conditional/TrueValueClause.java +++ b/src/main/java/net/kemitix/conditional/TrueValueClause.java @@ -35,7 +35,7 @@ import java.util.function.Supplier; */ class TrueValueClause implements Value.ValueClause { - protected static final Value.ValueClause TRUE = new TrueValueClause<>(); + protected static final Value.ValueClause TRUE = new TrueValueClause<>(); @Override public ValueSupplier then(final Supplier trueSupplier) { diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index d41b481..c07737a 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -75,11 +75,12 @@ public interface Value { * * @return a true or false value clause */ + @SuppressWarnings("unchecked") static ValueClause where(final boolean clause) { if (clause) { - return TrueValueClause.TRUE; + return (ValueClause) TrueValueClause.TRUE; } - return FalseValueClause.FALSE; + return (ValueClause) FalseValueClause.FALSE; } /**