From 452a95a8d92d2d7b1fefab0663969a96f22d0fd3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 18:18:30 +0100 Subject: [PATCH 1/6] Update CHANGELOG for 0.6.0 --- CHANGELOG | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 2af9d3d..2d19ccc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,12 +4,22 @@ CHANGELOG 0.6.0 ----- +* Remove `.travis-support` +* Add `Condition.isTrue()` +* Add `Condition.isFalse()` +* Add `Condition.not()` * Add `Condition.and(Condition)` * Add `Condition.or(Condition)` * Add `Condition.not()` * Deprecate `Condition.whereNot(boolean)` * Deprecate `Condition.andNot(boolean)` * Deprecate `Condition.orNot(boolean)` +* Add `Condition.flatMap(Function)` to make `Condition` a monad +* Add `Value.where(Condition, Supplier, Supplier)` +* Add `Value.where(Condition, Supplier)` +* Add `Value.where(Condition)` +* Deprecate `Value.whereNot(boolean)` +* Add `ValueClause.not()` 0.5.0 ----- From f9c6badde5892d4ceff8c5f5d94807c17464853a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 19:42:35 +0100 Subject: [PATCH 2/6] README: convert to orgmode format --- README.adoc | 337 ---------------------------------------------------- README.org | 313 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 313 insertions(+), 337 deletions(-) delete mode 100644 README.adoc create mode 100644 README.org diff --git a/README.adoc b/README.adoc deleted file mode 100644 index 5eadee4..0000000 --- a/README.adoc +++ /dev/null @@ -1,337 +0,0 @@ -# Conditional - -image:https://img.shields.io/github/release/kemitix/conditional.svg["Releases", link="https://github.com/kemitix/conditional/releases"] -image:https://api.codacy.com/project/badge/Grade/1188742d676e457da91415d2b3a5faf1["Codacy code quality", link="https://www.codacy.com/app/kemitix/conditional"] -image:https://travis-ci.org/kemitix/conditional.svg?branch=master["Build Status", link="https://travis-ci.org/kemitix/conditional"] -image:https://coveralls.io/repos/github/kemitix/conditional/badge.svg?branch=master["Coverage Status", link="https://coveralls.io/github/kemitix/conditional?branch=master"] - -* link:#condition[Condition] -* link:#value[Value] - -## Condition - -If-then-else in a functional-style. - -### if-then - -[[source,java]] ----- -if (isTrue()) { - doSomething(); -} ----- - -[[source,java]] ----- -Condition.where(isTrue()) - .then(() -> doSomething()); ----- - -### if-then-else - -[[source,java]] ----- -if (isTrue()) { - doSomething(); -} else { - doSomethingElse(); -} ----- - -[[source,java]] ----- -Condition.where(isTrue()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); ----- - -### if-and-then-else - -[[source,java]] ----- -if (isTrue() && isAlsoTrue()) { - doSomething(); -} else { - doSomethingElse(); -} ----- - -[[source,java]] ----- -Condition.where(isTrue()) - .and(() -> isAlsoTrue()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); ----- - -### if-or-then-else - -[[source,java]] ----- -if (isTrue() || alternativeIsTrue()) { - doSomething(); -} else { - doSomethingElse(); -} ----- - -[[source,java]] ----- -Condition.where(isTrue()) - .or(() -> alternativeIsTrue()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); ----- - -### if-not-then-else - -[[source,java]] ----- -if (!isFalse()) { - doSomething(); -} else { - doSomethingElse(); -} ----- - -[[source,java]] ----- -Condition.whereNot(isFalse()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); ----- - -### if-and-not-then-else - -[[source,java]] ----- -if (isTrue() || !isFalse()) { - doSomething(); -} else { - doSomethingElse(); -} ----- - -[[source,java]] ----- -Condition.where(isTrue()) - .andNot(() -> isFalse()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); ----- - -### if-or-not-then-else - -[[source,java]] ----- -if (isFalse() || !isAlsoFalse()) { - doSomething(); -} else { - doSomethingElse(); -} ----- - -[[source,java]] ----- -Condition.where(isFalse()) - .orNot(() -> isAlsoFalse()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); ----- - -### if-then-else-if - -[[source,java]] ----- -if (isFalse()) { - doSomething(); -} else if (isTrue()) { - doSomethingElse(); -} ----- - -[[source,java]] ----- -Condition.where(isFalse()) - .then(() -> doSomething()) - .otherwise(() -> isTrue()) - .then(() -> doSomethingElse()); ----- - -### if-then-if-then - -[[source,java]] ----- -if (isTrue()) { - doSomething(); - if (isAlsoTrue()) { - doSomethingElse(); - } -} ----- - -[[source,java]] ----- -Condition.where(isTrue()) - .then(() -> doSomething()) - .and(() -> isAlsoTrue()) - .then(() -> doSomethingElse()); ----- - -## Value - -Values from an if-then-else in a functional-style. - -Functional, and verbose, alternative to the ternary operator (`?:`). - -### if-then-else - -[[source,java]] ----- -String result; -if (isTrue()) { - result = TRUE; -} else { - result = FALSE; -} ----- - -[[source,java]] ----- -String result = isTrue() ? TRUE : FALSE; ----- - -[[source,java]] ----- -final String result = Value.where(isTrue(), () -> TRUE, () -> FALSE); ----- - -[[source,java]] ----- -final Optional result = Value.where(isTrue(), () -> TRUE); ----- - -[[source,java]] ----- -final String result = Value.where(isTrue()) - .then(() -> TRUE) - .otherwise(() -> FALSE); ----- - -### if-not-then-else - -[[source,java]] ----- -String result; -if (!isTrue()) { - result = TRUE; -} else { - result = FALSE; -} ----- - -[[source,java]] ----- -final String result = Value.whereNot(isTrue()) - .then(() -> TRUE) - .otherwise(() -> FALSE); ----- - -### if-and-then-else - -[[source,java]] ----- -String result; -if (isTrue() && alternativeIsTrue()) { - result = TRUE; -} else { - result = FALSE; -} ----- - -[[source,java]] ----- -final String result = Value.where(isTrue()) - .and(() -> alternativeIsTrue()) - .then(() -> TRUE) - .otherwise(() -> FALSE); ----- - -### if-and-not-then-else - -[[source,java]] ----- -String result; -if (isTrue() && !alternativeIsFalse()) { - result = TRUE; -} else { - result = FALSE; -} ----- - -[[source,java]] ----- -final String result = Value.where(isTrue()) - .andNot(() -> alternativeIsFalse()) - .then(() -> TRUE) - .otherwise(() -> FALSE); ----- - -### if-or-then-else - -[[source,java]] ----- -String result; -if (isTrue() || alternativeIsTrue()) { - result = TRUE; -} else { - result = FALSE; -} ----- - -[[source,java]] ----- -final String result = Value.where(isTrue()) - .or(() -> alternativeIsTrue()) - .then(() -> TRUE) - .otherwise(() -> FALSE); ----- - -### if-or-not-then-else - -[[source,java]] ----- -String result; -if (isTrue() || !isFalse()) { - result = TRUE; -} else { - result = FALSE; -} ----- - -[[source,java]] ----- -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/README.org b/README.org new file mode 100644 index 0000000..3cc00e8 --- /dev/null +++ b/README.org @@ -0,0 +1,313 @@ +* Conditional + +** Functional Condition and Value. + + [[https://oss.sonatype.org/content/repositories/releases/net/kemitix/conditional][file:https://img.shields.io/nexus/r/https/oss.sonatype.org/net.kemitix/conditional.svg?style=for-the-badge]] + [[https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.kemitix%22%20AND%20a%3A%22conditional%22][file:https://img.shields.io/maven-central/v/net.kemitix/conditional.svg?style=for-the-badge]] + + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://img.shields.io/sonar/https/sonarcloud.io/net.kemitix%3Aconditional/coverage.svg?style=for-the-badge#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://img.shields.io/sonar/https/sonarcloud.io/net.kemitix%3Aconditional/tech_debt.svg?style=for-the-badge#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=sqale_rating#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=alert_status#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=reliability_rating#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=security_rating#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=sqale_index#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=vulnerabilities#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=bugs#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=code_smells#.svg]] + [[https://sonarcloud.io/dashboard?id=net.kemitix%3Aconditional][file:https://sonarcloud.io/api/project_badges/measure?project=net.kemitix%3Aconditional&metric=ncloc#.svg]] + + [[https://app.codacy.com/project/kemitix/conditional/dashboard][file:https://img.shields.io/codacy/grade/1188742d676e457da91415d2b3a5faf1.svg?style=for-the-badge]] + [[http://i.jpeek.org/net.kemitix/conditional/index.html][file:http://i.jpeek.org/net.kemitix/conditional/badge.svg]] + + +** Maven + + #+BEGIN_SRC xml + + net.kemitix + conditional + RELEASE + + #+END_SRC + + The latest version should be shown above with the nexus and maven-central + badges or can be found on [[https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.kemitix%22%20AND%20a%3A%22mon%22][Maven Central]]. + + +** Condition + + The `if ... then` and `if ... then ... else` constructs can't be used in a + lambda without being wrapped in a code block. + + Using `Condition` allows the use of an if-style expression without needing to + wrap it in a code block + + +*** if-then + + #+BEGIN_SRC java + if (isTrue()) { + doSomething(); + } + + Condition.where(isTrue()) + .then(() -> doSomething()); + #+END_SRC + + +*** if-then-else + + #+BEGIN_SRC java + if (isTrue()) { + doSomething(); + } else { + doSomethingElse(); + } + + Condition.where(isTrue()) + .then(() -> doSomething()) + .otherwise(() -> doSomethingElse()); + #+END_SRC + + +*** if-and-then-else + + #+BEGIN_SRC java + if (isTrue() && isAlsoTrue()) { + doSomething(); + } else { + doSomethingElse(); + } + + Condition.where(isTrue()) + .and(() -> isAlsoTrue()) + .then(() -> doSomething()) + .otherwise(() -> doSomethingElse()); + #+END_SRC + + +*** if-or-then-else + + #+BEGIN_SRC java + if (isTrue() || alternativeIsTrue()) { + doSomething(); + } else { + doSomethingElse(); + } + + Condition.where(isTrue()) + .or(() -> alternativeIsTrue()) + .then(() -> doSomething()) + .otherwise(() -> doSomethingElse()); + #+END_SRC + + +*** if-not-then-else + + #+BEGIN_SRC java + if (!isFalse()) { + doSomething(); + } else { + doSomethingElse(); + } + + Condition.whereNot(isFalse()) + .then(() -> doSomething()) + .otherwise(() -> doSomethingElse()); + #+END_SRC + + +*** if-and-not-then-else + + #+BEGIN_SRC java + if (isTrue() || !isFalse()) { + doSomething(); + } else { + doSomethingElse(); + } + + Condition.where(isTrue()) + .andNot(() -> isFalse()) + .then(() -> doSomething()) + .otherwise(() -> doSomethingElse()); + #+END_SRC + + +*** if-or-not-then-else + + #+BEGIN_SRC java + if (isFalse() || !isAlsoFalse()) { + doSomething(); + } else { + doSomethingElse(); + } + + Condition.where(isFalse()) + .orNot(() -> isAlsoFalse()) + .then(() -> doSomething()) + .otherwise(() -> doSomethingElse()); + #+END_SRC + + +*** if-then-else-if + + #+BEGIN_SRC java + if (isFalse()) { + doSomething(); + } else if (isTrue()) { + doSomethingElse(); + } + + Condition.where(isFalse()) + .then(() -> doSomething()) + .otherwise(() -> isTrue()) + .then(() -> doSomethingElse()); + #+END_SRC + + +*** if-then-if-then + + #+BEGIN_SRC java + if (isTrue()) { + doSomething(); + if (isAlsoTrue()) { + doSomethingElse(); + } + } + + Condition.where(isTrue()) + .then(() -> doSomething()) + .and(() -> isAlsoTrue()) + .then(() -> doSomethingElse()); + #+END_SRC + + +** Value + + Values from an if-then-else in a functional-style. + + Functional, and verbose, alternative to the ternary operator (=?:=). + + +*** if-then-else + + #+BEGIN_SRC java + String result; + if (isTrue()) { + result = TRUE; + } else { + result = FALSE; + } + + String result = isTrue() ? TRUE : FALSE; + + final String result = Value.where(isTrue(), () -> TRUE, () -> FALSE); + + final Optional result = Value.where(isTrue(), () -> TRUE); + + final String result = Value.where(isTrue()) + .then(() -> TRUE) + .otherwise(() -> FALSE); + #+END_SRC + + +*** if-not-then-else + + #+BEGIN_SRC java + String result; + if (!isTrue()) { + result = TRUE; + } else { + result = FALSE; + } + + final String result = Value.whereNot(isTrue()) + .then(() -> TRUE) + .otherwise(() -> FALSE); + #+END_SRC + + +*** if-and-then-else + + #+BEGIN_SRC java + String result; + if (isTrue() && alternativeIsTrue()) { + result = TRUE; + } else { + result = FALSE; + } + + final String result = Value.where(isTrue()) + .and(() -> alternativeIsTrue()) + .then(() -> TRUE) + .otherwise(() -> FALSE); + #+END_SRC + + +*** if-and-not-then-else + + #+BEGIN_SRC java + String result; + if (isTrue() && !alternativeIsFalse()) { + result = TRUE; + } else { + result = FALSE; + } + + final String result = Value.where(isTrue()) + .andNot(() -> alternativeIsFalse()) + .then(() -> TRUE) + .otherwise(() -> FALSE); + #+END_SRC + + +*** if-or-then-else + + #+BEGIN_SRC java + String result; + if (isTrue() || alternativeIsTrue()) { + result = TRUE; + } else { + result = FALSE; + } + + final String result = Value.where(isTrue()) + .or(() -> alternativeIsTrue()) + .then(() -> TRUE) + .otherwise(() -> FALSE); + #+END_SRC + + +*** if-or-not-then-else + + #+BEGIN_SRC java + String result; + if (isTrue() || !isFalse()) { + result = TRUE; + } else { + result = FALSE; + } + + final String result = Value.where(isTrue()) + .orNot(() -> isFalse()) + .then(() -> TRUE) + .otherwise(() -> FALSE); + #+END_SRC + + +*** if-then + + #+BEGIN_SRC java + Optional result; + if (isTrue()) { + result = Optional.of(TRUE); + } else { + result = Optional.empty(); + } + + final Optional result = Value.where(isTrue()) + .then(() -> TRUE) + .optional(); + #+END_SRC + From 888123b7225ba4c3768178763dcc12583210ab67 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 19:53:31 +0100 Subject: [PATCH 3/6] Value: cleanup deprecated whereNot() --- README.org | 1 + src/main/java/net/kemitix/conditional/Value.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.org b/README.org index 3cc00e8..a7884cb 100644 --- a/README.org +++ b/README.org @@ -1,5 +1,6 @@ * Conditional + ** Functional Condition and Value. [[https://oss.sonatype.org/content/repositories/releases/net/kemitix/conditional][file:https://img.shields.io/nexus/r/https/oss.sonatype.org/net.kemitix/conditional.svg?style=for-the-badge]] diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index 81c8d01..ca4ddc2 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -134,7 +134,7 @@ public interface Value { */ @Deprecated static ValueClause whereNot(final boolean clause) { - return where(!clause); + return Value.where(clause).not(); } /** From 47d3832a14668fe8a64d59b350a87f7069d4a053 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 20:08:08 +0100 Subject: [PATCH 4/6] README: update Condition examples for 0.6.0 --- README.org | 69 +++++++++++++++++------------------------------------- 1 file changed, 22 insertions(+), 47 deletions(-) diff --git a/README.org b/README.org index a7884cb..cffa95e 100644 --- a/README.org +++ b/README.org @@ -72,6 +72,28 @@ #+END_SRC +*** isTrue() / isFalse() / not() + + #+BEGIN_SRC java + final Condition condition = Condition.where(isTrue()); + final boolean isTrue = condition.isTrue(); + final boolean isFalse = condition.isFalse(); + final Condition not = condition.not(); + final Condition andCondition1 = condition.and(Condition.where(isAlsoTrue())); + final Condition andCondition2 = condition.and(isAlsoTrue()); + final Condition orCondition1 = condition.or(Condition.where(isAlsoTrue())); + final Condition orCondition2 = condition.or(isAlsoTrue()); + #+END_SRC + + +*** flatMap(Function) + + #+BEGIN_SRC java + final Condition condition = Condition.where(isTrue()) + .flatMap(b -> Condition.where(b)); + #+END_SRC + + *** if-and-then-else #+BEGIN_SRC java @@ -104,53 +126,6 @@ #+END_SRC -*** if-not-then-else - - #+BEGIN_SRC java - if (!isFalse()) { - doSomething(); - } else { - doSomethingElse(); - } - - Condition.whereNot(isFalse()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); - #+END_SRC - - -*** if-and-not-then-else - - #+BEGIN_SRC java - if (isTrue() || !isFalse()) { - doSomething(); - } else { - doSomethingElse(); - } - - Condition.where(isTrue()) - .andNot(() -> isFalse()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); - #+END_SRC - - -*** if-or-not-then-else - - #+BEGIN_SRC java - if (isFalse() || !isAlsoFalse()) { - doSomething(); - } else { - doSomethingElse(); - } - - Condition.where(isFalse()) - .orNot(() -> isAlsoFalse()) - .then(() -> doSomething()) - .otherwise(() -> doSomethingElse()); - #+END_SRC - - *** if-then-else-if #+BEGIN_SRC java From 42b4a7ec7ccbce5b35e85367cade32c2db327d34 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 20:16:44 +0100 Subject: [PATCH 5/6] Deprecate `Value.andNot(Supplier)` --- CHANGELOG | 5 +++ README.org | 33 ------------------- .../java/net/kemitix/conditional/Value.java | 4 ++- 3 files changed, 8 insertions(+), 34 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2d19ccc..9e0efab 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,11 @@ CHANGELOG ========= +0.7.0 +----- + +* Deprecate `Value.andNot(Supplier)` + 0.6.0 ----- diff --git a/README.org b/README.org index cffa95e..5f293fa 100644 --- a/README.org +++ b/README.org @@ -188,22 +188,6 @@ #+END_SRC -*** if-not-then-else - - #+BEGIN_SRC java - String result; - if (!isTrue()) { - result = TRUE; - } else { - result = FALSE; - } - - final String result = Value.whereNot(isTrue()) - .then(() -> TRUE) - .otherwise(() -> FALSE); - #+END_SRC - - *** if-and-then-else #+BEGIN_SRC java @@ -221,23 +205,6 @@ #+END_SRC -*** if-and-not-then-else - - #+BEGIN_SRC java - String result; - if (isTrue() && !alternativeIsFalse()) { - result = TRUE; - } else { - result = FALSE; - } - - final String result = Value.where(isTrue()) - .andNot(() -> alternativeIsFalse()) - .then(() -> TRUE) - .otherwise(() -> FALSE); - #+END_SRC - - *** if-or-then-else #+BEGIN_SRC java diff --git a/src/main/java/net/kemitix/conditional/Value.java b/src/main/java/net/kemitix/conditional/Value.java index ca4ddc2..cf8b263 100644 --- a/src/main/java/net/kemitix/conditional/Value.java +++ b/src/main/java/net/kemitix/conditional/Value.java @@ -181,9 +181,11 @@ public interface Value { * * @param clause the condition to test * @return a true or false value clause + * @deprecated use {@link #and(Supplier)}.{@link #not()} */ + @Deprecated default ValueClause andNot(final Supplier clause) { - return and(() -> !clause.get()); + return and(clause).not(); } /** From 8506cbf489fa65f5f665b60fd45da8649507c674 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 28 Jul 2018 20:23:17 +0100 Subject: [PATCH 6/6] README: update Value examples for 0.6.0 --- README.org | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.org b/README.org index 5f293fa..07983ee 100644 --- a/README.org +++ b/README.org @@ -180,11 +180,14 @@ final String result = Value.where(isTrue(), () -> TRUE, () -> FALSE); + final String result = Value.where(Condition.where(isTrue), () -> TRUE, () -> FALSE); + final Optional result = Value.where(isTrue(), () -> TRUE); final String result = Value.where(isTrue()) - .then(() -> TRUE) - .otherwise(() -> FALSE); + .not() + .then(() -> FALSE) + .otherwise(() -> TRUE); #+END_SRC