Format content with baeldung formatter

This commit is contained in:
mcasari 2023-08-22 19:49:54 +02:00
parent c6e1f95e42
commit 8a6e3f233e
5 changed files with 80 additions and 78 deletions

View File

@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>shallow-deep-copy</artifactId> <artifactId>shallow-deep-copy</artifactId>
<name>shallow-deep-copy</name> <name>shallow-deep-copy</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId> <artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
</project> </project>

View File

@ -1,23 +1,23 @@
package com.baeldung.shallowdeepcopy; package com.baeldung.shallowdeepcopy;
public class Dependency implements Cloneable { public class Dependency implements Cloneable {
private int value = 13; private int value = 13;
public Dependency(int value) {
super();
this.value = value;
}
public int getValue() { public Dependency(int value) {
return value; super();
} this.value = value;
}
public void setValue(int value) { public int getValue() {
this.value = value; return value;
} }
@Override public void setValue(int value) {
protected Object clone() throws CloneNotSupportedException { this.value = value;
return super.clone(); }
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
} }

View File

@ -1,31 +1,31 @@
package com.baeldung.shallowdeepcopy; package com.baeldung.shallowdeepcopy;
public class Main implements Cloneable { public class Main implements Cloneable {
private Dependency dependency; private Dependency dependency;
public Main(Dependency dependency) {
super();
this.dependency = dependency;
}
@Override public Main(Dependency dependency) {
public Object clone() throws CloneNotSupportedException { super();
return super.clone(); this.dependency = dependency;
} }
public Object deepClone() throws CloneNotSupportedException { @Override
Main dependentClone = (Main) super.clone(); public Object clone() throws CloneNotSupportedException {
Dependency dependencyClone = (Dependency) this.dependency.clone(); return super.clone();
dependentClone.setDependency(dependencyClone); }
return dependentClone;
} public Object deepClone() throws CloneNotSupportedException {
Main dependentClone = (Main) super.clone();
public Dependency getDependency() { Dependency dependencyClone = (Dependency) this.dependency.clone();
return dependency; 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;
}
} }

View File

@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<configuration> <configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT"
<encoder> class="ch.qos.logback.core.ConsoleAppender">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n <encoder>
</pattern> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</encoder> </pattern>
</appender> </encoder>
</appender>
<root level="INFO"> <root level="INFO">
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>
</configuration> </configuration>

View File

@ -5,23 +5,24 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class ShallowDeepCopyUnitTest { 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
@Test public void givenAnObjectAndADependency_whenShallowClonedAndDepencyChanges_ThenDepencyHasNewValue() throws Exception {
public void givenAnObjectAndADependency_whenDeepClonedAndDepencyChanges_ThenDepencyKeepsOldValue() throws Exception { Dependency dependency = new Dependency(13);
Dependency dependency = new Dependency(13); Main main = new Main(dependency);
Main main = new Main(dependency); Main shallowClone = (Main) main.clone();
Main deepClone = (Main) main.deepClone(); dependency.setValue(17);
dependency.setValue(17); assertEquals(17, shallowClone.getDependency()
assertEquals(13, deepClone.getDependency().getValue()); .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());
}
} }