Skip to content

Commit 327719e

Browse files
xenterosKevinGilmore
authored andcommitted
Add complete codes for an article about deep copy (eugenp#3610)
* Add complete codes for an article about deep copy * Add missing gson dependency * Apply changes suggested in PR comments * Apply changes suggested in PR comments * Apply changes suggested in PR comments
1 parent 378cae0 commit 327719e

5 files changed

Lines changed: 274 additions & 0 deletions

File tree

‎core-java/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
<artifactId>jackson-databind</artifactId>
7979
<version>${jackson.version}</version>
8080
</dependency>
81+
<dependency>
82+
<groupId>com.google.code.gson</groupId>
83+
<artifactId>gson</artifactId>
84+
<version>${gson.version}</version>
85+
</dependency>
8186

8287
<!-- logging -->
8388
<dependency>
@@ -468,6 +473,7 @@
468473
<properties>
469474
<!-- marshalling -->
470475
<jackson.version>2.8.5</jackson.version>
476+
<gson.version>2.8.2</gson.version>
471477

472478
<!-- logging -->
473479
<org.slf4j.version>1.7.21</org.slf4j.version>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.deepcopy;
2+
3+
import java.io.Serializable;
4+
5+
public class Address implements Serializable, Cloneable {
6+
7+
@Override
8+
public Object clone() {
9+
try {
10+
return (Address) super.clone();
11+
} catch (CloneNotSupportedException e) {
12+
return new Address(this.street, this.getCity(), this.getCountry());
13+
}
14+
}
15+
16+
private static final long serialVersionUID = 1740913841244949416L;
17+
private String street;
18+
private String city;
19+
20+
private String country;
21+
22+
public Address(Address that) {
23+
this(that.getStreet(), that.getCity(), that.getCountry());
24+
}
25+
26+
public Address(String street, String city, String country) {
27+
this.street = street;
28+
this.city = city;
29+
this.country = country;
30+
}
31+
32+
public Address() {
33+
}
34+
35+
public String getStreet() {
36+
return street;
37+
}
38+
39+
public String getCity() {
40+
return city;
41+
}
42+
43+
public String getCountry() {
44+
return country;
45+
}
46+
47+
public void setStreet(String street) {
48+
this.street = street;
49+
}
50+
51+
public void setCity(String city) {
52+
this.city = city;
53+
}
54+
55+
public void setCountry(String country) {
56+
this.country = country;
57+
}
58+
}
59+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.deepcopy;
2+
3+
import java.io.Serializable;
4+
5+
public class User implements Serializable, Cloneable {
6+
7+
private static final long serialVersionUID = -3427002229954777557L;
8+
private String firstName;
9+
private String lastName;
10+
private Address address;
11+
12+
public User(String firstName, String lastName, Address address) {
13+
this.firstName = firstName;
14+
this.lastName = lastName;
15+
this.address = address;
16+
}
17+
18+
public User(User that) {
19+
this(that.getFirstName(), that.getLastName(), new Address(that.getAddress()));
20+
}
21+
22+
public User() {
23+
}
24+
25+
@Override
26+
public Object clone() {
27+
User user;
28+
try {
29+
user = (User) super.clone();
30+
} catch (CloneNotSupportedException e) {
31+
user = new User(this.getFirstName(), this.getLastName(), this.getAddress());
32+
}
33+
user.address = (Address) this.address.clone();
34+
return user;
35+
}
36+
37+
public String getFirstName() {
38+
return firstName;
39+
}
40+
41+
public String getLastName() {
42+
return lastName;
43+
}
44+
45+
public Address getAddress() {
46+
return address;
47+
}
48+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.baeldung.deepcopy;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.IOException;
6+
7+
import org.apache.commons.lang.SerializationUtils;
8+
import org.junit.Ignore;
9+
import org.junit.Test;
10+
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.google.gson.Gson;
13+
14+
public class DeepCopyUnitTest {
15+
16+
@Test
17+
public void whenCreatingDeepCopyWithCopyConstructor_thenObjectsShouldNotBeSame() {
18+
19+
Address address = new Address("Downing St 10", "London", "England");
20+
User pm = new User("Prime", "Minister", address);
21+
22+
User deepCopy = new User(pm);
23+
24+
assertThat(deepCopy).isNotSameAs(pm);
25+
}
26+
27+
@Test
28+
public void whenModifyingOriginalObject_thenConstructorCopyShouldNotChange() {
29+
Address address = new Address("Downing St 10", "London", "England");
30+
User pm = new User("Prime", "Minister", address);
31+
User deepCopy = new User(pm);
32+
33+
address.setCountry("Great Britain");
34+
35+
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
36+
}
37+
38+
@Test
39+
public void whenModifyingOriginalObject_thenCloneCopyShouldNotChange() {
40+
Address address = new Address("Downing St 10", "London", "England");
41+
User pm = new User("Prime", "Minister", address);
42+
User deepCopy = (User) pm.clone();
43+
44+
address.setCountry("Great Britain");
45+
46+
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
47+
}
48+
49+
@Test
50+
public void whenModifyingOriginalObject_thenCommonsCloneShouldNotChange() {
51+
Address address = new Address("Downing St 10", "London", "England");
52+
User pm = new User("Prime", "Minister", address);
53+
User deepCopy = (User) SerializationUtils.clone(pm);
54+
55+
address.setCountry("Great Britain");
56+
57+
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
58+
}
59+
60+
@Test
61+
public void whenModifyingOriginalObject_thenGsonCloneShouldNotChange() {
62+
Address address = new Address("Downing St 10", "London", "England");
63+
User pm = new User("Prime", "Minister", address);
64+
Gson gson = new Gson();
65+
User deepCopy = gson.fromJson(gson.toJson(pm), User.class);
66+
67+
address.setCountry("Great Britain");
68+
69+
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
70+
}
71+
72+
@Test
73+
public void whenModifyingOriginalObject_thenJacksonCopyShouldNotChange() throws IOException {
74+
Address address = new Address("Downing St 10", "London", "England");
75+
User pm = new User("Prime", "Minister", address);
76+
ObjectMapper objectMapper = new ObjectMapper();
77+
User deepCopy = objectMapper.readValue(objectMapper.writeValueAsString(pm), User.class);
78+
79+
address.setCountry("Great Britain");
80+
81+
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
82+
}
83+
84+
@Test
85+
@Ignore
86+
public void whenMakingCopies_thenShowHowLongEachMethodTakes() throws CloneNotSupportedException, IOException {
87+
int times = 1000000;
88+
Address address = new Address("Downing St 10", "London", "England");
89+
User pm = new User("Prime", "Minister", address);
90+
91+
long start = System.currentTimeMillis();
92+
for (int i = 0; i < times; i++) {
93+
User primeMinisterClone = (User) SerializationUtils.clone(pm);
94+
}
95+
long end = System.currentTimeMillis();
96+
System.out.println("Cloning with Apache Commons Lang took " + (end - start) + " milliseconds.");
97+
98+
start = System.currentTimeMillis();
99+
Gson gson = new Gson();
100+
for (int i = 0; i < times; i++) {
101+
User primeMinisterClone = gson.fromJson(gson.toJson(pm), User.class);
102+
}
103+
end = System.currentTimeMillis();
104+
System.out.println("Cloning with Gson took " + (end - start) + " milliseconds.");
105+
106+
start = System.currentTimeMillis();
107+
for (int i = 0; i < times; i++) {
108+
User primeMinisterClone = new User(pm);
109+
}
110+
end = System.currentTimeMillis();
111+
System.out.println("Cloning with the copy constructor took " + (end - start) + " milliseconds.");
112+
113+
start = System.currentTimeMillis();
114+
for (int i = 0; i < times; i++) {
115+
User primeMinisterClone = (User) pm.clone();
116+
}
117+
end = System.currentTimeMillis();
118+
System.out.println("Cloning with Cloneable interface took " + (end - start) + " milliseconds.");
119+
120+
start = System.currentTimeMillis();
121+
ObjectMapper objectMapper = new ObjectMapper();
122+
for (int i = 0; i < times; i++) {
123+
User primeMinisterClone = objectMapper.readValue(objectMapper.writeValueAsString(pm), User.class);
124+
}
125+
end = System.currentTimeMillis();
126+
System.out.println("Cloning with Jackson took " + (end - start) + " milliseconds.");
127+
}
128+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.deepcopy;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class ShallowCopyUnitTest {
8+
9+
10+
@Test
11+
public void whenShallowCopying_thenObjectsShouldNotBeSame() {
12+
13+
Address address = new Address("Downing St 10", "London", "England");
14+
User pm = new User("Prime", "Minister", address);
15+
16+
User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress());
17+
18+
assertThat(shallowCopy)
19+
.isNotSameAs(pm);
20+
}
21+
22+
@Test
23+
public void whenModifyingOriginalObject_thenCopyShouldChange() {
24+
Address address = new Address("Downing St 10", "London", "England");
25+
User pm = new User("Prime", "Minister", address);
26+
User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress());
27+
28+
address.setCountry("Great Britain");
29+
30+
assertThat(shallowCopy.getAddress().getCountry())
31+
.isEqualTo(pm.getAddress().getCountry());
32+
}
33+
}

0 commit comments

Comments
 (0)