commit
5165edaedf
14 changed files with 608 additions and 5 deletions
2
.mvn/wrapper/maven-wrapper.properties
vendored
2
.mvn/wrapper/maven-wrapper.properties
vendored
|
@ -1 +1 @@
|
||||||
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip
|
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
0.5.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* Add `Before`, `After` and `Around` combinators
|
||||||
|
* Use `kemitix-maven-tiles`
|
||||||
|
* Add `BeanBuilder` experiment
|
||||||
|
* Upgrade `lombok` to 1.16.20
|
||||||
|
* Upgrade assertj to 3.9.1
|
||||||
|
|
||||||
0.4.0
|
0.4.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
46
Jenkinsfile.groovy
Normal file
46
Jenkinsfile.groovy
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
final String gitRepoUrl = 'git@github.com:kemitix/mon.git'
|
||||||
|
final String mvn = "mvn --batch-mode --update-snapshots"
|
||||||
|
|
||||||
|
pipeline {
|
||||||
|
agent any
|
||||||
|
stages {
|
||||||
|
stage('Prepare') {
|
||||||
|
steps {
|
||||||
|
git url: gitRepoUrl, branch: '**', credentialsId: 'github-kemitix'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Build') {
|
||||||
|
parallel {
|
||||||
|
stage('Java 8') {
|
||||||
|
steps {
|
||||||
|
withMaven(maven: 'maven 3.5.2', jdk: 'JDK 1.8') {
|
||||||
|
sh 'mvn clean install'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// requires maven-failsafe-plugin:2.21 when it is released
|
||||||
|
// stage('Java 9') {
|
||||||
|
// steps {
|
||||||
|
// withMaven(maven: 'maven 3.5.2', jdk: 'JDK 9') {
|
||||||
|
// sh 'mvn clean install'
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Reporting') {
|
||||||
|
steps {
|
||||||
|
junit '**/target/surefire-reports/*.xml'
|
||||||
|
archiveArtifacts '**/target/*.jar'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stage('Deploy') {
|
||||||
|
when { expression { (env.GIT_BRANCH == 'master') } }
|
||||||
|
steps {
|
||||||
|
withMaven(maven: 'maven 3.5.2', jdk: 'JDK 1.8') {
|
||||||
|
sh "${mvn} deploy --activate-profiles release -DskipTests=true"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
lombok.config
Normal file
1
lombok.config
Normal file
|
@ -0,0 +1 @@
|
||||||
|
lombok.addGeneratedAnnotation=false
|
40
pom.xml
40
pom.xml
|
@ -7,17 +7,32 @@
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>net.kemitix</groupId>
|
<groupId>net.kemitix</groupId>
|
||||||
<artifactId>kemitix-parent</artifactId>
|
<artifactId>kemitix-parent</artifactId>
|
||||||
<version>3.2.4</version>
|
<version>5.1.0</version>
|
||||||
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>mon</artifactId>
|
<artifactId>mon</artifactId>
|
||||||
<version>0.4.0</version>
|
<version>0.5.0</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
<assertj-core.version>3.8.0</assertj-core.version>
|
<assertj.version>3.9.1</assertj.version>
|
||||||
|
<lombok.version>1.16.20</lombok.version>
|
||||||
|
<tiles-maven-plugin.version>2.10</tiles-maven-plugin.version>
|
||||||
|
<kemitix-tiles.version>0.6.1</kemitix-tiles.version>
|
||||||
|
<maven-surefire-plugin.version>2.20.1</maven-surefire-plugin.version>
|
||||||
|
<maven-failsafe-plugin.version>2.20.1</maven-failsafe-plugin.version>
|
||||||
|
<digraph-dependency.basePackage>net.kemitix.mon</digraph-dependency.basePackage>
|
||||||
|
<kemitix-checkstyle.version>4.0.1</kemitix-checkstyle.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
@ -27,9 +42,26 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj-core.version}</version>
|
<version>${assertj.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>io.repaint.maven</groupId>
|
||||||
|
<artifactId>tiles-maven-plugin</artifactId>
|
||||||
|
<version>${tiles-maven-plugin.version}</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<tiles>
|
||||||
|
<tile>net.kemitix.tiles:all:${kemitix-tiles.version}</tile>
|
||||||
|
<tile>net.kemitix.checkstyle:tile:${kemitix-checkstyle.version}</tile>
|
||||||
|
</tiles>
|
||||||
|
</configuration>
|
||||||
|
</plugin><!-- tiles-maven-plugin -->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
61
src/main/java/net/kemitix/mon/BeanBuilder.java
Normal file
61
src/main/java/net/kemitix/mon/BeanBuilder.java
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
/**
|
||||||
|
* 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.mon;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper to create bean-style objects.
|
||||||
|
*
|
||||||
|
* @param <T> The type of the bean being built
|
||||||
|
*/
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class BeanBuilder<T> {
|
||||||
|
|
||||||
|
private final Supplier<T> supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a BeanBuilder from the Supplier.
|
||||||
|
*
|
||||||
|
* @param constructor supplies a new instance of the bean
|
||||||
|
* @param <T> the type of the bean being built
|
||||||
|
* @return a BeanBuilder instance
|
||||||
|
*/
|
||||||
|
public static <T> BeanBuilder<T> define(final Supplier<T> constructor) {
|
||||||
|
return new BeanBuilder<>(constructor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new bean and passes it to the customiser.
|
||||||
|
*
|
||||||
|
* @param customiser customises the template bean
|
||||||
|
* @return the final customised bean
|
||||||
|
*/
|
||||||
|
public T with(final Consumer<T> customiser) {
|
||||||
|
final T result = supplier.get();
|
||||||
|
customiser.accept(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
78
src/main/java/net/kemitix/mon/combinator/After.java
Normal file
78
src/main/java/net/kemitix/mon/combinator/After.java
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/**
|
||||||
|
* 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.mon.combinator;
|
||||||
|
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* After pattern combinator.
|
||||||
|
*
|
||||||
|
* <p>Original from http://boundsofjava.com/newsletter/003-introducing-combinators-part1</p>
|
||||||
|
*
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @author Federico Peralta Schaffner (fps@boundsofjava.com)
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface After<T, R> extends
|
||||||
|
Function<Function<T, R>,
|
||||||
|
Function<
|
||||||
|
BiConsumer<T, R>,
|
||||||
|
Function<T, R>>> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decorates a function with a Consumer that will be supplier with the argument before applying it to the function.
|
||||||
|
*
|
||||||
|
* @param function the function to apply the argument to and return the result value of
|
||||||
|
* @param after the bi-consumer that will receive the argument and the result of the function
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @return a partially applied Function that will take an argument and return the result of applying it to the
|
||||||
|
* function parameter
|
||||||
|
*/
|
||||||
|
static <T, R> Function<T, R> decorate(
|
||||||
|
final Function<T, R> function,
|
||||||
|
final BiConsumer<T, R> after
|
||||||
|
) {
|
||||||
|
return After.<T, R>create().apply(function)
|
||||||
|
.apply(after);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an After curried function.
|
||||||
|
*
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @return a curried function that will pass the argument and the result of the function to the supplied bi-consumer
|
||||||
|
*/
|
||||||
|
static <T, R> After<T, R> create() {
|
||||||
|
return function -> after -> argument -> {
|
||||||
|
final R result = function.apply(argument);
|
||||||
|
after.accept(argument, result);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
101
src/main/java/net/kemitix/mon/combinator/Around.java
Normal file
101
src/main/java/net/kemitix/mon/combinator/Around.java
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
/**
|
||||||
|
* 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.mon.combinator;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Around pattern combinator.
|
||||||
|
*
|
||||||
|
* <p>Original from http://boundsofjava.com/newsletter/003-introducing-combinators-part1</p>
|
||||||
|
*
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @author Federico Peralta Schaffner (fps@boundsofjava.com)
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Around<T, R> extends
|
||||||
|
Function<
|
||||||
|
Function<T, R>,
|
||||||
|
Function<
|
||||||
|
BiConsumer<Around.Executable<R>, T>,
|
||||||
|
Function<T, R>>> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decorates a function with an BiConsumer that will be supplier with an executable to perform the function, and the
|
||||||
|
* argument that will be applied when executed.
|
||||||
|
*
|
||||||
|
* @param function the function to apply the argument to and return the result value of
|
||||||
|
* @param around the bi-consumer that will supplied with the executable and the argument
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @return a partially applied Function that will take an argument, and the result of applying it to function
|
||||||
|
*/
|
||||||
|
static <T, R> Function<T, R> decorate(
|
||||||
|
final Function<T, R> function,
|
||||||
|
final BiConsumer<Executable<R>, T> around
|
||||||
|
) {
|
||||||
|
return Around.<T, R>create().apply(function)
|
||||||
|
.apply(around);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an Around curried function.
|
||||||
|
*
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @return a curried function that will execute the around function, passing an executable and the invocations
|
||||||
|
* argument. The around function must {@code execute()} the executable and may capture the result.
|
||||||
|
*/
|
||||||
|
static <T, R> Around<T, R> create() {
|
||||||
|
return function -> around -> argument -> {
|
||||||
|
final AtomicReference<R> result = new AtomicReference<>();
|
||||||
|
final Executable<R> callback = () -> {
|
||||||
|
result.set(function.apply(argument));
|
||||||
|
return result.get();
|
||||||
|
};
|
||||||
|
around.accept(callback, argument);
|
||||||
|
return result.get();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The executable that will be supplied to the around function to trigger the surrounded function.
|
||||||
|
*
|
||||||
|
* @param <R> the return type of the function
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
interface Executable<R> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the function.
|
||||||
|
*
|
||||||
|
* @return the result of applying the function
|
||||||
|
*/
|
||||||
|
R execute();
|
||||||
|
}
|
||||||
|
}
|
78
src/main/java/net/kemitix/mon/combinator/Before.java
Normal file
78
src/main/java/net/kemitix/mon/combinator/Before.java
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/**
|
||||||
|
* 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.mon.combinator;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Before pattern combinator.
|
||||||
|
*
|
||||||
|
* <p>Original from http://boundsofjava.com/newsletter/003-introducing-combinators-part1</p>
|
||||||
|
*
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @author Federico Peralta Schaffner (fps@boundsofjava.com)
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Before<T, R> extends
|
||||||
|
Function<
|
||||||
|
Consumer<T>,
|
||||||
|
Function<
|
||||||
|
Function<T, R>,
|
||||||
|
Function<T, R>>> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decorates a function with a Consumer that will be supplied with the argument before applying it to the function.
|
||||||
|
*
|
||||||
|
* @param before the consumer that will receive the argument before the function
|
||||||
|
* @param function the function to apply the argument to and return the result value of
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @return a partially applied Function that will take an argument and return the result of applying it to the
|
||||||
|
* function parameter
|
||||||
|
*/
|
||||||
|
static <T, R> Function<T, R> decorate(
|
||||||
|
final Consumer<T> before,
|
||||||
|
final Function<T, R> function
|
||||||
|
) {
|
||||||
|
return Before.<T, R>create().apply(before)
|
||||||
|
.apply(function);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Before curried function.
|
||||||
|
*
|
||||||
|
* @param <T> the argument type
|
||||||
|
* @param <R> the result type
|
||||||
|
*
|
||||||
|
* @return a curried function that will pass the argument to before applying the supplied function
|
||||||
|
*/
|
||||||
|
static <T, R> Before<T, R> create() {
|
||||||
|
return before -> function -> argument -> {
|
||||||
|
before.accept(argument);
|
||||||
|
return function.apply(argument);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
22
src/main/java/net/kemitix/mon/combinator/package-info.java
Normal file
22
src/main/java/net/kemitix/mon/combinator/package-info.java
Normal 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.mon.combinator;
|
42
src/test/java/net/kemitix/mon/BeanBuilderTest.java
Normal file
42
src/test/java/net/kemitix/mon/BeanBuilderTest.java
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package net.kemitix.mon;
|
||||||
|
|
||||||
|
import static net.kemitix.mon.BeanBuilder.define;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class BeanBuilderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canCreateAndSetupObject() {
|
||||||
|
//given
|
||||||
|
final Supplier<DataObject> templateSupplier = () -> new DataObject("name");
|
||||||
|
final Consumer<DataObject> propertySetter = data -> data.setValue("value");
|
||||||
|
|
||||||
|
//when
|
||||||
|
final DataObject result = define(templateSupplier)
|
||||||
|
.with(propertySetter);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertThat(result)
|
||||||
|
.returns("name", DataObject::getName)
|
||||||
|
.returns("value", DataObject::getValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
private class DataObject {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
44
src/test/java/net/kemitix/mon/combinator/AfterTest.java
Normal file
44
src/test/java/net/kemitix/mon/combinator/AfterTest.java
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package net.kemitix.mon.combinator;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class AfterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canCreateAfterCombinator() {
|
||||||
|
//given
|
||||||
|
final List<String> events = new ArrayList<>();
|
||||||
|
final Function<Integer, Integer> squareDecorated =
|
||||||
|
After.decorate(
|
||||||
|
argument -> function(argument, events),
|
||||||
|
(argument, result) -> after(argument, result, events)
|
||||||
|
);
|
||||||
|
//when
|
||||||
|
final Integer result = squareDecorated.apply(2);
|
||||||
|
//then
|
||||||
|
assertThat(result).isEqualTo(4);
|
||||||
|
assertThat(events).containsExactly("function", "after 2 -> 4");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void after(
|
||||||
|
final Integer argument,
|
||||||
|
final Integer result,
|
||||||
|
final List<String> events
|
||||||
|
) {
|
||||||
|
events.add(String.format("after %d -> %d", argument, result));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer function(
|
||||||
|
final Integer argument,
|
||||||
|
final List<String> events
|
||||||
|
) {
|
||||||
|
events.add("function");
|
||||||
|
return argument * argument;
|
||||||
|
}
|
||||||
|
}
|
46
src/test/java/net/kemitix/mon/combinator/AroundTest.java
Normal file
46
src/test/java/net/kemitix/mon/combinator/AroundTest.java
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package net.kemitix.mon.combinator;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class AroundTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canCreateAnAroundCombinator() {
|
||||||
|
//given
|
||||||
|
final List<String> events = new ArrayList<>();
|
||||||
|
final Function<Integer, Integer> squareDecorated =
|
||||||
|
Around.decorate(
|
||||||
|
argument -> function(argument, events),
|
||||||
|
(executable, argument) -> around(executable, argument, events)
|
||||||
|
);
|
||||||
|
//when
|
||||||
|
final Integer result = squareDecorated.apply(2);
|
||||||
|
//then
|
||||||
|
assertThat(result).isEqualTo(4);
|
||||||
|
assertThat(events).containsExactly("around before 2", "function", "around after 4");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void around(
|
||||||
|
final Around.Executable<Integer> executable,
|
||||||
|
final Integer argument,
|
||||||
|
final List<String> events
|
||||||
|
) {
|
||||||
|
events.add("around before " + argument);
|
||||||
|
final Integer result = executable.execute();
|
||||||
|
events.add("around after " + result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer function(
|
||||||
|
final Integer argument,
|
||||||
|
final List<String> events
|
||||||
|
) {
|
||||||
|
events.add("function");
|
||||||
|
return argument * argument;
|
||||||
|
}
|
||||||
|
}
|
43
src/test/java/net/kemitix/mon/combinator/BeforeTest.java
Normal file
43
src/test/java/net/kemitix/mon/combinator/BeforeTest.java
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package net.kemitix.mon.combinator;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class BeforeTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void canCreateBeforeCombinator() {
|
||||||
|
//given
|
||||||
|
final List<String> events = new ArrayList<>();
|
||||||
|
final Function<Integer, Integer> squareDecorated =
|
||||||
|
Before.decorate(
|
||||||
|
argument -> before(argument, events),
|
||||||
|
argument -> function(argument, events)
|
||||||
|
);
|
||||||
|
//when
|
||||||
|
final Integer result = squareDecorated.apply(2);
|
||||||
|
//then
|
||||||
|
assertThat(result).isEqualTo(4);
|
||||||
|
assertThat(events).containsExactly("before 2", "function");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void before(
|
||||||
|
final Integer argument,
|
||||||
|
final List<String> events
|
||||||
|
) {
|
||||||
|
events.add("before " + argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer function(
|
||||||
|
final Integer argument,
|
||||||
|
final List<String> events
|
||||||
|
) {
|
||||||
|
events.add("function");
|
||||||
|
return argument * argument;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue