BeanBuilder (#18)
* Add BeanBuilder * Upgrade kemitix-parent to 5.1.0 * Add kemitix-checkstyle:tile:4.0.1 * Upgrade kemitix-maven-tiles to 0.5.2 * Build with java 1.8 * Downgrade surefire to 2.20 from the broken 2.02.1 in kemitix-maven-tiles * Add javadoc and rename parameters
This commit is contained in:
parent
5d111f3077
commit
11a63baa9f
3 changed files with 111 additions and 3 deletions
11
pom.xml
11
pom.xml
|
@ -7,19 +7,23 @@
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>net.kemitix</groupId>
|
<groupId>net.kemitix</groupId>
|
||||||
<artifactId>kemitix-parent</artifactId>
|
<artifactId>kemitix-parent</artifactId>
|
||||||
<version>5.0.3</version>
|
<version>5.1.0</version>
|
||||||
<relativePath/>
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>mon</artifactId>
|
<artifactId>mon</artifactId>
|
||||||
<version>0.5.0-SNAPSHOT</version>
|
<version>0.5.0-SNAPSHOT</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-core.version>3.8.0</assertj-core.version>
|
||||||
<lombok.version>1.16.18</lombok.version>
|
<lombok.version>1.16.18</lombok.version>
|
||||||
<tiles-maven-plugin.version>2.10</tiles-maven-plugin.version>
|
<tiles-maven-plugin.version>2.10</tiles-maven-plugin.version>
|
||||||
<kemitix-tiles.version>0.1.1</kemitix-tiles.version>
|
<kemitix-tiles.version>0.5.2</kemitix-tiles.version>
|
||||||
|
<!-- kemitix-tiles provides surefire 2.20.1 which is broken - downgrade surefire to 2.20 -->
|
||||||
|
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
|
||||||
<digraph-dependency.basePackage>net.kemitix.mon</digraph-dependency.basePackage>
|
<digraph-dependency.basePackage>net.kemitix.mon</digraph-dependency.basePackage>
|
||||||
|
<kemitix-checkstyle.version>4.0.1</kemitix-checkstyle.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -52,7 +56,8 @@
|
||||||
<extensions>true</extensions>
|
<extensions>true</extensions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<tiles>
|
<tiles>
|
||||||
<tile>net.kemitix.tiles:all-tiles:${kemitix-tiles.version}</tile>
|
<tile>net.kemitix.tiles:all:${kemitix-tiles.version}</tile>
|
||||||
|
<tile>net.kemitix.checkstyle:tile:${kemitix-checkstyle.version}</tile>
|
||||||
</tiles>
|
</tiles>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin><!-- tiles-maven-plugin -->
|
</plugin><!-- tiles-maven-plugin -->
|
||||||
|
|
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;
|
||||||
|
}
|
||||||
|
}
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue