From fa34902b702d98b542bd30207e9b11479d5af23e Mon Sep 17 00:00:00 2001 From: mcasari Date: Sat, 19 Aug 2023 23:42:04 +0200 Subject: [PATCH 1/9] shallow vs deep copy first commit --- shallow-deep-copy/README.md | 7 +++++ shallow-deep-copy/pom.xml | 16 ++++++++++ .../baeldung/shallowdeepcopy/Dependency.java | 23 ++++++++++++++ .../com/baeldung/shallowdeepcopy/Main.java | 31 +++++++++++++++++++ .../src/main/resources/logback.xml | 13 ++++++++ .../ShallowDeepCopyUnitTest.java | 27 ++++++++++++++++ 6 files changed, 117 insertions(+) create mode 100644 shallow-deep-copy/README.md create mode 100644 shallow-deep-copy/pom.xml create mode 100644 shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Dependency.java create mode 100644 shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Main.java create mode 100644 shallow-deep-copy/src/main/resources/logback.xml create mode 100644 shallow-deep-copy/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyUnitTest.java diff --git a/shallow-deep-copy/README.md b/shallow-deep-copy/README.md new file mode 100644 index 0000000000..e1a426a138 --- /dev/null +++ b/shallow-deep-copy/README.md @@ -0,0 +1,7 @@ +## Shallow vs Deep Copy + +This module contains an article about shallow vs deep copy of Java objects + +### Relevant articles: + +- [Creating a deep vs shallow copy of an object in Java](https://drafts.baeldung.com/?p=172294&preview=true) diff --git a/shallow-deep-copy/pom.xml b/shallow-deep-copy/pom.xml new file mode 100644 index 0000000000..01c1430f28 --- /dev/null +++ b/shallow-deep-copy/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + shallow-deep-copy + shallow-deep-copy + jar + + + 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 new file mode 100644 index 0000000000..a5da5fc163 --- /dev/null +++ b/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Dependency.java @@ -0,0 +1,23 @@ +package com.baeldung.shallowdeepcopy; + +public class Dependency implements Cloneable { + private int value = 13; + + public Dependency(int value) { + super(); + this.value = value; + } + + public int getValue() { + return value; + } + + 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 new file mode 100644 index 0000000000..fb24007162 --- /dev/null +++ b/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Main.java @@ -0,0 +1,31 @@ +package com.baeldung.shallowdeepcopy; + +public class Main implements Cloneable { + private Dependency dependency; + + public Main(Dependency dependency) { + super(); + this.dependency = 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; + } + +} diff --git a/shallow-deep-copy/src/main/resources/logback.xml b/shallow-deep-copy/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/shallow-deep-copy/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %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 new file mode 100644 index 0000000000..321c2df327 --- /dev/null +++ b/shallow-deep-copy/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.shallowdeepcopy; + +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()); + } +} From 8a6e3f233ec0591fe20d82fcf00c80cb8164b577 Mon Sep 17 00:00:00 2001 From: mcasari Date: Tue, 22 Aug 2023 19:49:54 +0200 Subject: [PATCH 2/9] Format content with baeldung formatter --- shallow-deep-copy/pom.xml | 22 ++++----- .../baeldung/shallowdeepcopy/Dependency.java | 32 ++++++------- .../com/baeldung/shallowdeepcopy/Main.java | 48 +++++++++---------- .../src/main/resources/logback.xml | 19 ++++---- .../ShallowDeepCopyUnitTest.java | 37 +++++++------- 5 files changed, 80 insertions(+), 78 deletions(-) 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()); + } } From 4ce8b0e70a341783254dcbb718841742f4b47409 Mon Sep 17 00:00:00 2001 From: mcasari Date: Tue, 19 Sep 2023 21:53:05 +0200 Subject: [PATCH 3/9] Test cases for float to BigDecimal conversion - first commit --- convert-float-to-bigdecimal/README.md | 7 +++ convert-float-to-bigdecimal/pom.xml | 16 +++++++ .../src/main/resources/logback.xml | 14 ++++++ .../ConvertFloatToBigDecimalUnitTest.java | 48 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 convert-float-to-bigdecimal/README.md create mode 100644 convert-float-to-bigdecimal/pom.xml create mode 100644 convert-float-to-bigdecimal/src/main/resources/logback.xml create mode 100644 convert-float-to-bigdecimal/src/test/java/com/baeldung/convertfloattobigdecimal/ConvertFloatToBigDecimalUnitTest.java diff --git a/convert-float-to-bigdecimal/README.md b/convert-float-to-bigdecimal/README.md new file mode 100644 index 0000000000..e01c981831 --- /dev/null +++ b/convert-float-to-bigdecimal/README.md @@ -0,0 +1,7 @@ +## Shallow vs Deep Copy + +This module contains an article about converting float to BigDecimal in Java + +### Relevant articles: + +- [Converting from Float to BigDecimal in Java](https://drafts.baeldung.com/?p=175240&preview=true) diff --git a/convert-float-to-bigdecimal/pom.xml b/convert-float-to-bigdecimal/pom.xml new file mode 100644 index 0000000000..8fa20b300f --- /dev/null +++ b/convert-float-to-bigdecimal/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + convert-float-to-bigdecimal + convert-float-to-bigdecimal + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + \ No newline at end of file diff --git a/convert-float-to-bigdecimal/src/main/resources/logback.xml b/convert-float-to-bigdecimal/src/main/resources/logback.xml new file mode 100644 index 0000000000..830ab22727 --- /dev/null +++ b/convert-float-to-bigdecimal/src/main/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/convert-float-to-bigdecimal/src/test/java/com/baeldung/convertfloattobigdecimal/ConvertFloatToBigDecimalUnitTest.java b/convert-float-to-bigdecimal/src/test/java/com/baeldung/convertfloattobigdecimal/ConvertFloatToBigDecimalUnitTest.java new file mode 100644 index 0000000000..c43d2ab87a --- /dev/null +++ b/convert-float-to-bigdecimal/src/test/java/com/baeldung/convertfloattobigdecimal/ConvertFloatToBigDecimalUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.convertfloattobigdecimal; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.math.BigDecimal; + +import org.junit.jupiter.api.Test; + +class ConvertFloatToBigDecimalUnitTest { + + @Test + public void whenFloatComparedWithDifferentValues_thenCouldMatch() { + assertNotEquals(1.1f, 1.09f); + assertEquals(1.1f, 1.09999999f); + } + + @Test + public void whenCreatedFromFloat_thenCouldNotMatch() { + float floatToConvert = 0.5f; + BigDecimal bdFromFloat = new BigDecimal(floatToConvert); + assertEquals("0.5", bdFromFloat.toString()); + floatToConvert = 1.1f; + bdFromFloat = new BigDecimal(floatToConvert); + assertNotEquals("1.1", bdFromFloat.toString()); + } + + @Test + public void whenCreatedFromString_thenMatches() { + String floatValue = Float.toString(1.1f); + BigDecimal bdFromString = new BigDecimal(floatValue); + assertEquals("1.1", bdFromString.toString()); + } + + @Test + public void whenCreatedByValueOfAndIsFloat_thenDoesNotMatch() { + float floatToConvert = 1.1f; + BigDecimal bdByValueOf = BigDecimal.valueOf(floatToConvert); + assertNotEquals("1.1", bdByValueOf.toString()); + } + + @Test + public void whenCreatedByValueOfAndIsDouble_thenMatches() { + double doubleToConvert = 1.1d; + BigDecimal bdByValueOf = BigDecimal.valueOf(doubleToConvert); + assertEquals("1.1", bdByValueOf.toString()); + } +} From a7940d9658d262bf3671c99753b2d4ab8eead674 Mon Sep 17 00:00:00 2001 From: mcasari Date: Thu, 21 Sep 2023 20:28:15 +0200 Subject: [PATCH 4/9] BAEL-6846 - Convert from float to BigDecimal --- convert-float-to-bigdecimal/README.md | 7 ----- convert-float-to-bigdecimal/pom.xml | 16 ---------- .../src/main/resources/logback.xml | 14 --------- .../FloatToBigDecimalUnitTest.java | 24 ++++++++++---- shallow-deep-copy/README.md | 7 ----- shallow-deep-copy/pom.xml | 16 ---------- .../baeldung/shallowdeepcopy/Dependency.java | 23 -------------- .../com/baeldung/shallowdeepcopy/Main.java | 31 ------------------- .../src/main/resources/logback.xml | 14 --------- .../ShallowDeepCopyUnitTest.java | 28 ----------------- 10 files changed, 18 insertions(+), 162 deletions(-) delete mode 100644 convert-float-to-bigdecimal/README.md delete mode 100644 convert-float-to-bigdecimal/pom.xml delete mode 100644 convert-float-to-bigdecimal/src/main/resources/logback.xml rename convert-float-to-bigdecimal/src/test/java/com/baeldung/convertfloattobigdecimal/ConvertFloatToBigDecimalUnitTest.java => core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java (69%) delete mode 100644 shallow-deep-copy/README.md delete mode 100644 shallow-deep-copy/pom.xml delete mode 100644 shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Dependency.java delete mode 100644 shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Main.java delete mode 100644 shallow-deep-copy/src/main/resources/logback.xml delete mode 100644 shallow-deep-copy/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyUnitTest.java diff --git a/convert-float-to-bigdecimal/README.md b/convert-float-to-bigdecimal/README.md deleted file mode 100644 index e01c981831..0000000000 --- a/convert-float-to-bigdecimal/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## Shallow vs Deep Copy - -This module contains an article about converting float to BigDecimal in Java - -### Relevant articles: - -- [Converting from Float to BigDecimal in Java](https://drafts.baeldung.com/?p=175240&preview=true) diff --git a/convert-float-to-bigdecimal/pom.xml b/convert-float-to-bigdecimal/pom.xml deleted file mode 100644 index 8fa20b300f..0000000000 --- a/convert-float-to-bigdecimal/pom.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - 4.0.0 - convert-float-to-bigdecimal - convert-float-to-bigdecimal - jar - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - \ No newline at end of file diff --git a/convert-float-to-bigdecimal/src/main/resources/logback.xml b/convert-float-to-bigdecimal/src/main/resources/logback.xml deleted file mode 100644 index 830ab22727..0000000000 --- a/convert-float-to-bigdecimal/src/main/resources/logback.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/convert-float-to-bigdecimal/src/test/java/com/baeldung/convertfloattobigdecimal/ConvertFloatToBigDecimalUnitTest.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java similarity index 69% rename from convert-float-to-bigdecimal/src/test/java/com/baeldung/convertfloattobigdecimal/ConvertFloatToBigDecimalUnitTest.java rename to core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java index c43d2ab87a..20422edbd7 100644 --- a/convert-float-to-bigdecimal/src/test/java/com/baeldung/convertfloattobigdecimal/ConvertFloatToBigDecimalUnitTest.java +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertfloattobigdecimal; +package com.baeldung.floattobigdecimal; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -7,7 +7,7 @@ import java.math.BigDecimal; import org.junit.jupiter.api.Test; -class ConvertFloatToBigDecimalUnitTest { +class FloatToBigDecimalUnitTest { @Test public void whenFloatComparedWithDifferentValues_thenCouldMatch() { @@ -16,12 +16,16 @@ class ConvertFloatToBigDecimalUnitTest { } @Test - public void whenCreatedFromFloat_thenCouldNotMatch() { + public void whenCreatedFromCertainFloatValues_thenMatches() { float floatToConvert = 0.5f; BigDecimal bdFromFloat = new BigDecimal(floatToConvert); assertEquals("0.5", bdFromFloat.toString()); - floatToConvert = 1.1f; - bdFromFloat = new BigDecimal(floatToConvert); + } + + @Test + public void whenCreatedFromCertainFloatValues_thenDoesNotMatch() { + float floatToConvert = 1.1f; + BigDecimal bdFromFloat = new BigDecimal(floatToConvert); assertNotEquals("1.1", bdFromFloat.toString()); } @@ -39,10 +43,18 @@ class ConvertFloatToBigDecimalUnitTest { assertNotEquals("1.1", bdByValueOf.toString()); } + @Test + public void whenFloatCastToDouble_thenGotADifferentNumber() { + float floatToConvert = 1.1f; + double doubleCast = floatToConvert; + assertEquals("1.100000023841858", Double.toString(doubleCast)); + } + @Test public void whenCreatedByValueOfAndIsDouble_thenMatches() { double doubleToConvert = 1.1d; BigDecimal bdByValueOf = BigDecimal.valueOf(doubleToConvert); assertEquals("1.1", bdByValueOf.toString()); } -} + +} \ No newline at end of file diff --git a/shallow-deep-copy/README.md b/shallow-deep-copy/README.md deleted file mode 100644 index e1a426a138..0000000000 --- a/shallow-deep-copy/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## Shallow vs Deep Copy - -This module contains an article about shallow vs deep copy of Java objects - -### Relevant articles: - -- [Creating a deep vs shallow copy of an object in Java](https://drafts.baeldung.com/?p=172294&preview=true) diff --git a/shallow-deep-copy/pom.xml b/shallow-deep-copy/pom.xml deleted file mode 100644 index 323f720df3..0000000000 --- a/shallow-deep-copy/pom.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - 4.0.0 - shallow-deep-copy - shallow-deep-copy - jar - - - 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 deleted file mode 100644 index 2550acd268..0000000000 --- a/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Dependency.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.shallowdeepcopy; - -public class Dependency implements Cloneable { - private int value = 13; - - public Dependency(int value) { - super(); - this.value = value; - } - - public int getValue() { - return value; - } - - 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 deleted file mode 100644 index 33d565bb10..0000000000 --- a/shallow-deep-copy/src/main/java/com/baeldung/shallowdeepcopy/Main.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.shallowdeepcopy; - -public class Main implements Cloneable { - private Dependency dependency; - - public Main(Dependency dependency) { - super(); - this.dependency = 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; - } - -} diff --git a/shallow-deep-copy/src/main/resources/logback.xml b/shallow-deep-copy/src/main/resources/logback.xml deleted file mode 100644 index 830ab22727..0000000000 --- a/shallow-deep-copy/src/main/resources/logback.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - %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 deleted file mode 100644 index f75bbc040b..0000000000 --- a/shallow-deep-copy/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyUnitTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.shallowdeepcopy; - -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()); - } -} From c56bc04f95f7b9c9929d32f5c02ee359d8535265 Mon Sep 17 00:00:00 2001 From: mcasari Date: Sat, 23 Sep 2023 17:02:20 +0200 Subject: [PATCH 5/9] BAEL-6846 - change assertNotEquals to assertEquals --- .../baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java index 20422edbd7..2d6d1ab5d0 100644 --- a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java @@ -26,7 +26,7 @@ class FloatToBigDecimalUnitTest { public void whenCreatedFromCertainFloatValues_thenDoesNotMatch() { float floatToConvert = 1.1f; BigDecimal bdFromFloat = new BigDecimal(floatToConvert); - assertNotEquals("1.1", bdFromFloat.toString()); + assertEquals("1.10000002384185791015625", bdFromFloat.toString()); } @Test @@ -40,7 +40,7 @@ class FloatToBigDecimalUnitTest { public void whenCreatedByValueOfAndIsFloat_thenDoesNotMatch() { float floatToConvert = 1.1f; BigDecimal bdByValueOf = BigDecimal.valueOf(floatToConvert); - assertNotEquals("1.1", bdByValueOf.toString()); + assertEquals("1.100000023841858", bdByValueOf.toString()); } @Test From 39c353c6aeec68b85418da16a3907a98ef9869ea Mon Sep 17 00:00:00 2001 From: mcasari Date: Sun, 1 Oct 2023 21:58:55 +0200 Subject: [PATCH 6/9] BAEL-6846 - updates due to further discussion about float representation --- .../FloatToBigDecimalUnitTest.java | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java index 2d6d1ab5d0..8895c01575 100644 --- a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java @@ -16,45 +16,44 @@ class FloatToBigDecimalUnitTest { } @Test - public void whenCreatedFromCertainFloatValues_thenMatches() { - float floatToConvert = 0.5f; - BigDecimal bdFromFloat = new BigDecimal(floatToConvert); - assertEquals("0.5", bdFromFloat.toString()); - } - - @Test - public void whenCreatedFromCertainFloatValues_thenDoesNotMatch() { + public void whenCreatedFromFloat_thenMatchesInternallyStoredValue() { float floatToConvert = 1.1f; BigDecimal bdFromFloat = new BigDecimal(floatToConvert); assertEquals("1.10000002384185791015625", bdFromFloat.toString()); } @Test - public void whenCreatedFromString_thenMatches() { - String floatValue = Float.toString(1.1f); - BigDecimal bdFromString = new BigDecimal(floatValue); + public void whenCreatedFromString_thenPreservesTheOriginal() { + BigDecimal bdFromString = new BigDecimal("1.1"); assertEquals("1.1", bdFromString.toString()); } @Test - public void whenCreatedByValueOfAndIsFloat_thenDoesNotMatch() { - float floatToConvert = 1.1f; - BigDecimal bdByValueOf = BigDecimal.valueOf(floatToConvert); - assertEquals("1.100000023841858", bdByValueOf.toString()); + public void whenCreatedFromFloatConvertedToString_thenFloatInternalValueGetsTruncated() { + String floatValue = Float.toString(1.1f); + BigDecimal bdFromString = new BigDecimal(floatValue); + assertEquals("1.1", floatValue); + assertEquals("1.1", bdFromString.toString()); } @Test - public void whenFloatCastToDouble_thenGotADifferentNumber() { - float floatToConvert = 1.1f; - double doubleCast = floatToConvert; - assertEquals("1.100000023841858", Double.toString(doubleCast)); + public void whenFloatConvertedToString_thenGetsTruncated() { + String floatValue = Float.toString(1.10000002384185791015625f); + assertEquals("1.1", floatValue); } @Test - public void whenCreatedByValueOfAndIsDouble_thenMatches() { - double doubleToConvert = 1.1d; - BigDecimal bdByValueOf = BigDecimal.valueOf(doubleToConvert); - assertEquals("1.1", bdByValueOf.toString()); + public void whenCreatedByValueOf_thenFloatValueGetsTruncated() { + assertEquals("1.100000023841858", BigDecimal.valueOf(1.1f) + .toString()); + assertEquals("1.100000023841858", BigDecimal.valueOf(1.10000002384185791015625f) + .toString()); + } + + @Test + public void whenDoubleConvertsFloatToString_thenFloatValueGetsTruncated() { + assertEquals("1.100000023841858", Double.toString(1.1f)); + assertEquals("1.100000023841858", Double.toString(1.10000002384185791015625f)); } } \ No newline at end of file From 6f085f068a1874b555f971b2109ff88ad78c07e6 Mon Sep 17 00:00:00 2001 From: mcasari Date: Tue, 3 Oct 2023 18:34:40 +0200 Subject: [PATCH 7/9] BAEL-6846 - minor code format fix --- .../floattobigdecimal/FloatToBigDecimalUnitTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java index 8895c01575..fe5dc360e4 100644 --- a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java @@ -44,10 +44,8 @@ class FloatToBigDecimalUnitTest { @Test public void whenCreatedByValueOf_thenFloatValueGetsTruncated() { - assertEquals("1.100000023841858", BigDecimal.valueOf(1.1f) - .toString()); - assertEquals("1.100000023841858", BigDecimal.valueOf(1.10000002384185791015625f) - .toString()); + assertEquals("1.100000023841858", BigDecimal.valueOf(1.1f).toString()); + assertEquals("1.100000023841858", BigDecimal.valueOf(1.10000002384185791015625f).toString()); } @Test From 7e1d5991e61a969ec922f5a7182096ee76002d0f Mon Sep 17 00:00:00 2001 From: mcasari Date: Tue, 3 Oct 2023 23:41:11 +0200 Subject: [PATCH 8/9] BAEL-6846 - tests revision due to article updates --- .../FloatToBigDecimalUnitTest.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java index fe5dc360e4..9d125f557d 100644 --- a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java @@ -17,40 +17,32 @@ class FloatToBigDecimalUnitTest { @Test public void whenCreatedFromFloat_thenMatchesInternallyStoredValue() { - float floatToConvert = 1.1f; + float floatToConvert = 1.10000002384185791015625f; BigDecimal bdFromFloat = new BigDecimal(floatToConvert); assertEquals("1.10000002384185791015625", bdFromFloat.toString()); } @Test public void whenCreatedFromString_thenPreservesTheOriginal() { - BigDecimal bdFromString = new BigDecimal("1.1"); - assertEquals("1.1", bdFromString.toString()); + BigDecimal bdFromString = new BigDecimal("1.10000002384185791015625"); + assertEquals("1.10000002384185791015625", bdFromString.toString()); } @Test public void whenCreatedFromFloatConvertedToString_thenFloatInternalValueGetsTruncated() { - String floatValue = Float.toString(1.1f); + String floatValue = Float.toString(1.10000002384185791015625f); BigDecimal bdFromString = new BigDecimal(floatValue); assertEquals("1.1", floatValue); assertEquals("1.1", bdFromString.toString()); } - @Test - public void whenFloatConvertedToString_thenGetsTruncated() { - String floatValue = Float.toString(1.10000002384185791015625f); - assertEquals("1.1", floatValue); - } - @Test public void whenCreatedByValueOf_thenFloatValueGetsTruncated() { - assertEquals("1.100000023841858", BigDecimal.valueOf(1.1f).toString()); assertEquals("1.100000023841858", BigDecimal.valueOf(1.10000002384185791015625f).toString()); } @Test public void whenDoubleConvertsFloatToString_thenFloatValueGetsTruncated() { - assertEquals("1.100000023841858", Double.toString(1.1f)); assertEquals("1.100000023841858", Double.toString(1.10000002384185791015625f)); } From 68fd94288d141c87381ca9deeca3284dfd54cce9 Mon Sep 17 00:00:00 2001 From: mcasari Date: Fri, 6 Oct 2023 21:57:56 +0200 Subject: [PATCH 9/9] change string constructor test case in simpler form --- .../baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java index 9d125f557d..f783d00976 100644 --- a/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java +++ b/core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/floattobigdecimal/FloatToBigDecimalUnitTest.java @@ -24,8 +24,8 @@ class FloatToBigDecimalUnitTest { @Test public void whenCreatedFromString_thenPreservesTheOriginal() { - BigDecimal bdFromString = new BigDecimal("1.10000002384185791015625"); - assertEquals("1.10000002384185791015625", bdFromString.toString()); + BigDecimal bdFromString = new BigDecimal("1.1"); + assertEquals("1.1", bdFromString.toString()); } @Test