From 11a63baa9f31de46f3bc5e0dd1f7cca57b683090 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 28 Feb 2018 15:00:00 +0000 Subject: [PATCH] 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 --- pom.xml | 11 +++- .../java/net/kemitix/mon/BeanBuilder.java | 61 +++++++++++++++++++ .../java/net/kemitix/mon/BeanBuilderTest.java | 42 +++++++++++++ 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/kemitix/mon/BeanBuilder.java create mode 100644 src/test/java/net/kemitix/mon/BeanBuilderTest.java diff --git a/pom.xml b/pom.xml index 9f0bb00..da5660b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,19 +7,23 @@ net.kemitix kemitix-parent - 5.0.3 + 5.1.0 mon 0.5.0-SNAPSHOT + 1.8 4.12 3.8.0 1.16.18 2.10 - 0.1.1 + 0.5.2 + + 2.20 net.kemitix.mon + 4.0.1 @@ -52,7 +56,8 @@ true - net.kemitix.tiles:all-tiles:${kemitix-tiles.version} + net.kemitix.tiles:all:${kemitix-tiles.version} + net.kemitix.checkstyle:tile:${kemitix-checkstyle.version} diff --git a/src/main/java/net/kemitix/mon/BeanBuilder.java b/src/main/java/net/kemitix/mon/BeanBuilder.java new file mode 100644 index 0000000..672a088 --- /dev/null +++ b/src/main/java/net/kemitix/mon/BeanBuilder.java @@ -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 The type of the bean being built + */ +@RequiredArgsConstructor +public class BeanBuilder { + + private final Supplier supplier; + + /** + * Create a BeanBuilder from the Supplier. + * + * @param constructor supplies a new instance of the bean + * @param the type of the bean being built + * @return a BeanBuilder instance + */ + public static BeanBuilder define(final Supplier 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 customiser) { + final T result = supplier.get(); + customiser.accept(result); + return result; + } +} diff --git a/src/test/java/net/kemitix/mon/BeanBuilderTest.java b/src/test/java/net/kemitix/mon/BeanBuilderTest.java new file mode 100644 index 0000000..a1b3ad3 --- /dev/null +++ b/src/test/java/net/kemitix/mon/BeanBuilderTest.java @@ -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 templateSupplier = () -> new DataObject("name"); + final Consumer 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; + + } +}