Move BeanBuilder to experimental package and allow multiple with() calls

This commit is contained in:
Paul Campbell 2018-06-26 07:28:34 +01:00
parent 2f5b01f460
commit 3e80dded1d
3 changed files with 52 additions and 18 deletions

View file

@ -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,7 +34,7 @@ import java.util.function.Supplier;
@RequiredArgsConstructor
public class BeanBuilder<T> {
private final Supplier<T> supplier;
private final T bean;
/**
* Create a BeanBuilder from the Supplier.
@ -44,7 +44,7 @@ public class BeanBuilder<T> {
* @return a BeanBuilder instance
*/
public static <T> BeanBuilder<T> define(final Supplier<T> constructor) {
return new BeanBuilder<>(constructor);
return new BeanBuilder<>(constructor.get());
}
/**
@ -53,9 +53,17 @@ public class BeanBuilder<T> {
* @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;
public BeanBuilder<T> with(final Consumer<T> customiser) {
customiser.accept(bean);
return this;
}
/**
* Finished building the bean and returns it.
*
* @return the bean
*/
public T build() {
return bean;
}
}

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.mon.experimental;

View file

@ -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<DataObject> templateSupplier = () -> new DataObject("name");
final Consumer<DataObject> propertySetter = data -> data.setValue("value");
final Supplier<DataObject> template = () -> new DataObject("name");
final Consumer<DataObject> value = data -> data.setValue("value");
final Consumer<DataObject> 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;