diff --git a/shallow-deep-copy/pom.xml b/shallow-deep-copy/pom.xml index 01c1430f28..323f720df3 100644 --- a/shallow-deep-copy/pom.xml +++ b/shallow-deep-copy/pom.xml @@ -1,16 +1,16 @@ - 4.0.0 - shallow-deep-copy - shallow-deep-copy - jar + 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 + shallow-deep-copy + shallow-deep-copy + jar - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + \ No newline at end of file diff --git a/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Dependency.java b/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Dependency.java index a5da5fc163..2550acd268 100644 --- a/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Dependency.java +++ b/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Dependency.java @@ -1,23 +1,23 @@ package com.baeldung.shallowdeepcopy; public class Dependency implements Cloneable { - private int value = 13; - - public Dependency(int value) { - super(); - this.value = value; - } + private int value = 13; - public int getValue() { - return value; - } + public Dependency(int value) { + super(); + this.value = value; + } - public void setValue(int value) { - this.value = value; - } + public int getValue() { + return value; + } - @Override - protected Object clone() throws CloneNotSupportedException { - return super.clone(); - } + public void setValue(int value) { + this.value = value; + } + + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } } diff --git a/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Main.java b/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Main.java index fb24007162..33d565bb10 100644 --- a/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Main.java +++ b/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Main.java @@ -1,31 +1,31 @@ package com.baeldung.shallowdeepcopy; public class Main implements Cloneable { - private Dependency dependency; - - public Main(Dependency dependency) { - super(); - this.dependency = dependency; - } + private Dependency dependency; - @Override - public Object clone() throws CloneNotSupportedException { - return super.clone(); - } + public Main(Dependency dependency) { + super(); + this.dependency = dependency; + } - public Object deepClone() throws CloneNotSupportedException { - Main dependentClone = (Main) super.clone(); - Dependency dependencyClone = (Dependency) this.dependency.clone(); - dependentClone.setDependency(dependencyClone); - return dependentClone; - } - - public Dependency getDependency() { - return dependency; - } + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + public Object deepClone() throws CloneNotSupportedException { + Main dependentClone = (Main) super.clone(); + Dependency dependencyClone = (Dependency) this.dependency.clone(); + dependentClone.setDependency(dependencyClone); + return dependentClone; + } + + public Dependency getDependency() { + return dependency; + } + + public void setDependency(Dependency dependency) { + this.dependency = dependency; + } - public void setDependency(Dependency dependency) { - this.dependency = dependency; - } - } diff --git a/shallow-deep-copy/src/main/resources/logback.xml b/shallow-deep-copy/src/main/resources/logback.xml index 7d900d8ea8..830ab22727 100644 --- a/shallow-deep-copy/src/main/resources/logback.xml +++ b/shallow-deep-copy/src/main/resources/logback.xml @@ -1,13 +1,14 @@ - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + - - - + + + \ No newline at end of file diff --git a/shallow-deep-copy/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyUnitTest.java b/shallow-deep-copy/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyUnitTest.java index 321c2df327..f75bbc040b 100644 --- a/shallow-deep-copy/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyUnitTest.java +++ b/shallow-deep-copy/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyUnitTest.java @@ -5,23 +5,24 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; class ShallowDeepCopyUnitTest { - - @Test - public void givenAnObjectAndADependency_whenShallowClonedAndDepencyChanges_ThenDepencyHasNewValue() throws Exception { - Dependency dependency = new Dependency(13); - Main main = new Main(dependency); - Main shallowClone = (Main) main.clone(); - dependency.setValue(17); - assertEquals(17, shallowClone.getDependency().getValue()); - } - - @Test - public void givenAnObjectAndADependency_whenDeepClonedAndDepencyChanges_ThenDepencyKeepsOldValue() throws Exception { - Dependency dependency = new Dependency(13); - Main main = new Main(dependency); - Main deepClone = (Main) main.deepClone(); - dependency.setValue(17); - assertEquals(13, deepClone.getDependency().getValue()); - } + @Test + public void givenAnObjectAndADependency_whenShallowClonedAndDepencyChanges_ThenDepencyHasNewValue() throws Exception { + Dependency dependency = new Dependency(13); + Main main = new Main(dependency); + Main shallowClone = (Main) main.clone(); + dependency.setValue(17); + assertEquals(17, shallowClone.getDependency() + .getValue()); + } + + @Test + public void givenAnObjectAndADependency_whenDeepClonedAndDepencyChanges_ThenDepencyKeepsOldValue() throws Exception { + Dependency dependency = new Dependency(13); + Main main = new Main(dependency); + Main deepClone = (Main) main.deepClone(); + dependency.setValue(17); + assertEquals(13, deepClone.getDependency() + .getValue()); + } }