Add primitive type check method and unit tests
This commit is contained in:
parent
4128525f00
commit
45be1b6045
|
@ -12,4 +12,12 @@
|
|||
<artifactId>core-java-lang-oop-types</artifactId>
|
||||
<name>core-java-lang-oop-types</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,38 @@
|
|||
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;
|
||||
static {
|
||||
WRAPPER_TYPE_MAP = new HashMap<Class<?>, Class<?>>(16);
|
||||
WRAPPER_TYPE_MAP.put(Integer.class, int.class);
|
||||
WRAPPER_TYPE_MAP.put(Byte.class, byte.class);
|
||||
WRAPPER_TYPE_MAP.put(Character.class, char.class);
|
||||
WRAPPER_TYPE_MAP.put(Boolean.class, boolean.class);
|
||||
WRAPPER_TYPE_MAP.put(Double.class, double.class);
|
||||
WRAPPER_TYPE_MAP.put(Float.class, float.class);
|
||||
WRAPPER_TYPE_MAP.put(Long.class, long.class);
|
||||
WRAPPER_TYPE_MAP.put(Short.class, short.class);
|
||||
WRAPPER_TYPE_MAP.put(Void.class, void.class);
|
||||
}
|
||||
|
||||
public boolean isPrimitiveTypeByCommansLang(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());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
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 PrimitiveTypeUtilTest {
|
||||
|
||||
private PrimitiveTypeUtil primitiveTypeUtil;
|
||||
private boolean booleanVal = false;
|
||||
private Long longWrapper = 1L;
|
||||
private String nonPrimitiveVal = "Test";
|
||||
|
||||
@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 givenObjectWhenCheckWithCommansLangShouldValidate() {
|
||||
assertTrue(primitiveTypeUtil.isPrimitiveTypeByCommansLang(booleanVal));
|
||||
assertTrue(primitiveTypeUtil.isPrimitiveTypeByCommansLang(longWrapper));
|
||||
assertFalse(primitiveTypeUtil.isPrimitiveTypeByCommansLang(nonPrimitiveVal));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPrimitiveOrWrapperWhenCheckWithCustomMethodShouldReturnTrue() {
|
||||
assertTrue(primitiveTypeUtil.isPrimitiveType(booleanVal));
|
||||
assertTrue(primitiveTypeUtil.isPrimitiveType(longWrapper));
|
||||
assertFalse(primitiveTypeUtil.isPrimitiveType(nonPrimitiveVal));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue