Merge pull request #9794 from LeasyZhang/determin-primitive-objects
LeasyZhang/shiwangzhihe@gmail.com
This commit is contained in:
commit
53a869163c
|
@ -12,4 +12,12 @@
|
||||||
<artifactId>core-java-lang-oop-types</artifactId>
|
<artifactId>core-java-lang-oop-types</artifactId>
|
||||||
<name>core-java-lang-oop-types</name>
|
<name>core-java-lang-oop-types</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.primitivetype;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
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 static boolean isPrimitiveType(Object source) {
|
||||||
|
return WRAPPER_TYPE_MAP.containsKey(source.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.baeldung.primitivetype;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.ClassUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.google.common.primitives.Primitives;
|
||||||
|
|
||||||
|
public class PrimitiveTypeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAClass_whenCheckWithPrimitiveTypeUtil_thenShouldValidate() {
|
||||||
|
assertTrue(PrimitiveTypeUtil.isPrimitiveType(false));
|
||||||
|
assertTrue(PrimitiveTypeUtil.isPrimitiveType(1L));
|
||||||
|
assertFalse(PrimitiveTypeUtil.isPrimitiveType(StringUtils.EMPTY));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAClass_whenCheckWithCommonsLang_thenShouldValidate() {
|
||||||
|
assertTrue(ClassUtils.isPrimitiveOrWrapper(Boolean.FALSE.getClass()));
|
||||||
|
assertTrue(ClassUtils.isPrimitiveOrWrapper(boolean.class));
|
||||||
|
assertFalse(ClassUtils.isPrimitiveOrWrapper(StringUtils.EMPTY.getClass()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAClass_whenCheckWithGuava_thenShouldValidate() {
|
||||||
|
assertTrue(Primitives.isWrapperType(Boolean.FALSE.getClass()));
|
||||||
|
assertFalse(Primitives.isWrapperType(StringUtils.EMPTY.getClass()));
|
||||||
|
assertFalse(Primitives.isWrapperType(Boolean.TYPE.getClass()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue