commit
a9bdd03904
17 changed files with 463 additions and 201 deletions
|
@ -5,7 +5,7 @@ cache:
|
|||
directories:
|
||||
- "$HOME/.m2"
|
||||
install: true
|
||||
script: "./mvnw clean install"
|
||||
script: "./mvnw -B -U clean install"
|
||||
after_success:
|
||||
- sh .travis-support/coveralls.sh
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
CHANGELOG
|
||||
=========
|
||||
|
||||
0.4.0
|
||||
-----
|
||||
|
||||
* Building with Jenkins
|
||||
* Upgrade `kemitix-parent` to 5.0.3
|
||||
|
||||
0.3.0
|
||||
-----
|
||||
|
||||
|
|
87
Jenkinsfile.groovy
Normal file
87
Jenkinsfile.groovy
Normal file
|
@ -0,0 +1,87 @@
|
|||
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 or target branch is master
|
||||
when {
|
||||
expression {
|
||||
(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', jdk: 'JDK LTS') {
|
||||
sh "${mvn} compile"
|
||||
sh "${mvn} checkstyle:checkstyle"
|
||||
sh "${mvn} pmd:pmd"
|
||||
pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: ''
|
||||
}
|
||||
}
|
||||
}
|
||||
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('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', jdk: 'JDK LTS') {
|
||||
sh "${mvn} com.gavinmogan:codacy-maven-plugin:coverage " +
|
||||
"-DcoverageReportFile=target/site/jacoco/jacoco.xml " +
|
||||
"-DprojectToken=`$JENKINS_HOME/codacy/token` " +
|
||||
"-DapiToken=`$JENKINS_HOME/codacy/apitoken` " +
|
||||
"-Dcommit=`git rev-parse HEAD`"
|
||||
}
|
||||
}
|
||||
}
|
||||
stage('Archiving') {
|
||||
when { expression { findFiles(glob: '**/target/*.jar').length > 0 } }
|
||||
steps {
|
||||
archiveArtifacts '**/target/*.jar'
|
||||
}
|
||||
}
|
||||
stage('Deploy') {
|
||||
when { expression { (env.GIT_BRANCH == 'master' && env.GIT_URL.startsWith('https://')) } }
|
||||
steps {
|
||||
withMaven(maven: 'maven', jdk: 'JDK LTS') {
|
||||
sh "${mvn} deploy --activate-profiles release -DskipTests=true"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
49
README.adoc
49
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,7 +213,8 @@ final Optional<String> result = Value.where(isTrue(), () -> TRUE);
|
|||
|
||||
[[source,java]]
|
||||
----
|
||||
final String result = Value.<String>where(isTrue()).then(() -> TRUE)
|
||||
final String result = Value.<String>where(isTrue())
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
----
|
||||
|
||||
|
@ -231,7 +232,8 @@ if (!isTrue()) {
|
|||
|
||||
[[source,java]]
|
||||
----
|
||||
final String result = Value.<String>whereNot(isTrue()).then(() -> TRUE)
|
||||
final String result = Value.<String>whereNot(isTrue())
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
----
|
||||
|
||||
|
@ -249,7 +251,8 @@ if (isTrue() && alternativeIsTrue()) {
|
|||
|
||||
[[source,java]]
|
||||
----
|
||||
final String result = Value.<String>where(isTrue()).and(alternativeIsTrue())
|
||||
final String result = Value.<String>where(isTrue())
|
||||
.and(() -> alternativeIsTrue())
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
----
|
||||
|
@ -268,7 +271,8 @@ if (isTrue() && !alternativeIsFalse()) {
|
|||
|
||||
[[source,java]]
|
||||
----
|
||||
final String result = Value.<String>where(isTrue()).andNot(alternativeIsFalse())
|
||||
final String result = Value.<String>where(isTrue())
|
||||
.andNot(() -> alternativeIsFalse())
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
----
|
||||
|
@ -287,7 +291,8 @@ if (isTrue() || alternativeIsTrue()) {
|
|||
|
||||
[[source,java]]
|
||||
----
|
||||
final String result = Value.<String>where(isTrue()).or(alternativeIsTrue())
|
||||
final String result = Value.<String>where(isTrue())
|
||||
.or(() -> alternativeIsTrue())
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
----
|
||||
|
@ -306,7 +311,27 @@ if (isTrue() || !isFalse()) {
|
|||
|
||||
[[source,java]]
|
||||
----
|
||||
final String result = Value.<String>where(isTrue()).orNot(isFalse())
|
||||
final String result = Value.<String>where(isTrue())
|
||||
.orNot(() -> isFalse())
|
||||
.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();
|
||||
-----
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
test:
|
||||
override:
|
||||
- ./mvnw clean install
|
1
lombok.config
Normal file
1
lombok.config
Normal file
|
@ -0,0 +1 @@
|
|||
lombok.addGeneratedAnnotation=false
|
46
pom.xml
46
pom.xml
|
@ -4,28 +4,38 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<kemitix-checkstyle-ruleset.version>3.2.0</kemitix-checkstyle-ruleset.version>
|
||||
<kemitix-checkstyle-ruleset.level>5-complexity</kemitix-checkstyle-ruleset.level>
|
||||
<java.version>1.8</java.version>
|
||||
<kemitix-checkstyle.version>4.0.1</kemitix-checkstyle.version>
|
||||
<kemitix.checkstyle.ruleset.level>5-complexity</kemitix.checkstyle.ruleset.level>
|
||||
<jacoco-class-line-covered-ratio>1</jacoco-class-line-covered-ratio>
|
||||
<jacoco-class-instruction-covered-ratio>1</jacoco-class-instruction-covered-ratio>
|
||||
<jacoco-class-missed-count-maximum>0</jacoco-class-missed-count-maximum>
|
||||
<pitest.coverage>100</pitest.coverage>
|
||||
<pitest.mutation>100</pitest.mutation>
|
||||
<junit.version>4.12</junit.version>
|
||||
<assertj.version>3.8.0</assertj.version>
|
||||
<assertj.version>3.9.1</assertj.version>
|
||||
<coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
|
||||
<tiles-maven-plugin.version>2.10</tiles-maven-plugin.version>
|
||||
<kemitix-tiles.version>0.8.1</kemitix-tiles.version>
|
||||
<lombok.version>1.16.20</lombok.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>net.kemitix</groupId>
|
||||
<artifactId>kemitix-parent</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<version>5.1.0</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<artifactId>conditional</artifactId>
|
||||
<version>0.3.0</version>
|
||||
<version>0.4.0</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
@ -43,25 +53,17 @@
|
|||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.kemitix</groupId>
|
||||
<artifactId>kemitix-checkstyle-ruleset-maven-plugin</artifactId>
|
||||
<version>${kemitix-checkstyle-ruleset.version}</version>
|
||||
<groupId>io.repaint.maven</groupId>
|
||||
<artifactId>tiles-maven-plugin</artifactId>
|
||||
<version>${tiles-maven-plugin.version}</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<level>${kemitix-checkstyle-ruleset.level}</level>
|
||||
<tiles>
|
||||
<tile>net.kemitix.tiles:all:${kemitix-tiles.version}</tile>
|
||||
<tile>net.kemitix.tiles:pmd-strict:${kemitix-tiles.version}</tile>
|
||||
<tile>net.kemitix.checkstyle:tile:${kemitix-checkstyle.version}</tile>
|
||||
</tiles>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.eluder.coveralls</groupId>
|
||||
<artifactId>coveralls-maven-plugin</artifactId>
|
||||
<version>${coveralls-maven-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
36
src/main/java/net/kemitix/conditional/Action.java
Normal file
36
src/main/java/net/kemitix/conditional/Action.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Paul Campbell
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies
|
||||
* or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
|
||||
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.kemitix.conditional;
|
||||
|
||||
/**
|
||||
* An action to perform in a clause when a {@link Condition} is met.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Action {
|
||||
|
||||
/**
|
||||
* The action to perform.
|
||||
*/
|
||||
void perform();
|
||||
}
|
|
@ -21,6 +21,8 @@
|
|||
|
||||
package net.kemitix.conditional;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* If-then-else in a functional-style.
|
||||
*
|
||||
|
@ -35,9 +37,11 @@ public interface Condition {
|
|||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
@SuppressWarnings("avoidinlineconditionals")
|
||||
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
|
||||
*/
|
||||
Condition and(boolean clause);
|
||||
Condition and(Supplier<Boolean> clause);
|
||||
|
||||
/**
|
||||
* Logically AND combine the current {@code Condition} with boolean opposite of the clause.
|
||||
|
@ -67,8 +71,8 @@ public interface Condition {
|
|||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
default Condition andNot(final boolean clause) {
|
||||
return and(!clause);
|
||||
default Condition andNot(final Supplier<Boolean> clause) {
|
||||
return and(() -> !clause.get());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,7 +82,8 @@ public interface Condition {
|
|||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
Condition or(boolean clause);
|
||||
@SuppressWarnings("PMD.ShortMethodName")
|
||||
Condition or(Supplier<Boolean> clause);
|
||||
|
||||
/**
|
||||
* Logically OR combine the current {@code Condition} with the boolean opposite of the clause.
|
||||
|
@ -87,8 +92,8 @@ public interface Condition {
|
|||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
default Condition orNot(final boolean clause) {
|
||||
return or(!clause);
|
||||
default Condition orNot(final Supplier<Boolean> clause) {
|
||||
return or(() -> !clause.get());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,14 +103,14 @@ public interface Condition {
|
|||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
Condition then(Runnable response);
|
||||
Condition then(Action response);
|
||||
|
||||
/**
|
||||
* Perform this response if the {@code Condition} is {@code false}.
|
||||
*
|
||||
* @param response the response to perform
|
||||
*/
|
||||
void otherwise(Runnable response);
|
||||
void otherwise(Action response);
|
||||
|
||||
/**
|
||||
* Create a new {@code Condition} for the clause as a continuation to an existing {@code Condition}.
|
||||
|
@ -114,8 +119,8 @@ public interface Condition {
|
|||
*
|
||||
* @return the Condition
|
||||
*/
|
||||
default Condition otherwise(final boolean clause) {
|
||||
return where(clause);
|
||||
default Condition otherwise(final Supplier<Boolean> clause) {
|
||||
return where(clause.get());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
|
||||
package net.kemitix.conditional;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* A {@code Condition} that has evaluated to {@code false}.
|
||||
*
|
||||
|
@ -28,26 +30,27 @@ package net.kemitix.conditional;
|
|||
*/
|
||||
final class FalseCondition implements Condition {
|
||||
|
||||
protected static final Condition FALSE = new net.kemitix.conditional.FalseCondition();
|
||||
public static final Condition FALSE = new net.kemitix.conditional.FalseCondition();
|
||||
|
||||
@Override
|
||||
public Condition and(final boolean clause) {
|
||||
public Condition and(final Supplier<Boolean> clause) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition or(final boolean secondClause) {
|
||||
return Condition.where(secondClause);
|
||||
@SuppressWarnings("PMD.ShortMethodName")
|
||||
public Condition or(final Supplier<Boolean> secondClause) {
|
||||
return Condition.where(secondClause.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition then(final Runnable response) {
|
||||
public Condition then(final Action response) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void otherwise(final Runnable response) {
|
||||
response.run();
|
||||
public void otherwise(final Action response) {
|
||||
response.perform();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
package net.kemitix.conditional;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
|
@ -32,33 +33,41 @@ import java.util.function.Supplier;
|
|||
*/
|
||||
class FalseValueClause<T> implements Value.ValueClause<T> {
|
||||
|
||||
protected static final Value.ValueClause FALSE = new FalseValueClause();
|
||||
protected static final Value.ValueClause<?> FALSE = new FalseValueClause<>();
|
||||
|
||||
@Override
|
||||
public ValueSupplier<T> then(final Supplier<T> trueSupplier) {
|
||||
return new FalseValueSupplier();
|
||||
return new FalseValueSupplier<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value.ValueClause<T> and(final boolean clause) {
|
||||
public Value.ValueClause<T> and(final Supplier<Boolean> clause) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value.ValueClause<T> or(final boolean clause) {
|
||||
return Value.where(clause);
|
||||
@SuppressWarnings("PMD.ShortMethodName")
|
||||
public Value.ValueClause<T> or(final Supplier<Boolean> clause) {
|
||||
return Value.where(clause.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* An intermediate result of the {@link Value} where the clause has evaluated to false.
|
||||
*
|
||||
* @param <T> the type of the value
|
||||
*/
|
||||
private class FalseValueSupplier implements ValueSupplier<T> {
|
||||
private static final class FalseValueSupplier<T> implements ValueSupplier<T> {
|
||||
|
||||
@Override
|
||||
public T otherwise(final Supplier<T> falseSupplier) {
|
||||
return falseSupplier.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> optional() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
|
||||
package net.kemitix.conditional;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* A {@code Condition} that has evaluated to {@code true}.
|
||||
*
|
||||
|
@ -28,26 +30,27 @@ package net.kemitix.conditional;
|
|||
*/
|
||||
final class TrueCondition implements Condition {
|
||||
|
||||
protected static final Condition TRUE = new net.kemitix.conditional.TrueCondition();
|
||||
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<Boolean> clause) {
|
||||
return Condition.where(clause.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition or(final boolean secondClause) {
|
||||
@SuppressWarnings("PMD.ShortMethodName")
|
||||
public Condition or(final Supplier<Boolean> secondClause) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition then(final Runnable response) {
|
||||
response.run();
|
||||
public Condition then(final Action response) {
|
||||
response.perform();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void otherwise(final Runnable response) {
|
||||
public void otherwise(final Action response) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ package net.kemitix.conditional;
|
|||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
|
@ -34,29 +35,33 @@ import java.util.function.Supplier;
|
|||
*/
|
||||
class TrueValueClause<T> implements Value.ValueClause<T> {
|
||||
|
||||
protected static final Value.ValueClause TRUE = new TrueValueClause();
|
||||
protected static final Value.ValueClause<?> TRUE = new TrueValueClause<>();
|
||||
|
||||
@Override
|
||||
public ValueSupplier<T> then(final Supplier<T> trueSupplier) {
|
||||
return new TrueValueSupplier(trueSupplier);
|
||||
return new TrueValueSupplier<>(trueSupplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value.ValueClause<T> and(final boolean clause) {
|
||||
return Value.where(clause);
|
||||
public Value.ValueClause<T> and(final Supplier<Boolean> clause) {
|
||||
return Value.where(clause.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value.ValueClause<T> or(final boolean clause) {
|
||||
@SuppressWarnings("PMD.ShortMethodName")
|
||||
public Value.ValueClause<T> or(final Supplier<Boolean> clause) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* An intermediate result of the {@link Value} where the clause has evaluated to true.
|
||||
*
|
||||
* @param <T> the type of the value
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
private class TrueValueSupplier implements ValueSupplier<T> {
|
||||
private static final class TrueValueSupplier<T> implements ValueSupplier<T> {
|
||||
|
||||
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
|
||||
private final Supplier<T> valueSupplier;
|
||||
|
||||
@Override
|
||||
|
@ -64,6 +69,11 @@ class TrueValueClause<T> implements Value.ValueClause<T> {
|
|||
return valueSupplier.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> optional() {
|
||||
return Optional.ofNullable(valueSupplier.get());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -41,10 +41,11 @@ public interface Value {
|
|||
*
|
||||
* @return the value from either the trueSupplier or the falseSupplier
|
||||
*/
|
||||
@SuppressWarnings("PMD.LawOfDemeter")
|
||||
static <T> T where(
|
||||
boolean clause,
|
||||
Supplier<T> trueSupplier,
|
||||
Supplier<T> falseSupplier
|
||||
final boolean clause,
|
||||
final Supplier<T> trueSupplier,
|
||||
final Supplier<T> falseSupplier
|
||||
) {
|
||||
return Value.<T>where(clause).then(trueSupplier)
|
||||
.otherwise(falseSupplier);
|
||||
|
@ -60,8 +61,8 @@ public interface Value {
|
|||
* @return an Optional either containing the value from the trueSupplier or empty
|
||||
*/
|
||||
static <T> Optional<T> where(
|
||||
boolean clause,
|
||||
Supplier<T> trueSupplier
|
||||
final boolean clause,
|
||||
final Supplier<T> trueSupplier
|
||||
) {
|
||||
return Optional.ofNullable(Value.where(clause, trueSupplier, () -> null));
|
||||
}
|
||||
|
@ -74,9 +75,12 @@ public interface Value {
|
|||
*
|
||||
* @return a true or false value clause
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "avoidinlineconditionals"})
|
||||
@SuppressWarnings("unchecked")
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -87,7 +91,7 @@ public interface Value {
|
|||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -114,7 +118,7 @@ public interface Value {
|
|||
*
|
||||
* @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.
|
||||
|
@ -123,7 +127,8 @@ public interface Value {
|
|||
*
|
||||
* @return a true or false value clause
|
||||
*/
|
||||
ValueClause<T> or(boolean clause);
|
||||
@SuppressWarnings("PMD.ShortMethodName")
|
||||
ValueClause<T> or(Supplier<Boolean> clause);
|
||||
|
||||
/**
|
||||
* Logically AND combine the current {@link ValueClause} with boolean opposite of the clause.
|
||||
|
@ -132,8 +137,8 @@ public interface Value {
|
|||
*
|
||||
* @return a true or false value clause
|
||||
*/
|
||||
default ValueClause<T> andNot(final boolean clause) {
|
||||
return and(!clause);
|
||||
default ValueClause<T> andNot(final Supplier<Boolean> clause) {
|
||||
return and(() -> !clause.get());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -143,8 +148,8 @@ public interface Value {
|
|||
*
|
||||
* @return a true or false value clause
|
||||
*/
|
||||
default ValueClause<T> orNot(boolean clause) {
|
||||
return or(!clause);
|
||||
default ValueClause<T> orNot(final Supplier<Boolean> clause) {
|
||||
return or(() -> !clause.get());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -163,6 +168,12 @@ public interface Value {
|
|||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
@ -10,9 +12,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class ConditionalTest {
|
||||
|
||||
private Runnable thenResponse;
|
||||
private Action thenResponse;
|
||||
|
||||
private Runnable otherwiseResponse;
|
||||
private Action otherwiseResponse;
|
||||
|
||||
private boolean thenFlag;
|
||||
|
||||
|
@ -77,7 +79,7 @@ public class ConditionalTest {
|
|||
//when
|
||||
Condition.where(true)
|
||||
.then(thenResponse)
|
||||
.and(true)
|
||||
.and(() -> true)
|
||||
.then(otherwiseResponse);
|
||||
//then
|
||||
assertThatBothResponsesRun();
|
||||
|
@ -138,7 +140,7 @@ public class ConditionalTest {
|
|||
public void whereTrueAndNotFalseThenRuns() {
|
||||
//when
|
||||
Condition.where(true)
|
||||
.andNot(false)
|
||||
.andNot(() -> false)
|
||||
.then(thenResponse);
|
||||
//then
|
||||
assertThatTheThenResponseRuns();
|
||||
|
@ -148,7 +150,7 @@ public class ConditionalTest {
|
|||
public void whereTrueAndNotTrueThenOtherwiseRuns() {
|
||||
//when
|
||||
Condition.where(true)
|
||||
.andNot(true)
|
||||
.andNot(() -> true)
|
||||
.then(thenResponse)
|
||||
.otherwise(otherwiseResponse);
|
||||
//then
|
||||
|
@ -159,7 +161,7 @@ public class ConditionalTest {
|
|||
public void whereFalseOrNotFalseThenRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
.orNot(false)
|
||||
.orNot(() -> false)
|
||||
.then(thenResponse);
|
||||
//then
|
||||
assertThatTheThenResponseRuns();
|
||||
|
@ -169,7 +171,7 @@ public class ConditionalTest {
|
|||
public void whereFalseOrNotTrueThenOtherwiseRuns() {
|
||||
//when
|
||||
Condition.where(false)
|
||||
.orNot(true)
|
||||
.orNot(() -> true)
|
||||
.then(thenResponse)
|
||||
.otherwise(otherwiseResponse);
|
||||
//then
|
||||
|
@ -181,7 +183,7 @@ public class ConditionalTest {
|
|||
//when
|
||||
Condition.where(false)
|
||||
.then(thenResponse)
|
||||
.otherwise(true)
|
||||
.otherwise(() -> true)
|
||||
.then(otherwiseResponse);
|
||||
//then
|
||||
assertThatTheOtherwiseResponseRuns();
|
||||
|
@ -192,7 +194,7 @@ public class ConditionalTest {
|
|||
//when
|
||||
Condition.where(false)
|
||||
.then(thenResponse)
|
||||
.otherwise(false)
|
||||
.otherwise(() -> false)
|
||||
.then(otherwiseResponse);
|
||||
//then
|
||||
assertThatNoResponseRuns();
|
||||
|
@ -269,7 +271,7 @@ public class ConditionalTest {
|
|||
final boolean secondClause
|
||||
) {
|
||||
Condition.where(firstClause)
|
||||
.and(secondClause)
|
||||
.and(() -> secondClause)
|
||||
.then(thenResponse)
|
||||
.otherwise(otherwiseResponse);
|
||||
}
|
||||
|
@ -279,8 +281,34 @@ public class ConditionalTest {
|
|||
final boolean secondClause
|
||||
) {
|
||||
Condition.where(firstClause)
|
||||
.or(secondClause)
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -69,7 +70,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereTrueAndTrueIsTrue() {
|
||||
//when
|
||||
val result = Value.<String>where(true).and(true)
|
||||
val result = Value.<String>where(true).and(() -> true)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -79,7 +80,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereTrueAndFalseIsFalse() {
|
||||
//when
|
||||
val result = Value.<String>where(true).and(false)
|
||||
val result = Value.<String>where(true).and(() -> false)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -89,7 +90,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereFalseAndTrueIsFalse() {
|
||||
//when
|
||||
val result = Value.<String>where(false).and(true)
|
||||
val result = Value.<String>where(false).and(() -> true)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -99,7 +100,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereFalseAndFalseIsFalse() {
|
||||
//when
|
||||
val result = Value.<String>where(false).and(false)
|
||||
val result = Value.<String>where(false).and(() -> false)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -109,7 +110,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereTrueOrTrueIsTrue() {
|
||||
//when
|
||||
val result = Value.<String>where(true).or(true)
|
||||
val result = Value.<String>where(true).or(() -> true)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -119,7 +120,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereTrueOrFalseIsTrue() {
|
||||
//when
|
||||
val result = Value.<String>where(true).or(false)
|
||||
val result = Value.<String>where(true).or(() -> false)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -129,7 +130,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereFalseOrTrueIsTrue() {
|
||||
//when
|
||||
val result = Value.<String>where(false).or(true)
|
||||
val result = Value.<String>where(false).or(() -> true)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -139,7 +140,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereFalseOrFalseIsFalse() {
|
||||
//when
|
||||
val result = Value.<String>where(false).or(false)
|
||||
val result = Value.<String>where(false).or(() -> false)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -167,7 +168,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereTrueAndNotTrueIsFalse() {
|
||||
//when
|
||||
val result = Value.<String>where(true).andNot(true)
|
||||
val result = Value.<String>where(true).andNot(() -> true)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -177,7 +178,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereTrueAndNotFalseIsTrue() {
|
||||
//when
|
||||
val result = Value.<String>where(true).andNot(false)
|
||||
val result = Value.<String>where(true).andNot(() -> false)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -187,7 +188,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereFalseAndNotTrueIsFalse() {
|
||||
//when
|
||||
val result = Value.<String>where(false).andNot(true)
|
||||
val result = Value.<String>where(false).andNot(() -> true)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -197,7 +198,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereFalseAndNotFalseIsFalse() {
|
||||
//when
|
||||
val result = Value.<String>where(false).andNot(false)
|
||||
val result = Value.<String>where(false).andNot(() -> false)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -207,7 +208,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereTrueOrNotTrueIsTrue() {
|
||||
//when
|
||||
val result = Value.<String>where(true).orNot(true)
|
||||
val result = Value.<String>where(true).orNot(() -> true)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -217,7 +218,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereTrueOrNotFalseIsTrue() {
|
||||
//when
|
||||
val result = Value.<String>where(true).orNot(false)
|
||||
val result = Value.<String>where(true).orNot(() -> false)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -227,7 +228,7 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereFalseOrNotTrueIsFalse() {
|
||||
//when
|
||||
val result = Value.<String>where(false).orNot(true)
|
||||
val result = Value.<String>where(false).orNot(() -> true)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
|
@ -237,10 +238,54 @@ public class ValueTest {
|
|||
@Test
|
||||
public void valueWhereFalseOrNotFalseIsTrue() {
|
||||
//when
|
||||
val result = Value.<String>where(false).orNot(false)
|
||||
val result = Value.<String>where(false).orNot(() -> false)
|
||||
.then(() -> TRUE)
|
||||
.otherwise(() -> FALSE);
|
||||
//then
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
box: maven:3.5.0-jdk-8
|
||||
|
||||
build:
|
||||
steps:
|
||||
- xenoterracide/maven:
|
||||
goals: clean install
|
Loading…
Reference in a new issue