From bc3ed2671687b829fbdac4d1331aee1bf3811ba0 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Tue, 16 Jul 2019 17:20:12 +0530 Subject: [PATCH 01/10] BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder --- libraries-2/pom.xml | 11 +- .../com/baeldung/freebuilder/Employee.java | 65 +++++++ .../freebuilder/builder/classic/Address.java | 25 +++ .../freebuilder/builder/classic/Employee.java | 181 ++++++++++++++++++ .../freebuilder/EmployeeBuilderUnitTest.java | 150 +++++++++++++++ .../classic/EmployeeBuilderUnitTest.java | 32 ++++ pom.xml | 2 +- 7 files changed, 463 insertions(+), 3 deletions(-) create mode 100644 libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java create mode 100644 libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Address.java create mode 100644 libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java create mode 100644 libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java create mode 100644 libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java diff --git a/libraries-2/pom.xml b/libraries-2/pom.xml index ff73888b69..c6be8df38d 100644 --- a/libraries-2/pom.xml +++ b/libraries-2/pom.xml @@ -142,9 +142,15 @@ org.apache.mesos mesos ${mesos.library.version} + + + org.inferred + freebuilder + ${freebuilder.version} + true - + 3.0.7 3.6.2 @@ -165,5 +171,6 @@ 2.8.5 3.14.2 4.1.2 - + 2.4.1 + diff --git a/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java b/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java new file mode 100644 index 0000000000..08470cc5cd --- /dev/null +++ b/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java @@ -0,0 +1,65 @@ +package com.baeldung.freebuilder; + +import com.baeldung.freebuilder.builder.classic.Address; +import org.inferred.freebuilder.FreeBuilder; + +import javax.annotation.Nullable; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.UnaryOperator; + +@FreeBuilder +public interface Employee { + + String getName(); + + int getAge(); + + String getDepartment(); + + String getRole(); + + String getSupervisorName(); + + String getDesignation(); + + String getEmail(); + + long getPhoneNumber(); + + Optional getPermanent(); + + Optional getDateOfJoining(); + + @Nullable + String getCurrentProject(); + + Address getAddress(); + + List getAccessTokens(); + + Map getAssetsSerialIdMapping(); + + + class Builder extends Employee_Builder { + + public Builder() { + // setting default value for department + setDepartment("Builder Pattern"); + } + + @Override + public Builder setEmail(String email) { + if (checkValidEmail(email)) + return super.setEmail(email); + else + throw new IllegalArgumentException("Invalid email"); + + } + + private boolean checkValidEmail(String email) { + return email.contains("@"); + } + } +} diff --git a/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Address.java b/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Address.java new file mode 100644 index 0000000000..8e53a9f155 --- /dev/null +++ b/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Address.java @@ -0,0 +1,25 @@ +package com.baeldung.freebuilder.builder.classic; + +import org.inferred.freebuilder.FreeBuilder; + +import java.util.Optional; + +@FreeBuilder +public interface Address { + + Optional getAddressLine1(); + + Optional getAddressLine2(); + + Optional getAddressLine3(); + + String getCity(); + + Optional getState(); + + Optional getPinCode(); + + class Builder extends Address_Builder { + + } +} \ No newline at end of file diff --git a/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java b/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java new file mode 100644 index 0000000000..bd9c8b0a17 --- /dev/null +++ b/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java @@ -0,0 +1,181 @@ +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 Employee() { + + } + + private void setName(String name) { + 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; + } + + public int getAge() { + return age; + } + + public String getDepartment() { + 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; + return this; + } + + public Builder setAge(int age) { + this.age = age; + return this; + } + + public Builder setDepartment(String department) { + this.department = department; + 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; + + } + } + +} diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java new file mode 100644 index 0000000000..e3a9e49633 --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java @@ -0,0 +1,150 @@ +package com.baeldung.freebuilder; + +import com.baeldung.freebuilder.builder.classic.Address; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class EmployeeBuilderUnitTest { + + private static final int PIN_CODE = 223344; + public static final String CITY_NAME = "New York"; + + @Test + public void whenBuildEmployeeWithAddress_thenReturnEmployeeWithValidAddress() { + + // when + Address.Builder addressBuilder = new Address.Builder(); + Address address = addressBuilder.setCity(CITY_NAME).build(); + + 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(); + + // then + assertTrue(employee.getAddress().getCity().equalsIgnoreCase(CITY_NAME)); + + } + + @Test + public void whenMapPincodeInAddress_thenReturnEmployeeWithValidAddressPincode() { + + // when + Address.Builder addressBuilder = new Address.Builder(); + Address address = addressBuilder.setCity(CITY_NAME).setPinCode(PIN_CODE).build(); + + 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(); + + // then + assertTrue(employee.getAddress().getPinCode().get() == PIN_CODE); + + } + + @Test + public void whenOptionalFields_thenReturnEmployeeWithEmptyValues() { + + // when + Address.Builder addressBuilder = new Address.Builder(); + Address address = addressBuilder.setCity(CITY_NAME).build(); + + Employee.Builder builder = new Employee.Builder(); + + Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") + .setAddress(address).build(); + + // then + assertFalse(employee.getDateOfJoining().isPresent()); + + } + + @Test + public void whenNullableFields_thenReturnEmployeeWithNullValueForField() { + + // when + Address.Builder addressBuilder = new Address.Builder(); + Address address = addressBuilder.setCity(CITY_NAME).build(); + + Employee.Builder builder = new Employee.Builder(); + + Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") + .setAddress(address).build(); + + // then + assertNull(employee.getCurrentProject()); + + } + + @Test + public void whenCollectionFields_thenReturnEmployeeWithValues() { + + // when + Address.Builder addressBuilder = new Address.Builder(); + Address address = addressBuilder.setCity(CITY_NAME).build(); + + Employee.Builder builder = new Employee.Builder(); + + Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") + .addAccessTokens(1221819L).addAccessTokens(1223441L, 134567L).setAddress(address).build(); + + // then + assertTrue(employee.getAccessTokens().size() == 3); + + } + + @Test + public void whenMapFields_thenReturnEmployeeWithValues() { + + // when + Address.Builder addressBuilder = new Address.Builder(); + Address address = addressBuilder.setCity(CITY_NAME).build(); + + Employee.Builder builder = new Employee.Builder(); + + Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") + .addAccessTokens(1221819L).addAccessTokens(1223441L, 134567L).putAssetsSerialIdMapping("Laptop", 12345L).setAddress(address).build(); + + // then + assertTrue(employee.getAssetsSerialIdMapping().size() == 1); + + } + + @Test + public void whenNestedBuilderTypes_thenReturnEmployeeWithValues() { + + // when + Address.Builder addressBuilder = new Address.Builder(); + Address address = addressBuilder.setCity(CITY_NAME).build(); + + Employee.Builder builder = new Employee.Builder(); + + Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") + .addAccessTokens(1221819L).addAccessTokens(1223441L, 134567L).putAssetsSerialIdMapping("Laptop", 12345L).setAddress(address).mutateAddress(a -> a.setPinCode(112200)).build(); + + // then + assertTrue(employee.getAssetsSerialIdMapping().size() == 1); + + } + + @Test() + public void whenPartialEmployeeWithValidEmail_thenReturnEmployeeWithEmail() { + + // when + Employee.Builder builder = new Employee.Builder(); + + Employee employee = builder.setName("baeldung") + .setAge(10) + .setEmail("abc@xyz.com") + .buildPartial(); + + assertNotNull(employee.getEmail()); + } + +} diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java b/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java new file mode 100644 index 0000000000..930820009c --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.freebuilder.builder.classic; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +class EmployeeBuilderUnitTest { + + private static final String CITY = "New York"; + + @Test + public void whenBuildEmployeeWithAddress_thenReturnEmployeeWithValidAddress() { + + // when + Employee.Builder emplBuilder = new Employee.Builder(); + + Employee employee = emplBuilder + .setName("baeldung") + .setAge(12) + .setDepartment("Builder Pattern") + .setDesignation("Author") + .setEmail("abc@xyz.com") + .setPermanent(true) + .setSupervisorName("Admin") + .setPhoneNumber(4445566) + .build(); + + //then + Assertions.assertTrue(employee.getAddress().getCity().equalsIgnoreCase(CITY)); + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 05c719ec7c..90cea73a9d 100644 --- a/pom.xml +++ b/pom.xml @@ -1571,7 +1571,7 @@ 0.3.1 2.5.1 0.0.1 - 3.8 + 3.7 2.3 3.8 From e40946249a032155615d0e63800edb5081aa74fe Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Wed, 17 Jul 2019 08:54:01 +0530 Subject: [PATCH 02/10] BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder --- .../builder/classic/EmployeeBuilderUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java b/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java index 930820009c..5040bfc9e0 100644 --- a/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java @@ -6,16 +6,16 @@ import org.junit.jupiter.api.Test; class EmployeeBuilderUnitTest { - private static final String CITY = "New York"; + public static final String NAME = "baeldung"; @Test - public void whenBuildEmployeeWithAddress_thenReturnEmployeeWithValidAddress() { + public void whenBuildEmployee_thenReturnValidEmployee() { // when Employee.Builder emplBuilder = new Employee.Builder(); Employee employee = emplBuilder - .setName("baeldung") + .setName(NAME) .setAge(12) .setDepartment("Builder Pattern") .setDesignation("Author") @@ -26,7 +26,7 @@ class EmployeeBuilderUnitTest { .build(); //then - Assertions.assertTrue(employee.getAddress().getCity().equalsIgnoreCase(CITY)); + Assertions.assertTrue(employee.getName().equalsIgnoreCase(NAME)); } } \ No newline at end of file From ce7c3067f2d88311981891efc55498a95bb0244b Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Wed, 17 Jul 2019 10:19:50 +0530 Subject: [PATCH 03/10] BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder --- .../freebuilder/EmployeeBuilderUnitTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java index e3a9e49633..abdd213de5 100644 --- a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java @@ -1,16 +1,16 @@ package com.baeldung.freebuilder; -import com.baeldung.freebuilder.builder.classic.Address; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Optional; + +import org.junit.jupiter.api.Test; + +import com.baeldung.freebuilder.builder.classic.Address; + public class EmployeeBuilderUnitTest { private static final int PIN_CODE = 223344; From 97e38d338ff145cff3f069d4de86dbc1e0fdd21d Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Wed, 24 Jul 2019 21:32:42 +0530 Subject: [PATCH 04/10] BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder --- .../com/baeldung/freebuilder/Employee.java | 2 + .../freebuilder/builder/classic/Employee.java | 138 +----------------- .../freebuilder/EmployeeBuilderUnitTest.java | 18 ++- .../classic/EmployeeBuilderUnitTest.java | 5 - 4 files changed, 23 insertions(+), 140 deletions(-) diff --git a/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java b/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java index 08470cc5cd..d486c18a06 100644 --- a/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java +++ b/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java @@ -41,6 +41,8 @@ public interface Employee { Map getAssetsSerialIdMapping(); + Optional getSalaryInUSD(); + class Builder extends Employee_Builder { diff --git a/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java b/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java index bd9c8b0a17..6509676cb4 100644 --- a/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java +++ b/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java @@ -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); } } diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java index abdd213de5..c73e2be8a6 100644 --- a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java @@ -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); diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java b/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java index 5040bfc9e0..e54a1dbd78 100644 --- a/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java @@ -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 From 6a29eab8209128fd479544dd29cb7d7afb768805 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sun, 28 Jul 2019 16:05:17 +0530 Subject: [PATCH 05/10] BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder --- .../freebuilder/EmployeeBuilderUnitTest.java | 85 ++++++++++++++++--- 1 file changed, 74 insertions(+), 11 deletions(-) diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java index c73e2be8a6..fef60d9e7c 100644 --- a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java @@ -27,7 +27,16 @@ public class EmployeeBuilderUnitTest { 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) + .build(); // then assertTrue(employee.getAddress().getCity().equalsIgnoreCase(CITY_NAME)); @@ -71,8 +80,17 @@ public class EmployeeBuilderUnitTest { Employee.Builder builder = new Employee.Builder(); - Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") - .setAddress(address).build(); + Employee employee = builder.setName("baeldung") + .setAge(10) + .setDesignation("author") + .setEmail("abc@xyz.com") + .setSupervisorName("Admin") + .setPhoneNumber(4445566) + .setNullablePermanent(null) + .setDateOfJoining(Optional.empty()) + .setRole("developer") + .setAddress(address) + .build(); // then assertFalse(employee.getDateOfJoining().isPresent()); @@ -88,8 +106,17 @@ public class EmployeeBuilderUnitTest { Employee.Builder builder = new Employee.Builder(); - Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") - .setAddress(address).build(); + Employee employee = builder.setName("baeldung") + .setAge(10) + .setDesignation("author") + .setEmail("abc@xyz.com") + .setSupervisorName("Admin") + .setPhoneNumber(4445566) + .setNullablePermanent(null) + .setDateOfJoining(Optional.empty()) + .setRole("developer") + .setAddress(address) + .build(); // then assertNull(employee.getCurrentProject()); @@ -105,8 +132,19 @@ public class EmployeeBuilderUnitTest { Employee.Builder builder = new Employee.Builder(); - Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") - .addAccessTokens(1221819L).addAccessTokens(1223441L, 134567L).setAddress(address).build(); + Employee employee = builder.setName("baeldung") + .setAge(10) + .setDesignation("author") + .setEmail("abc@xyz.com") + .setSupervisorName("Admin") + .setPhoneNumber(4445566) + .setNullablePermanent(null) + .setDateOfJoining(Optional.empty()) + .setRole("developer") + .addAccessTokens(1221819L) + .addAccessTokens(1223441L, 134567L) + .setAddress(address) + .build(); // then assertTrue(employee.getAccessTokens().size() == 3); @@ -122,8 +160,20 @@ public class EmployeeBuilderUnitTest { Employee.Builder builder = new Employee.Builder(); - Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") - .addAccessTokens(1221819L).addAccessTokens(1223441L, 134567L).putAssetsSerialIdMapping("Laptop", 12345L).setAddress(address).build(); + Employee employee = builder.setName("baeldung") + .setAge(10) + .setDesignation("author") + .setEmail("abc@xyz.com") + .setSupervisorName("Admin") + .setPhoneNumber(4445566) + .setNullablePermanent(null) + .setDateOfJoining(Optional.empty()) + .setRole("developer") + .addAccessTokens(1221819L) + .addAccessTokens(1223441L, 134567L) + .putAssetsSerialIdMapping("Laptop", 12345L) + .setAddress(address) + .build(); // then assertTrue(employee.getAssetsSerialIdMapping().size() == 1); @@ -139,8 +189,21 @@ public class EmployeeBuilderUnitTest { Employee.Builder builder = new Employee.Builder(); - Employee employee = builder.setName("baeldung").setAge(10).setDesignation("author").setEmail("abc@xyz.com").setSupervisorName("Admin").setPhoneNumber(4445566).setNullablePermanent(null).setDateOfJoining(Optional.empty()).setRole("developer") - .addAccessTokens(1221819L).addAccessTokens(1223441L, 134567L).putAssetsSerialIdMapping("Laptop", 12345L).setAddress(address).mutateAddress(a -> a.setPinCode(112200)).build(); + Employee employee = builder.setName("baeldung") + .setAge(10) + .setDesignation("author") + .setEmail("abc@xyz.com") + .setSupervisorName("Admin") + .setPhoneNumber(4445566) + .setNullablePermanent(null) + .setDateOfJoining(Optional.empty()) + .setRole("developer") + .addAccessTokens(1221819L) + .addAccessTokens(1223441L, 134567L) + .putAssetsSerialIdMapping("Laptop", 12345L) + .setAddress(address) + .mutateAddress(a -> a.setPinCode(112200)) + .build(); // then assertTrue(employee.getAssetsSerialIdMapping().size() == 1); From f71224ed2cda1ab093764347ca636c412b111ff8 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Mon, 29 Jul 2019 21:15:24 +0530 Subject: [PATCH 06/10] BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder --- .../com/baeldung/freebuilder/EmployeeBuilderUnitTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java index fef60d9e7c..4427d3cb69 100644 --- a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java +++ b/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java @@ -86,13 +86,14 @@ public class EmployeeBuilderUnitTest { .setEmail("abc@xyz.com") .setSupervisorName("Admin") .setPhoneNumber(4445566) - .setNullablePermanent(null) - .setDateOfJoining(Optional.empty()) + .setPermanent(true) .setRole("developer") .setAddress(address) .build(); // then + assertTrue(employee.getPermanent().isPresent()); + assertTrue(employee.getPermanent().get()); assertFalse(employee.getDateOfJoining().isPresent()); } From d45e50fa995ac298da4e0b5c294a6520cb0eea70 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sun, 4 Aug 2019 13:21:06 +0530 Subject: [PATCH 07/10] BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder --- patterns/design-patterns-2/pom.xml | 13 +++++++++++++ .../java/com/baeldung/freebuilder}/Address.java | 2 +- .../java/com/baeldung/freebuilder/Employee.java | 2 -- .../com/baeldung/freebuilder/builder}/Employee.java | 2 +- .../freebuilder/EmployeeBuilderUnitTest.java | 10 ++++------ .../builder}/EmployeeBuilderUnitTest.java | 2 +- 6 files changed, 20 insertions(+), 11 deletions(-) rename {libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic => patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder}/Address.java (88%) rename {libraries-2 => patterns/design-patterns-2}/src/main/java/com/baeldung/freebuilder/Employee.java (92%) rename {libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic => patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/builder}/Employee.java (95%) rename {libraries-2 => patterns/design-patterns-2}/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java (99%) rename {libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic => patterns/design-patterns-2/src/test/java/com/baeldung/freebuilder/builder}/EmployeeBuilderUnitTest.java (91%) diff --git a/patterns/design-patterns-2/pom.xml b/patterns/design-patterns-2/pom.xml index 392035ab84..17afff87ae 100644 --- a/patterns/design-patterns-2/pom.xml +++ b/patterns/design-patterns-2/pom.xml @@ -30,6 +30,17 @@ org.apache.commons commons-lang3 ${commons-lang3.version} + + + org.inferred + freebuilder + ${freebuilder.version} + true + + + com.google.code.findbugs + jsr305 + ${javax.annotations.version} @@ -38,5 +49,7 @@ 1.8 1.8 16.0.2 + 2.4.1 + 3.0.2 diff --git a/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Address.java b/patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/Address.java similarity index 88% rename from libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Address.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/Address.java index 8e53a9f155..6d727f6d9f 100644 --- a/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Address.java +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/Address.java @@ -1,4 +1,4 @@ -package com.baeldung.freebuilder.builder.classic; +package com.baeldung.freebuilder; import org.inferred.freebuilder.FreeBuilder; diff --git a/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java b/patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/Employee.java similarity index 92% rename from libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/Employee.java index d486c18a06..06558dde0b 100644 --- a/libraries-2/src/main/java/com/baeldung/freebuilder/Employee.java +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/Employee.java @@ -1,13 +1,11 @@ package com.baeldung.freebuilder; -import com.baeldung.freebuilder.builder.classic.Address; import org.inferred.freebuilder.FreeBuilder; import javax.annotation.Nullable; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.function.UnaryOperator; @FreeBuilder public interface Employee { diff --git a/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java b/patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/builder/Employee.java similarity index 95% rename from libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java rename to patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/builder/Employee.java index 6509676cb4..141d734c43 100644 --- a/libraries-2/src/main/java/com/baeldung/freebuilder/builder/classic/Employee.java +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/freebuilder/builder/Employee.java @@ -1,4 +1,4 @@ -package com.baeldung.freebuilder.builder.classic; +package com.baeldung.freebuilder.builder; public class Employee { diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java similarity index 99% rename from libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java rename to patterns/design-patterns-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java index 4427d3cb69..d28edfcf3d 100644 --- a/libraries-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/freebuilder/EmployeeBuilderUnitTest.java @@ -1,16 +1,14 @@ package com.baeldung.freebuilder; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Optional; - -import org.junit.jupiter.api.Test; - -import com.baeldung.freebuilder.builder.classic.Address; - public class EmployeeBuilderUnitTest { private static final int PIN_CODE = 223344; diff --git a/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/freebuilder/builder/EmployeeBuilderUnitTest.java similarity index 91% rename from libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java rename to patterns/design-patterns-2/src/test/java/com/baeldung/freebuilder/builder/EmployeeBuilderUnitTest.java index e54a1dbd78..1394f851b6 100644 --- a/libraries-2/src/test/java/com/baeldung/freebuilder/builder/classic/EmployeeBuilderUnitTest.java +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/freebuilder/builder/EmployeeBuilderUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.freebuilder.builder.classic; +package com.baeldung.freebuilder.builder; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; From c3020771206d2e14416bbb936116afabe0629041 Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Wed, 7 Aug 2019 21:36:14 +0530 Subject: [PATCH 08/10] BAEL-2990 Automatic generation of the Builder pattern with FreeBuilder --- libraries-2/pom.xml | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/libraries-2/pom.xml b/libraries-2/pom.xml index a9a818615f..bac601df61 100644 --- a/libraries-2/pom.xml +++ b/libraries-2/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 libraries2 libraries2 @@ -111,15 +111,9 @@ org.apache.mesos mesos ${mesos.library.version} - - - org.inferred - freebuilder - ${freebuilder.version} - true - + 3.0.7 3.6.2 @@ -135,6 +129,5 @@ 2.8.5 3.14.2 4.1.2 - 2.4.1 - - + + \ No newline at end of file From fcc7ca0f1d1630a7f3186a8ff97d97aab6876d91 Mon Sep 17 00:00:00 2001 From: dev-chirag <41482403+dev-chirag@users.noreply.github.com> Date: Wed, 7 Aug 2019 21:40:20 +0530 Subject: [PATCH 09/10] Update pom.xml --- libraries-2/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries-2/pom.xml b/libraries-2/pom.xml index bac601df61..83adf1e199 100644 --- a/libraries-2/pom.xml +++ b/libraries-2/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 libraries2 libraries2 @@ -130,4 +130,4 @@ 3.14.2 4.1.2 - \ No newline at end of file + From ec8b02a6882161e309d9f673d736efccd2adc96f Mon Sep 17 00:00:00 2001 From: dev-chirag <41482403+dev-chirag@users.noreply.github.com> Date: Wed, 7 Aug 2019 21:40:48 +0530 Subject: [PATCH 10/10] Update pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 14b9d3cf81..aabede4f3a 100644 --- a/pom.xml +++ b/pom.xml @@ -1579,7 +1579,7 @@ 0.3.1 2.5.1 0.0.1 - 3.7 + 3.8 2.3 3.8