From f24fef9ec61cb6a1ee4e354980333396a9dcdd80 Mon Sep 17 00:00:00 2001 From: wugangca Date: Sat, 7 Sep 2019 03:17:33 -0600 Subject: [PATCH] [BAEL-3210] - Java Copy Constructor (#7706) * [BAEL-3210] - Copy constructors in Java * [BAEL-3210] - Copy constructors in Java * Change indentation space based on code style instructions. --- .../baeldung/copyconstructor/Employee.java | 30 +++++++++ .../com/baeldung/copyconstructor/Manager.java | 31 +++++++++ .../copyconstructor/EmployeeUnitTest.java | 36 ++++++++++ .../copyconstructor/ManagerUnitTest.java | 67 +++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java create mode 100644 core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java create mode 100644 core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java new file mode 100644 index 0000000000..7ad445e8ee --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java @@ -0,0 +1,30 @@ +package com.baeldung.copyconstructor; + +import java.util.Date; + +public class Employee { + + protected int id; + protected String name; + protected Date startDate; + + public Employee(int id, String name, Date startDate) { + this.id = id; + this.name = name; + this.startDate = startDate; + } + + public Employee(Employee employee) { + this.id = employee.id; + this.name = employee.name; + this.startDate = new Date(employee.startDate.getTime()); + } + + Date getStartDate() { + return startDate; + } + + public Employee copy() { + return new Employee(this); + } +} diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java new file mode 100644 index 0000000000..97b8580b8e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java @@ -0,0 +1,31 @@ +package com.baeldung.copyconstructor; + +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +public class Manager extends Employee { + + private List directReports; + + public Manager(int id, String name, Date startDate, List directReports) { + super(id, name, startDate); + this.directReports = directReports; + } + + public Manager(Manager manager) { + super(manager.id, manager.name, manager.startDate); + this.directReports = manager.directReports.stream() + .collect(Collectors.toList()); + } + + @Override + public Employee copy() { + return new Manager(this); + } + + List getDirectReport() { + return this.directReports; + } + +} diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java new file mode 100644 index 0000000000..19a4001b62 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.copyconstructor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.Date; + +import org.junit.Test; + +public class EmployeeUnitTest { + @Test + public void givenCopyConstructor_whenDeepCopy_thenDistinct() { + Date d1 = new Date(123); + Employee e1 = new Employee(1, "Baeldung", d1); + Employee e2 = new Employee(e1); + assertEquals(d1, e1.getStartDate()); + assertEquals(d1, e2.getStartDate()); + + d1.setTime(456); + assertEquals(d1, e1.getStartDate()); + assertNotEquals(d1, e2.getStartDate()); + } + + @Test + public void givenCopyMethod_whenCopy_thenDistinct() { + Date d1 = new Date(123); + Employee e1 = new Employee(1, "Baeldung", d1); + Employee e2 = e1.copy(); + assertEquals(d1, e1.getStartDate()); + assertEquals(d1, e2.getStartDate()); + + d1.setTime(456); + assertEquals(d1, e1.getStartDate()); + assertNotEquals(d1, e2.getStartDate()); + } +} diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java new file mode 100644 index 0000000000..ef9f261360 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java @@ -0,0 +1,67 @@ +package com.baeldung.copyconstructor; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.junit.Test; + +public class ManagerUnitTest { + @Test + public void givenCopyConstructor_whenDeepCopy_thenDistinct() { + Date startDate = new Date(123); + Employee e1 = new Employee(1, "Baeldung", startDate); + Employee e2 = new Employee(e1); + List directReports = new ArrayList(); + directReports.add(e1); + directReports.add(e2); + + Manager m1 = new Manager(1, "Baeldung Manager", startDate, directReports); + Manager m2 = new Manager(m1); + List directReports1 = m1.getDirectReport(); + List directReports2 = m2.getDirectReport(); + assertEquals(directReports1.size(), directReports2.size()); + assertArrayEquals(directReports1.toArray(), directReports2.toArray()); + + // clear m1's direct reports list. m2's list should not be affected + directReports.clear(); + directReports1 = m1.getDirectReport(); + directReports2 = m2.getDirectReport(); + assertEquals(0, directReports1.size()); + assertEquals(2, directReports2.size()); + + } + + @Test + public void givenCopyMethod_whenCopy_thenDistinct() { + Date startDate = new Date(123); + Employee e1 = new Employee(1, "Baeldung", startDate); + Employee e2 = new Employee(e1); + List directReports = new ArrayList(); + directReports.add(e1); + directReports.add(e2); + + // a Manager object whose declaration type is Employee. + Employee source = new Manager(1, "Baeldung Manager", startDate, directReports); + Employee clone = source.copy(); + + // after copy, clone should be still a Manager object. + assertTrue(clone instanceof Manager); + List directReports1 = ((Manager) source).getDirectReport(); + List directReports2 = ((Manager) clone).getDirectReport(); + assertEquals(directReports1.size(), directReports2.size()); + assertArrayEquals(directReports1.toArray(), directReports2.toArray()); + + // clear source's direct reports list. clone's list should not be affected + directReports.clear(); + directReports1 = ((Manager) source).getDirectReport(); + directReports2 = ((Manager) clone).getDirectReport(); + assertEquals(0, directReports1.size()); + assertEquals(2, directReports2.size()); + + } +}