Functional-style if-then-else
Paul Campbell
3fcd5450bf
* release/0.2.0: pom.xml: version set to 0.2.0 README.adoc: add documentation for Value Value: clean up unneeded generics type parameters Value: add javadoc ValueTest: remove ambiguity on then() and otherwise() parameters Value: rewrite using strong chaining logic Value: add initial implementation ConditionalTest: more tests for whereNot(), andNot() and orNot() Condition: remove use of 'if' internaly ConditionalTest: more tests ConditionalTest: clean up helper methods pom.xml: version set to 0.2.0-SNAPSHOT |
||
---|---|---|
src | ||
.gitignore | ||
.travis.yml | ||
LICENSE.txt | ||
pom.xml | ||
README.adoc |
# 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.<String>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.<String>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.<String>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.<String>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.<String>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.<String>where(isTrue()).orNot(isFalse()) .then(() -> TRUE) .otherwise(() -> FALSE); ----