From 3e80dded1d231527eb239c7badf66fcca7a10bdf Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 26 Jun 2018 07:28:34 +0100 Subject: [PATCH] Move BeanBuilder to experimental package and allow multiple with() calls --- .../mon/{ => experimental}/BeanBuilder.java | 24 ++++++++++++------- .../mon/experimental/package-info.java | 22 +++++++++++++++++ .../java/net/kemitix/mon/BeanBuilderTest.java | 24 +++++++++++-------- 3 files changed, 52 insertions(+), 18 deletions(-) rename src/main/java/net/kemitix/mon/{ => experimental}/BeanBuilder.java (80%) create mode 100644 src/main/java/net/kemitix/mon/experimental/package-info.java diff --git a/src/main/java/net/kemitix/mon/BeanBuilder.java b/src/main/java/net/kemitix/mon/experimental/BeanBuilder.java similarity index 80% rename from src/main/java/net/kemitix/mon/BeanBuilder.java rename to src/main/java/net/kemitix/mon/experimental/BeanBuilder.java index 672a088..f7c8b62 100644 --- a/src/main/java/net/kemitix/mon/BeanBuilder.java +++ b/src/main/java/net/kemitix/mon/experimental/BeanBuilder.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package net.kemitix.mon; +package net.kemitix.mon.experimental; import lombok.RequiredArgsConstructor; @@ -34,17 +34,17 @@ import java.util.function.Supplier; @RequiredArgsConstructor public class BeanBuilder { - private final Supplier supplier; + private final T bean; /** * Create a BeanBuilder from the Supplier. * * @param constructor supplies a new instance of the bean - * @param the type of the bean being built + * @param the type of the bean being built * @return a BeanBuilder instance */ public static BeanBuilder define(final Supplier constructor) { - return new BeanBuilder<>(constructor); + return new BeanBuilder<>(constructor.get()); } /** @@ -53,9 +53,17 @@ public class BeanBuilder { * @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; + public BeanBuilder with(final Consumer customiser) { + customiser.accept(bean); + return this; + } + + /** + * Finished building the bean and returns it. + * + * @return the bean + */ + public T build() { + return bean; } } diff --git a/src/main/java/net/kemitix/mon/experimental/package-info.java b/src/main/java/net/kemitix/mon/experimental/package-info.java new file mode 100644 index 0000000..b172415 --- /dev/null +++ b/src/main/java/net/kemitix/mon/experimental/package-info.java @@ -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.experimental; diff --git a/src/test/java/net/kemitix/mon/BeanBuilderTest.java b/src/test/java/net/kemitix/mon/BeanBuilderTest.java index a1b3ad3..d36c69e 100644 --- a/src/test/java/net/kemitix/mon/BeanBuilderTest.java +++ b/src/test/java/net/kemitix/mon/BeanBuilderTest.java @@ -1,28 +1,29 @@ 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 net.kemitix.mon.experimental.BeanBuilder; +import org.assertj.core.api.WithAssertions; import org.junit.Test; import java.util.function.Consumer; import java.util.function.Supplier; -public class BeanBuilderTest { +public class BeanBuilderTest implements WithAssertions { @Test public void canCreateAndSetupObject() { //given - final Supplier templateSupplier = () -> new DataObject("name"); - final Consumer propertySetter = data -> data.setValue("value"); - + final Supplier template = () -> new DataObject("name"); + final Consumer value = data -> data.setValue("value"); + final Consumer age = data -> data.setAge(42); //when - final DataObject result = define(templateSupplier) - .with(propertySetter); - + final DataObject result = BeanBuilder + .define(template) + .with(value) + .with(age) + .build(); //then assertThat(result) .returns("name", DataObject::getName) @@ -35,6 +36,9 @@ public class BeanBuilderTest { private final String name; + @Setter + private int age; + @Setter private String value;