[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.
This commit is contained in:
wugangca 2019-09-07 03:17:33 -06:00 committed by Josh Cummings
parent 648ef77f5d
commit f24fef9ec6
4 changed files with 164 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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<Employee> directReports;
public Manager(int id, String name, Date startDate, List<Employee> 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<Employee> getDirectReport() {
return this.directReports;
}
}

View File

@ -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());
}
}

View File

@ -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<Employee> directReports = new ArrayList<Employee>();
directReports.add(e1);
directReports.add(e2);
Manager m1 = new Manager(1, "Baeldung Manager", startDate, directReports);
Manager m2 = new Manager(m1);
List<Employee> directReports1 = m1.getDirectReport();
List<Employee> 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<Employee> directReports = new ArrayList<Employee>();
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<Employee> directReports1 = ((Manager) source).getDirectReport();
List<Employee> 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());
}
}