use utility class directly
This commit is contained in:
parent
ae479954f4
commit
33841c2034
|
@ -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<?>, 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue