From 33841c2034a82feb31714f986d4cb3441f56ceda Mon Sep 17 00:00:00 2001 From: joe zhang Date: Mon, 3 Aug 2020 22:52:22 +0800 Subject: [PATCH] use utility class directly --- .../primitivetype/PrimitiveTypeUtil.java | 16 +------- .../primitivetype/PrimitiveTypeUnitTest.java | 40 ++++++++++++++++++ .../PrimitiveTypeUtilUnitTest.java | 41 ------------------- 3 files changed, 42 insertions(+), 55 deletions(-) create mode 100644 core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/primitivetype/PrimitiveTypeUnitTest.java delete mode 100644 core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/primitivetype/PrimitiveTypeUtilUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/primitivetype/PrimitiveTypeUtil.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/primitivetype/PrimitiveTypeUtil.java index 1b2083b548..9607a0008f 100644 --- a/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/primitivetype/PrimitiveTypeUtil.java +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/primitivetype/PrimitiveTypeUtil.java @@ -3,10 +3,6 @@ package com.baeldung.primitivetype; import java.util.HashMap; import java.util.Map; -import org.apache.commons.lang3.ClassUtils; - -import com.google.common.primitives.Primitives; - public class PrimitiveTypeUtil { private static final Map, Class> WRAPPER_TYPE_MAP; @@ -23,16 +19,8 @@ public class PrimitiveTypeUtil { WRAPPER_TYPE_MAP.put(Void.class, void.class); } - public boolean isPrimitiveTypeByCommonsLang(Object source) { - return ClassUtils.isPrimitiveOrWrapper(source.getClass()); - } - - public boolean isPrimitiveTypeByGuava(Object source) { - return Primitives.isWrapperType(source.getClass()); - } - - public boolean isPrimitiveType(Object source) { - return WRAPPER_TYPE_MAP.containsKey(source.getClass()); + public boolean isPrimitiveType(Class sourceClass) { + return WRAPPER_TYPE_MAP.containsKey(sourceClass); } } diff --git a/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/primitivetype/PrimitiveTypeUnitTest.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/primitivetype/PrimitiveTypeUnitTest.java new file mode 100644 index 0000000000..5bbededbb3 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/primitivetype/PrimitiveTypeUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.primitivetype; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.apache.commons.lang3.ClassUtils; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.primitives.Primitives; + +public class PrimitiveTypeUnitTest { + + private PrimitiveTypeUtil primitiveTypeUtil; + + @Before + public void setup() { + primitiveTypeUtil = new PrimitiveTypeUtil(); + } + + @Test + public void givenAClass_whenCheckWithPrimitiveTypeUtil_thenShouldValidate() { + assertTrue(primitiveTypeUtil.isPrimitiveType(Boolean.class)); + assertFalse(primitiveTypeUtil.isPrimitiveType(String.class)); + } + + @Test + public void givenAClass_whenCheckWithCommonsLang_thenShouldValidate() { + assertTrue(ClassUtils.isPrimitiveOrWrapper(Boolean.class)); + assertTrue(ClassUtils.isPrimitiveOrWrapper(boolean.class)); + assertFalse(ClassUtils.isPrimitiveOrWrapper(String.class)); + } + + @Test + public void givenAClass_whenCheckWithGuava_thenShouldValidate() { + assertTrue(Primitives.isWrapperType(Boolean.class)); + assertFalse(Primitives.isWrapperType(String.class)); + assertFalse(Primitives.isWrapperType(boolean.class)); + } +} diff --git a/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/primitivetype/PrimitiveTypeUtilUnitTest.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/primitivetype/PrimitiveTypeUtilUnitTest.java deleted file mode 100644 index 5cd1b9f9d7..0000000000 --- a/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/primitivetype/PrimitiveTypeUtilUnitTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.primitivetype; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Before; -import org.junit.Test; - -public class PrimitiveTypeUtilUnitTest { - - private PrimitiveTypeUtil primitiveTypeUtil; - private boolean booleanVal = false; - private Long longWrapper = 1L; - private String nonPrimitiveVal = "non primitive string"; - - @Before - public void setup() { - primitiveTypeUtil = new PrimitiveTypeUtil(); - } - - @Test - public void givenObjectWhenCheckWithGuavaShouldValidate() { - assertTrue(primitiveTypeUtil.isPrimitiveTypeByGuava(booleanVal)); - assertTrue(primitiveTypeUtil.isPrimitiveTypeByGuava(longWrapper)); - assertFalse(primitiveTypeUtil.isPrimitiveTypeByGuava(nonPrimitiveVal)); - } - - @Test - public void givenObjectWhenCheckWithCommonsLangShouldValidate() { - assertTrue(primitiveTypeUtil.isPrimitiveTypeByCommonsLang(booleanVal)); - assertTrue(primitiveTypeUtil.isPrimitiveTypeByCommonsLang(longWrapper)); - assertFalse(primitiveTypeUtil.isPrimitiveTypeByCommonsLang(nonPrimitiveVal)); - } - - @Test - public void givenPrimitiveOrWrapperWhenCheckWithCustomMethodShouldReturnTrue() { - assertTrue(primitiveTypeUtil.isPrimitiveType(booleanVal)); - assertTrue(primitiveTypeUtil.isPrimitiveType(longWrapper)); - assertFalse(primitiveTypeUtil.isPrimitiveType(nonPrimitiveVal)); - } -}