BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder

This commit is contained in:
Chirag Dewan 2019-07-24 21:32:42 +05:30
parent b32a91a8ff
commit 97e38d338f
4 changed files with 23 additions and 140 deletions

View File

@ -41,6 +41,8 @@ public interface Employee {
Map<String, Long> getAssetsSerialIdMapping();
Optional<Double> getSalaryInUSD();
class Builder extends Employee_Builder {

View File

@ -2,61 +2,16 @@ package com.baeldung.freebuilder.builder.classic;
public class Employee {
private String name;
private int age;
private String department;
private String role;
private String supervisorName;
private String designation;
private String email;
private long phoneNumber;
private boolean isPermanent;
private Address address;
private final String name;
private final int age;
private final String department;
private Employee() {
}
private void setName(String name) {
private Employee(String name, int age, String department) {
this.name = name;
}
private void setAge(int age) {
this.age = age;
}
private void setDepartment(String department) {
this.department = department;
}
private void setRole(String role) {
this.role = role;
}
private void setSupervisorName(String supervisorName) {
this.supervisorName = supervisorName;
}
private void setDesignation(String designation) {
this.designation = designation;
}
private void setEmail(String email) {
this.email = email;
}
private void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
private void setPermanent(boolean permanent) {
isPermanent = permanent;
}
private void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
@ -69,46 +24,11 @@ public class Employee {
return department;
}
public String getRole() {
return role;
}
public String getSupervisorName() {
return supervisorName;
}
public String getDesignation() {
return designation;
}
public String getEmail() {
return email;
}
public long getPhoneNumber() {
return phoneNumber;
}
public boolean isPermanent() {
return isPermanent;
}
public Address getAddress() {
return address;
}
public static class Builder {
private String name;
private int age;
private String department;
private String role;
private String supervisorName;
private String designation;
private String email;
private long phoneNumber;
private boolean isPermanent;
private Address address;
public Builder setName(String name) {
this.name = name;
@ -125,56 +45,8 @@ public class Employee {
return this;
}
public Builder setRole(String role) {
this.role = role;
return this;
}
public Builder setSupervisorName(String supervisorName) {
this.supervisorName = supervisorName;
return this;
}
public Builder setDesignation(String designation) {
this.designation = designation;
return this;
}
public Builder setEmail(String email) {
this.email = email;
return this;
}
public Builder setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
return this;
}
public Builder setPermanent(boolean permanent) {
isPermanent = permanent;
return this;
}
public Builder setAddress(Address address) {
this.address = address;
return this;
}
public Employee build() {
Employee employee = new Employee();
employee.setName(this.name);
employee.setAge(this.age);
employee.setDepartment(this.department);
employee.setAddress(this.address);
employee.setDesignation(this.designation);
employee.setEmail(this.email);
employee.setPermanent(this.isPermanent);
employee.setName(this.name);
employee.setSupervisorName(this.supervisorName);
employee.setPhoneNumber(this.phoneNumber);
return employee;
return new Employee(name, age, department);
}
}

View File

@ -15,6 +15,8 @@ public class EmployeeBuilderUnitTest {
private static final int PIN_CODE = 223344;
public static final String CITY_NAME = "New York";
public static final int INPUT_SALARY_EUROS = 10000;
public static final double EUROS_TO_USD_RATIO = 0.6;
@Test
public void whenBuildEmployeeWithAddress_thenReturnEmployeeWithValidAddress() {
@ -33,15 +35,27 @@ public class EmployeeBuilderUnitTest {
}
@Test
public void whenMapPincodeInAddress_thenReturnEmployeeWithValidAddressPincode() {
public void whenMapSalary_thenReturnEmployeeWithSalaryInUSD() {
// when
Address.Builder addressBuilder = new Address.Builder();
Address address = addressBuilder.setCity(CITY_NAME).setPinCode(PIN_CODE).build();
long salaryInEuros = INPUT_SALARY_EUROS;
Employee.Builder builder = new Employee.Builder();
Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setPermanent(true).setRole("developer").setAddress(address).build();
Employee employee = builder
.setName("baeldung")
.setAge(10)
.setDesignation("author")
.setEmail("abc@xyz.com")
.setSupervisorName("Admin")
.setPhoneNumber(4445566)
.setPermanent(true)
.setRole("developer")
.setAddress(address)
.mapSalaryInUSD(sal -> salaryInEuros * EUROS_TO_USD_RATIO)
.build();
// then
assertTrue(employee.getAddress().getPinCode().get() == PIN_CODE);

View File

@ -18,11 +18,6 @@ class EmployeeBuilderUnitTest {
.setName(NAME)
.setAge(12)
.setDepartment("Builder Pattern")
.setDesignation("Author")
.setEmail("abc@xyz.com")
.setPermanent(true)
.setSupervisorName("Admin")
.setPhoneNumber(4445566)
.build();
//then