Initial commit

This commit is contained in:
Paul Campbell 2017-04-21 10:49:24 +01:00
commit 0fbb4f62b6
6 changed files with 399 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
target
.idea
*.iml

20
LICENSE.txt Normal file
View file

@ -0,0 +1,20 @@
/**
* 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.
*/

62
pom.xml Normal file
View file

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>2.3.0</kemitix-checkstyle-ruleset.version>
<kemitix-checkstyle-ruleset.level>5-complexity</kemitix-checkstyle-ruleset.level>
<junit.version>4.12</junit.version>
<assertj.version>3.6.2</assertj.version>
</properties>
<parent>
<groupId>net.kemitix</groupId>
<artifactId>kemitix-parent</artifactId>
<version>2.7.0</version>
<relativePath/>
</parent>
<artifactId>conditional</artifactId>
<version>0.1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-maven-plugin</artifactId>
<version>${kemitix-checkstyle-ruleset.version}</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>${kemitix-checkstyle-ruleset.level}</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>sevntu-maven</id>
<name>sevntu-maven</name>
<url>http://sevntu-checkstyle.github.io/sevntu.checkstyle/maven2</url>
</pluginRepository>
</pluginRepositories>
</project>

View file

@ -0,0 +1,139 @@
/**
* 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;
/**
* If-then-else in a functional-style.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
public interface Condition {
/**
* Create a new {@code Condition}.
*
* @param clause the condition to test
*
* @return the Condition
*/
static Condition where(final boolean clause) {
if (clause) {
return new TrueCondition();
}
return new FalseCondition();
}
/**
* Logically AND combine the current {@code Condition} with the clause.
*
* @param clause the condition to test
*
* @return the Condition
*/
Condition and(boolean clause);
/**
* Logically OR combine the current {@code Condition} with the clause.
*
* @param clause the condition to test
*
* @return the Condition
*/
Condition or(boolean clause);
/**
* Perform this response if the {@code Condition} is {@code true}.
*
* @param response the response to perform
*
* @return the Condition
*/
Condition then(Runnable response);
/**
* Perform this response if the {@code Condition} is {@code false}.
*
* @param response the response to perform
*/
void otherwise(Runnable response);
/**
* A {@code Condition} that has evaluated to {@code true}.
*/
class TrueCondition implements Condition {
@Override
public Condition and(final boolean clause) {
if (clause) {
return this;
}
return new FalseCondition();
}
@Override
public Condition or(final boolean secondClause) {
return this;
}
@Override
public Condition then(final Runnable response) {
response.run();
return this;
}
@Override
public void otherwise(final Runnable response) {
}
}
/**
* A {@code Condition} that has evaluated to {@code false}.
*/
class FalseCondition implements Condition {
@Override
public Condition and(final boolean clause) {
return this;
}
@Override
public Condition or(final boolean secondClause) {
if (secondClause) {
return new TrueCondition();
}
return this;
}
@Override
public Condition then(final Runnable response) {
return this;
}
@Override
public void otherwise(final Runnable response) {
response.run();
}
}
}

View file

@ -0,0 +1,22 @@
/**
* 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;

View file

@ -0,0 +1,153 @@
package net.kemitix.conditional;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Paul Campbell (pcampbell@kemitix.net).
*/
public class ConditionalTest {
private Runnable thenResponse;
private Runnable otherwiseResponse;
private boolean thenFlag;
private boolean otherwiseFlag;
@Before
public void setUp() {
thenResponse = () -> thenFlag = true;
otherwiseResponse = () -> otherwiseFlag = true;
}
@Test
public void whereTrueThenRuns() {
//when
when(true);
//then
thenTheThenResponseRuns();
}
@Test
public void whereFalseOtherwiseRuns() {
//when
when(false);
//then
thenTheOtherwiseResponseRuns();
}
@Test
public void whereTrueAndTrueThenRuns() {
//when
when(true, true);
//then
thenTheThenResponseRuns();
}
@Test
public void whereTrueAndFalseThenOtherwiseRuns() {
//when
when(true, false);
//then
thenTheOtherwiseResponseRuns();
}
@Test
public void whereFalseAndTrueThenOtherwiseRuns() {
//when
when(false, true);
//then
thenTheOtherwiseResponseRuns();
}
@Test
public void whereFalseAndFalseThenOtherwiseRuns() {
//when
when(false, false);
//then
thenTheOtherwiseResponseRuns();
}
@Test
public void whereTrueThenDoSomethingAndThenDoSomethingElse() {
//when
Condition.where(true)
.then(thenResponse)
.and(true)
.then(otherwiseResponse);
//then
thenBothResponsesRun();
}
@Test
public void whereTrueOrTrueThenDoSomething() {
//when
whenOr(true, true);
//then
thenTheThenResponseRuns();
}
@Test
public void whereTrueOrFalseThenDoSomething() {
//when
whenOr(true, false);
//then
thenTheThenResponseRuns();
}
@Test
public void whereFalseOrTrueThenDoSomething() {
//when
whenOr(false, true);
//then
thenTheThenResponseRuns();
}
@Test
public void whereFalseOrFalseThenDoSomethingElse() {
//when
whenOr(false, false);
//then
thenTheOtherwiseResponseRuns();
}
private void whenOr(final boolean firstClause, final boolean secondClause) {
Condition.where(firstClause)
.or(secondClause)
.then(thenResponse)
.otherwise(otherwiseResponse);
}
private void when(final boolean clause) {
Condition.where(clause)
.then(thenResponse)
.otherwise(otherwiseResponse);
}
private void thenBothResponsesRun() {
assertThat(thenFlag).isTrue();
assertThat(otherwiseFlag).isTrue();
}
private void thenTheThenResponseRuns() {
assertThat(thenFlag).isTrue();
assertThat(otherwiseFlag).isFalse();
}
private void thenTheOtherwiseResponseRuns() {
assertThat(thenFlag).isFalse();
assertThat(otherwiseFlag).isTrue();
}
private void when(final boolean firstClause, final boolean secondClause) {
Condition.where(firstClause)
.and(secondClause)
.then(thenResponse)
.otherwise(otherwiseResponse);
}
}