diff --git a/src/java/org/apache/commons/lang/ObjectUtils.java b/src/java/org/apache/commons/lang/ObjectUtils.java index fb629b637..a57706f19 100644 --- a/src/java/org/apache/commons/lang/ObjectUtils.java +++ b/src/java/org/apache/commons/lang/ObjectUtils.java @@ -29,8 +29,9 @@ import java.io.Serializable; * @author Daniel Rall * @author Stephen Colebourne * @author Gary Gregory + * @author Mario Winterer * @since 1.0 - * @version $Id: ObjectUtils.java,v 1.23 2004/02/18 22:59:50 ggregory Exp $ + * @version $Id: ObjectUtils.java,v 1.24 2004/06/01 21:08:48 scolebourne Exp $ */ public class ObjectUtils { @@ -113,7 +114,24 @@ public class ObjectUtils { } return object1.equals(object2); } - + + /** + *

Gets the hash code of an object returning zero when the + * object is null.

+ * + *
+     * ObjectUtils.hashCode(null)   = 0
+     * ObjectUtils.hashCode(obj)    = obj.hashCode()
+     * 
+ * + * @param obj the object to obtain the hash code of, may be null + * @return the hash code of the object, or zero if null + * @since 2.1 + */ + public static int hashCode(Object obj) { + return ((obj == null) ? 0 : obj.hashCode()); + } + // Identity ToString //----------------------------------------------------------------------- /** diff --git a/src/test/org/apache/commons/lang/ObjectUtilsTest.java b/src/test/org/apache/commons/lang/ObjectUtilsTest.java index 664b331a0..1da31fc4a 100644 --- a/src/test/org/apache/commons/lang/ObjectUtilsTest.java +++ b/src/test/org/apache/commons/lang/ObjectUtilsTest.java @@ -30,7 +30,7 @@ import junit.textui.TestRunner; * @author Stephen Colebourne * @author Ringo De Smet * @author Gary Gregory - * @version $Id: ObjectUtilsTest.java,v 1.12 2004/02/18 23:06:19 ggregory Exp $ + * @version $Id: ObjectUtilsTest.java,v 1.13 2004/06/01 21:08:48 scolebourne Exp $ */ public class ObjectUtilsTest extends TestCase { private static final String FOO = "foo"; @@ -84,46 +84,51 @@ public class ObjectUtilsTest extends TestCase { assertTrue("ObjectUtils.equals(\"foo\", \"foo\") returned false", ObjectUtils.equals(FOO, FOO)); } - /** - * Show that java.util.Date and java.sql.Timestamp are apples and oranges. - * Prompted by an email discussion. - * - * The behavior is different b/w Sun Java 1.3.1_10 and 1.4.2_03. - */ - public void testDateEqualsJava() { - long now = 1076957313284L; // Feb 16, 2004 10:49... PST - java.util.Date date = new java.util.Date(now); - java.sql.Timestamp realTimestamp = new java.sql.Timestamp(now); - java.util.Date timestamp = realTimestamp; - // sanity check 1: - assertEquals(284000000, realTimestamp.getNanos()); - assertEquals(1076957313284L, date.getTime()); - // - // On Sun 1.3.1_10: - //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000> - // - //assertEquals(1076957313284L, timestamp.getTime()); - // - //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000> - // - //assertEquals(1076957313284L, realTimestamp.getTime()); - // sanity check 2: - assertEquals(date.getDay(), realTimestamp.getDay()); - assertEquals(date.getHours(), realTimestamp.getHours()); - assertEquals(date.getMinutes(), realTimestamp.getMinutes()); - assertEquals(date.getMonth(), realTimestamp.getMonth()); - assertEquals(date.getSeconds(), realTimestamp.getSeconds()); - assertEquals(date.getTimezoneOffset(), realTimestamp.getTimezoneOffset()); - assertEquals(date.getYear(), realTimestamp.getYear()); - // - // Time values are == and equals() on Sun 1.4.2_03 but NOT on Sun 1.3.1_10: - // - //assertFalse("Sanity check failed: date.getTime() == timestamp.getTime()", date.getTime() == timestamp.getTime()); - //assertFalse("Sanity check failed: timestamp.equals(date)", timestamp.equals(date)); - //assertFalse("Sanity check failed: date.equals(timestamp)", date.equals(timestamp)); - // real test: - //assertFalse("java.util.Date and java.sql.Timestamp should be equal", ObjectUtils.equals(date, timestamp)); + public void testHashCode() { + assertEquals(0, ObjectUtils.hashCode(null)); + assertEquals("a".hashCode(), ObjectUtils.hashCode("a")); } + +// /** +// * Show that java.util.Date and java.sql.Timestamp are apples and oranges. +// * Prompted by an email discussion. +// * +// * The behavior is different b/w Sun Java 1.3.1_10 and 1.4.2_03. +// */ +// public void testDateEqualsJava() { +// long now = 1076957313284L; // Feb 16, 2004 10:49... PST +// java.util.Date date = new java.util.Date(now); +// java.sql.Timestamp realTimestamp = new java.sql.Timestamp(now); +// java.util.Date timestamp = realTimestamp; +// // sanity check 1: +// assertEquals(284000000, realTimestamp.getNanos()); +// assertEquals(1076957313284L, date.getTime()); +// // +// // On Sun 1.3.1_10: +// //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000> +// // +// //assertEquals(1076957313284L, timestamp.getTime()); +// // +// //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000> +// // +// //assertEquals(1076957313284L, realTimestamp.getTime()); +// // sanity check 2: +// assertEquals(date.getDay(), realTimestamp.getDay()); +// assertEquals(date.getHours(), realTimestamp.getHours()); +// assertEquals(date.getMinutes(), realTimestamp.getMinutes()); +// assertEquals(date.getMonth(), realTimestamp.getMonth()); +// assertEquals(date.getSeconds(), realTimestamp.getSeconds()); +// assertEquals(date.getTimezoneOffset(), realTimestamp.getTimezoneOffset()); +// assertEquals(date.getYear(), realTimestamp.getYear()); +// // +// // Time values are == and equals() on Sun 1.4.2_03 but NOT on Sun 1.3.1_10: +// // +// //assertFalse("Sanity check failed: date.getTime() == timestamp.getTime()", date.getTime() == timestamp.getTime()); +// //assertFalse("Sanity check failed: timestamp.equals(date)", timestamp.equals(date)); +// //assertFalse("Sanity check failed: date.equals(timestamp)", date.equals(timestamp)); +// // real test: +// //assertFalse("java.util.Date and java.sql.Timestamp should be equal", ObjectUtils.equals(date, timestamp)); +// } public void testIdentityToString() { assertEquals(null, ObjectUtils.identityToString(null));