Author: tchemit Date: 2010-02-21 20:36:18 +0100 (Sun, 21 Feb 2010) New Revision: 1765 Modified: trunk/src/main/java/org/nuiton/util/ObjectUtil.java trunk/src/test/java/org/nuiton/util/ObjectUtilTest.java Log: add GetNullValue and isNullValue methods in Objectutil to deal thim primitive types nullity (as their default value) Modified: trunk/src/main/java/org/nuiton/util/ObjectUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/ObjectUtil.java 2010-02-21 18:16:09 UTC (rev 1764) +++ trunk/src/main/java/org/nuiton/util/ObjectUtil.java 2010-02-21 19:36:18 UTC (rev 1765) @@ -33,6 +33,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import org.apache.commons.beanutils.MethodUtils; import static org.nuiton.i18n.I18n._; import org.apache.commons.logging.Log; @@ -50,6 +51,13 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private Log log = LogFactory.getLog(ObjectUtil.class); + protected static final Integer ZERO = 0; + protected static final Character ZEROC = (char)0; + protected static final Float ZEROF = 0f; + protected static final Long ZEROL = 0l; + protected static final Double ZEROD = 0.; + protected static final Byte ZEROB = 0; + /** * ObjectUtil constructor * private because of this class is a static class : nobody @@ -398,5 +406,111 @@ return b?Boolean.TRUE:Boolean.FALSE; } + /** + * Obtains the null value for the given type (works too with primitive + * types). + * + * @param type the type to test + * @return the {@code null} value or default value for primitive types + * @since 1.1.5 + */ + public static Object getNullValue(Class<?> type) { + if (type == null) { + throw new NullPointerException("parameter 'type' can not be null"); + } + if (type.isPrimitive()) { + type = MethodUtils.getPrimitiveWrapper(type); + if (Boolean.class.isAssignableFrom(type)) { + return Boolean.FALSE; + } + if (Integer.class.isAssignableFrom(type)) { + return ZERO; + } + if (Character.class.isAssignableFrom(type)) { + return ZEROC; + } + if (Float.class.isAssignableFrom(type)) { + return ZEROF; + } + if (Long.class.isAssignableFrom(type)) { + return ZEROL; + } + if (Double.class.isAssignableFrom(type)) { + return ZEROD; + } + if (Byte.class.isAssignableFrom(type)) { + return ZEROB; + } + } + return null; + } + + /** + * Tests if the given value is null according to default value for + * primitive types if nedded. + * + * @param value the value to test + * @return {@code true} if value is null or default value on a primitive + * @since 1.1.5 + */ + public static boolean isNullValue(Object value) { + if (value == null) { + return true; + } + Class<?> type = value.getClass(); + + //FIXME-TC20100212 : this case can not be, due to auto-boxing mecanism + if (type.isPrimitive()) { + type = MethodUtils.getPrimitiveWrapper(type); + + if (Boolean.class.isAssignableFrom(type)) { + return Boolean.FALSE.equals(value); + } + if (Integer.class.isAssignableFrom(type)) { + return ZERO.equals(value); + } + if (Character.class.isAssignableFrom(type)) { + return ZEROC.equals(value); + } + if (Float.class.isAssignableFrom(type)) { + return ZEROF.equals(value); + } + if (Long.class.isAssignableFrom(type)) { + return ZEROL.equals(value); + } + if (Double.class.isAssignableFrom(type)) { + return ZEROD.equals(value); + } + if (Byte.class.isAssignableFrom(type)) { + return ZEROB.equals(value); + } + } + return false; + } + + public static boolean isNullValue(boolean value) { + return Boolean.FALSE.equals(value); + } + + public static boolean isNullValue(byte value) { + return value == ZEROB; + } + + public static boolean isNullValue(int value) { + return value == ZEROB; + } + + public static boolean isNullValue(char value) { + return value == ZEROC; + } + + public static boolean isNullValue(float value) { + return value == ZEROF; + } + + public static boolean isNullValue(double value) { + return value == ZEROD; + } + } // ObjectUtil Modified: trunk/src/test/java/org/nuiton/util/ObjectUtilTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/ObjectUtilTest.java 2010-02-21 18:16:09 UTC (rev 1764) +++ trunk/src/test/java/org/nuiton/util/ObjectUtilTest.java 2010-02-21 19:36:18 UTC (rev 1765) @@ -35,16 +35,18 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; /** * @author poussin * */ -public class ObjectUtilTest extends TestCase { +public class ObjectUtilTest extends Assert { - public void testCall() throws Exception { + @Test + public void testCall() throws Exception { Dummy dummy = new Dummy(); List<Method> methods = ObjectUtil.getMethod(Dummy.class, "setfile", true); @@ -65,7 +67,8 @@ Arrays.asList(dummy.getAllFile())); } - public void testCreate() throws Exception { +@Test + public void testCreate() throws Exception { Object o = ObjectUtil.create("java.lang.StringBuffer"); assertTrue(o != null); assertTrue(o instanceof StringBuffer); @@ -78,7 +81,51 @@ assertEquals(new File("/tmp/fileTest"), dummy.getFile()); } + @Test + public void testGetNullValue() throws Exception { + try { + assertNull(ObjectUtil.getNullValue(null)); + fail(); + } catch (Exception e) { + assertTrue(true); + } + + assertEquals(false, ObjectUtil.getNullValue(boolean.class)); + assertEquals(0, ObjectUtil.getNullValue(int.class)); + assertEquals((char)0, ObjectUtil.getNullValue(char.class)); + assertEquals(0f, ObjectUtil.getNullValue(float.class)); + assertEquals(0d, ObjectUtil.getNullValue(double.class)); + assertEquals((byte)0, ObjectUtil.getNullValue(byte.class)); + + assertNull(ObjectUtil.getNullValue(Boolean.class)); + assertNull(ObjectUtil.getNullValue(Integer.class)); + assertNull(ObjectUtil.getNullValue(Character.class)); + assertNull(ObjectUtil.getNullValue(Float.class)); + assertNull(ObjectUtil.getNullValue(Double.class)); + assertNull(ObjectUtil.getNullValue(Byte.class)); + } + + @Test + public void testIsNullValue() throws Exception { + + assertTrue(ObjectUtil.isNullValue(null)); + + assertTrue(ObjectUtil.isNullValue(false)); + assertTrue(ObjectUtil.isNullValue(0)); + assertTrue(ObjectUtil.isNullValue((char) 0)); + assertTrue(ObjectUtil.isNullValue(0f)); + assertTrue(ObjectUtil.isNullValue(0d)); + assertTrue(ObjectUtil.isNullValue((byte)0)); + + assertFalse(ObjectUtil.isNullValue(Boolean.FALSE)); + assertFalse(ObjectUtil.isNullValue(Integer.valueOf(0))); + assertFalse(ObjectUtil.isNullValue(Character.valueOf((char) 0))); + assertFalse(ObjectUtil.isNullValue(Float.valueOf(0))); + assertFalse(ObjectUtil.isNullValue(Double.valueOf(0))); + assertFalse(ObjectUtil.isNullValue(Byte.valueOf((byte)0))); + } + static public class Dummy { String name;