1
0

FluentBuilder<D> as interface with default method

This commit is contained in:
Michael Hoennig
2019-04-29 12:19:34 +02:00
parent 1916347490
commit cc0857e33b
5 changed files with 52 additions and 20 deletions

View File

@ -14,7 +14,7 @@ import java.util.Objects;
/**
* A DTO for the Customer entity.
*/
public class CustomerDTO extends FluentBuilder<CustomerDTO> implements AccessMappings {
public class CustomerDTO implements AccessMappings, FluentBuilder<CustomerDTO> {
@SelfId(resolver = CustomerService.class)
@AccessFor(read = Role.ANY_CUSTOMER_USER)

View File

@ -2,10 +2,41 @@ package org.hostsharing.hsadminng.service.dto;
import java.util.function.Consumer;
public class FluentBuilder<T> {
/**
* Just 'implement' this interface in your class to get a pseudo fluent builder, no more code needed.
*
* @param <T> class to be build (same as to which the interface was added)
*/
public interface FluentBuilder<T> {
/**
* Allows statements on the target instance possible as expression.
*
* This allows creating nested object structures, e.g. for test data
* in a much more readable way.
*
* <h3>Example</h3>
* {code
* // adding a fluent builder to your class
* class YourClass implements FluentBuilder<YourClass> {
* public int someField;
* public String anotherField;
* // ...
* }
*
* // using the fluent builder somewhere else
* someMethod( new YourClass().with( it -> {
* it.someField = 5;
* it.anotherField = "Hello";
* }));
* }
*
* @param builderFunction statements to apply to 'this'
*
* @return the instance on which 'with(...)' was executed.
*/
@SuppressWarnings("unchecked")
public T with(
default T with(
Consumer<T> builderFunction) {
builderFunction.accept((T) this);
return (T) this;

View File

@ -8,14 +8,13 @@ import org.springframework.context.ApplicationContext;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Objects;
/**
* A DTO for the Membership entity.
*/
public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Serializable, AccessMappings {
public class MembershipDTO implements AccessMappings, FluentBuilder<MembershipDTO> {
@SelfId(resolver = MembershipService.class)
@AccessFor(read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})