Move BeanBuilder to experimental package and allow multiple with() calls
This commit is contained in:
parent
2f5b01f460
commit
3e80dded1d
3 changed files with 52 additions and 18 deletions
|
@ -19,7 +19,7 @@
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* 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;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@ -34,17 +34,17 @@ import java.util.function.Supplier;
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class BeanBuilder<T> {
|
public class BeanBuilder<T> {
|
||||||
|
|
||||||
private final Supplier<T> supplier;
|
private final T bean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a BeanBuilder from the Supplier.
|
* Create a BeanBuilder from the Supplier.
|
||||||
*
|
*
|
||||||
* @param constructor supplies a new instance of the bean
|
* @param constructor supplies a new instance of the bean
|
||||||
* @param <T> the type of the bean being built
|
* @param <T> the type of the bean being built
|
||||||
* @return a BeanBuilder instance
|
* @return a BeanBuilder instance
|
||||||
*/
|
*/
|
||||||
public static <T> BeanBuilder<T> define(final Supplier<T> constructor) {
|
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
|
* @param customiser customises the template bean
|
||||||
* @return the final customised bean
|
* @return the final customised bean
|
||||||
*/
|
*/
|
||||||
public T with(final Consumer<T> customiser) {
|
public BeanBuilder<T> with(final Consumer<T> customiser) {
|
||||||
final T result = supplier.get();
|
customiser.accept(bean);
|
||||||
customiser.accept(result);
|
return this;
|
||||||
return result;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finished building the bean and returns it.
|
||||||
|
*
|
||||||
|
* @return the bean
|
||||||
|
*/
|
||||||
|
public T build() {
|
||||||
|
return bean;
|
||||||
}
|
}
|
||||||
}
|
}
|
22
src/main/java/net/kemitix/mon/experimental/package-info.java
Normal file
22
src/main/java/net/kemitix/mon/experimental/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.experimental;
|
|
@ -1,28 +1,29 @@
|
||||||
package net.kemitix.mon;
|
package net.kemitix.mon;
|
||||||
|
|
||||||
import static net.kemitix.mon.BeanBuilder.define;
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import net.kemitix.mon.experimental.BeanBuilder;
|
||||||
|
import org.assertj.core.api.WithAssertions;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class BeanBuilderTest {
|
public class BeanBuilderTest implements WithAssertions {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canCreateAndSetupObject() {
|
public void canCreateAndSetupObject() {
|
||||||
//given
|
//given
|
||||||
final Supplier<DataObject> templateSupplier = () -> new DataObject("name");
|
final Supplier<DataObject> template = () -> new DataObject("name");
|
||||||
final Consumer<DataObject> propertySetter = data -> data.setValue("value");
|
final Consumer<DataObject> value = data -> data.setValue("value");
|
||||||
|
final Consumer<DataObject> age = data -> data.setAge(42);
|
||||||
//when
|
//when
|
||||||
final DataObject result = define(templateSupplier)
|
final DataObject result = BeanBuilder
|
||||||
.with(propertySetter);
|
.define(template)
|
||||||
|
.with(value)
|
||||||
|
.with(age)
|
||||||
|
.build();
|
||||||
//then
|
//then
|
||||||
assertThat(result)
|
assertThat(result)
|
||||||
.returns("name", DataObject::getName)
|
.returns("name", DataObject::getName)
|
||||||
|
@ -35,6 +36,9 @@ public class BeanBuilderTest {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
private int age;
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
private String value;
|
private String value;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue