From 0c5441380750ef523d88d39469af1102d4e2d711 Mon Sep 17 00:00:00 2001 From: "Kent@lhind.hp.g5" Date: Fri, 22 Jan 2021 19:56:18 +0100 Subject: [PATCH] BAEL-2517 --- .../core-java-lang-oop-generics/pom.xml | 28 ++++++++++++ .../UncheckedConversion.java | 45 +++++++++++++++++++ .../UncheckedConversionUnitTest.java | 39 ++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/uncheckedconversion/UncheckedConversion.java create mode 100644 core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/uncheckedconversion/UncheckedConversionUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-generics/pom.xml b/core-java-modules/core-java-lang-oop-generics/pom.xml index 65a0aeac59..1a538edac9 100644 --- a/core-java-modules/core-java-lang-oop-generics/pom.xml +++ b/core-java-modules/core-java-lang-oop-generics/pom.xml @@ -13,4 +13,32 @@ core-java-lang-oop-generics jar + + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + + + 1.8 + 1.8 + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/uncheckedconversion/UncheckedConversion.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/uncheckedconversion/UncheckedConversion.java new file mode 100644 index 0000000000..9ad4a92077 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/uncheckedconversion/UncheckedConversion.java @@ -0,0 +1,45 @@ +package com.baeldung.uncheckedconversion; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.List; + +public class UncheckedConversion { + public static List getRawList() { + List result = new ArrayList(); + result.add("I am the 1st String."); + result.add("I am the 2nd String."); + result.add("I am the 3rd String."); + return result; + } + + public static List getRawListWithMixedTypes() { + List result = new ArrayList(); + result.add("I am the 1st String."); + result.add("I am the 2nd String."); + result.add("I am the 3rd String."); + result.add(new Date()); + return result; + } + + public static List castList(Class clazz, Collection rawCollection) { + List result = new ArrayList<>(rawCollection.size()); + for (Object o : rawCollection) { + try { + result.add(clazz.cast(o)); + } catch (ClassCastException e) { + // log the exception or other error handling + } + } + return result; + } + + public static List castList2(Class clazz, Collection rawCollection) throws ClassCastException { + List result = new ArrayList<>(rawCollection.size()); + for (Object o : rawCollection) { + result.add(clazz.cast(o)); + } + return result; + } +} diff --git a/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/uncheckedconversion/UncheckedConversionUnitTest.java b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/uncheckedconversion/UncheckedConversionUnitTest.java new file mode 100644 index 0000000000..37b9a878d3 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/uncheckedconversion/UncheckedConversionUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.uncheckedconversion; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class UncheckedConversionUnitTest { + + @Test + public void givenRawList_whenAssignToTypedList_shouldHaveCompilerWarning() { + List fromRawList = UncheckedConversion.getRawList(); + Assert.assertEquals(3, fromRawList.size()); + Assert.assertEquals("I am the 1st String.", fromRawList.get(0)); + } + + @Test(expected = ClassCastException.class) + public void givenRawList_whenListHasMixedType_shouldThrowClassCastException() { + List fromRawList = UncheckedConversion.getRawListWithMixedTypes(); + Assert.assertEquals(4, fromRawList.size()); + Assert.assertFalse(fromRawList.get(3).endsWith("String.")); + } + + @Test + public void givenRawList_whenAssignToTypedListAfterCallingCastList_shouldOnlyHaveElementsWithExpectedType() { + List rawList = UncheckedConversion.getRawListWithMixedTypes(); + List strList = UncheckedConversion.castList(String.class, rawList); + Assert.assertEquals(4, rawList.size()); + Assert.assertEquals("One element with the wrong type has been filtered out.", 3, strList.size()); + Assert.assertTrue(strList.stream().allMatch(el -> el.endsWith("String."))); + } + + @Test(expected = ClassCastException.class) + public void givenRawListWithWrongType_whenAssignToTypedListAfterCallingCastList2_shouldThrowException() { + List rawList = UncheckedConversion.getRawListWithMixedTypes(); + UncheckedConversion.castList2(String.class, rawList); + } + +}