Commit e31799ea authored by Dominik Gruntz's avatar Dominik Gruntz
Browse files

07_Prototype1: solutions

parent a4cd41bd
package patterns.clone.company.clone;
import java.util.ArrayList;
import java.util.List;
public class Company implements Cloneable {
private String name;
private List<Employee> employees = new ArrayList<>();
public Company(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return employees.size();
}
public void addEmployee(Employee p) {
this.employees.add(p);
}
@Override
public boolean equals(Object o) {
if (o != null && o.getClass() == this.getClass()) {
Company c = (Company) o;
return name.equals(c.name) && employees.equals(c.employees);
} else {
return false;
}
}
@Override
public Company clone() {
try {
// shallow copy
Company c = (Company) super.clone();
// // deep copy
// c.employees = new ArrayList<>(c.employees);
// really deep copy
c.employees = new ArrayList<>();
for (Employee e : employees) {
c.employees.add(e.clone());
}
return c;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}
package patterns.clone.company.clone;
public class Employee implements Cloneable {
private String name;
private int yearOfBirth;
public Employee(String name, int yearOfBirth) {
this.name = name;
this.yearOfBirth = yearOfBirth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYearOfBirth() {
return yearOfBirth;
}
public void setYearOfBirth(int yearOfBirth) {
this.yearOfBirth = yearOfBirth;
}
@Override
public boolean equals(Object o) {
if(o != null && o.getClass() == this.getClass()) {
Employee p = (Employee) o;
return (p.yearOfBirth == yearOfBirth) && (p.name.equals(name));
} else {
return false;
}
}
@Override
public Employee clone() {
try {
return (Employee) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
}
package patterns.clone.company.clone;
public class PartTimeEmployee extends Employee {
private int workload;
public PartTimeEmployee(String name, int yearOfBirth, int workload) {
super(name, yearOfBirth);
this.workload = workload;
}
public int getWorkload() {
return workload;
}
public void setWorkload(int workload) {
this.workload = workload;
}
@Override
public boolean equals(Object o) {
if (o != null && o.getClass() == this.getClass()) {
PartTimeEmployee p = (PartTimeEmployee)o;
return p.workload == workload && super.equals(o);
} else {
return false;
}
}
@Override
public PartTimeEmployee clone() {
return (PartTimeEmployee)super.clone();
}
}
package patterns.clone.company.copy;
import java.util.ArrayList;
import java.util.List;
public class Company {
private String name;
private List<Employee> employees;
public Company(String name) {
this.name = name;
this.employees = new ArrayList<>();
}
protected Company(Company c) {
this.name = c.name;
// shallow copy:
// this.employees = c.employees;
// deep copy
// this.employees = new ArrayList<>(c.employees);
// really deep copy
this.employees = new ArrayList<>();
for (Employee e : c.employees) {
this.employees.add(e.clone());
}
// this.employees = c.employees.stream().map(Employee::clone).collect(Collectors.toList());
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public int getSize() {
return employees.size();
}
public void addEmployee(Employee p) {
this.employees.add(p);
}
@Override
public boolean equals(Object o) {
if (o != null && o.getClass() == this.getClass()) {
Company c = (Company) o;
return name.equals(c.name) && employees.equals(c.employees);
} else {
return false;
}
}
@Override
public Company clone() {
return new Company(this);
}
}
package patterns.clone.company.copy;
public class Employee {
private String name;
private int yearOfBirth;
public Employee(String name, int yearOfBirth) {
this.name = name;
this.yearOfBirth = yearOfBirth;
}
protected Employee(Employee e) {
this.name = e.name;
this.yearOfBirth = e.yearOfBirth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYearOfBirth() {
return yearOfBirth;
}
public void setYearOfBirth(int yearOfBirth) {
this.yearOfBirth = yearOfBirth;
}
@Override
public boolean equals(Object o) {
if(o != null && o.getClass() == this.getClass()) {
Employee p = (Employee) o;
return (p.yearOfBirth == yearOfBirth) && (p.name.equals(name));
} else {
return false;
}
}
@Override
public Employee clone() {
return new Employee(this);
}
}
package patterns.clone.company.copy;
public class PartTimeEmployee extends Employee {
private int workload;
public PartTimeEmployee(String name, int yearOfBirth, int workload) {
super(name, yearOfBirth);
this.workload = workload;
}
protected PartTimeEmployee(PartTimeEmployee p) {
super(p);
this.workload = p.workload;
}
public int getWorkload() {
return workload;
}
public void setWorkload(int workload) {
this.workload = workload;
}
@Override
public boolean equals(Object o) {
if (o != null && o.getClass() == this.getClass()) {
PartTimeEmployee p = (PartTimeEmployee)o;
return p.workload == workload && super.equals(o);
} else {
return false;
}
}
@Override
public PartTimeEmployee clone() {
return new PartTimeEmployee(this);
}
}
package patterns.clone.company.clone;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CloningTest {
private Company c1;
private Employee p1;
@BeforeEach
public void setUp() throws Exception {
p1 = new Employee("P1", 1990);
Employee p2 = new Employee("P2", 1989);
Employee p3 = new Employee("P3", 1993);
c1 = new Company("C1");
c1.addEmployee(p1);
c1.addEmployee(p2);
c1.addEmployee(p3);
}
@Test
public void testCompanyClone() {
Company c2 = c1.clone();
assertTrue(c1 != c2);
assertEquals(c1.getClass(), c2.getClass());
assertEquals(c1.getName(), c2.getName());
}
@Test
public void testEmployeeClone() {
Employee p2 = p1.clone();
assertTrue(p1 != p2);
assertEquals(p1.getClass(), p2.getClass());
assertEquals(p1.getName(), p2.getName());
assertEquals(p1.getYearOfBirth(), p2.getYearOfBirth());
}
@Test
public void testShallow() {
Company c2 = c1.clone();
assertEquals(c1, c2);
}
@Test
public void testDeep1() {
Company c2 = c1.clone();
assertEquals(c1, c2);
c1.addEmployee(new Employee("new", 1993));
assertFalse(c1.equals(c2));
}
@Test
public void testDeep2() {
Company c2 = c1.clone();
assertEquals(c1, c2, "this is not even a shallow copy");
c1.addEmployee(new Employee("new", 1993));
assertFalse(c1.equals(c2), "this is only a shallow copy");
c2 = c1.clone();
p1.setName("Changed");
assertFalse(c1.equals(c2), "you didn't copy deep enough");
}
@Test
public void testDeepWithPartTimeEmployee() {
PartTimeEmployee ppe = new PartTimeEmployee("new", 1993, 42);
c1.addEmployee(ppe);
Company c2 = c1.clone();
assertEquals(c1, c2, "this is not even a shallow copy");
ppe.setWorkload(10);
assertFalse(c1.equals(c2), "you didn't copy deep enough");
}
}
package patterns.clone.company.copy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CloningTest {
private Company c1;
private Employee p1;
@BeforeEach
public void setUp() throws Exception {
p1 = new Employee("P1", 1990);
Employee p2 = new Employee("P2", 1989);
Employee p3 = new Employee("P3", 1993);
c1 = new Company("C1");
c1.addEmployee(p1);
c1.addEmployee(p2);
c1.addEmployee(p3);
}
@Test
public void testCompanyClone() {
Company c2 = c1.clone();
assertTrue(c1 != c2);
assertEquals(c1.getClass(), c2.getClass());
assertEquals(c1.getName(), c2.getName());
}
@Test
public void testEmployeeClone() {
Employee p2 = p1.clone();
assertTrue(p1 != p2);
assertEquals(p1.getClass(), p2.getClass());
assertEquals(p1.getName(), p2.getName());
assertEquals(p1.getYearOfBirth(), p2.getYearOfBirth());
}
@Test
public void testShallow() {
Company c2 = c1.clone();
assertEquals(c1, c2);
}
@Test
public void testDeep1() {
Company c2 = c1.clone();
assertEquals(c1, c2);
c1.addEmployee(new Employee("new", 1993));
assertFalse(c1.equals(c2));
}
@Test
public void testDeep2() {
Company c2 = c1.clone();
assertEquals(c1, c2, "this is not even a shallow copy");
c1.addEmployee(new Employee("new", 1993));
assertFalse(c1.equals(c2), "this is only a shallow copy");
c2 = c1.clone();
p1.setName("Changed");
assertFalse(c1.equals(c2), "you didn't copy deep enough");
}
@Test
public void testDeepWithPartTimeEmployee() {
PartTimeEmployee ppe = new PartTimeEmployee("new", 1993, 42);
c1.addEmployee(ppe);
Company c2 = c1.clone();
assertEquals(c1, c2, "this is not even a shallow copy");
ppe.setWorkload(10);
assertFalse(c1.equals(c2), "you didn't copy deep enough");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment