You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/06/01 23:08:48 UTC

cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ObjectUtilsTest.java

scolebourne    2004/06/01 14:08:48

  Modified:    lang/src/java/org/apache/commons/lang ObjectUtils.java
               lang/src/test/org/apache/commons/lang ObjectUtilsTest.java
  Log:
  Add ObjectUtils.hashCode() - a null safe hash code
  bug 28554, from Mario Winterer
  
  Revision  Changes    Path
  1.24      +20 -2     jakarta-commons/lang/src/java/org/apache/commons/lang/ObjectUtils.java
  
  Index: ObjectUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ObjectUtils.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- ObjectUtils.java	18 Feb 2004 22:59:50 -0000	1.23
  +++ ObjectUtils.java	1 Jun 2004 21:08:48 -0000	1.24
  @@ -29,6 +29,7 @@
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    * @author Stephen Colebourne
    * @author Gary Gregory
  + * @author Mario Winterer
    * @since 1.0
    * @version $Id$
    */
  @@ -113,7 +114,24 @@
           }
           return object1.equals(object2);
       }
  -    
  +
  +    /**
  +     * <p>Gets the hash code of an object returning zero when the
  +     * object is <code>null</code>.</p>
  +     *
  +     * <pre>
  +     * ObjectUtils.hashCode(null)   = 0
  +     * ObjectUtils.hashCode(obj)    = obj.hashCode()
  +     * </pre>
  +     *
  +     * @param obj  the object to obtain the hash code of, may be <code>null</code>
  +     * @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
       //-----------------------------------------------------------------------
       /**
  
  
  
  1.13      +46 -41    jakarta-commons/lang/src/test/org/apache/commons/lang/ObjectUtilsTest.java
  
  Index: ObjectUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ObjectUtilsTest.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- ObjectUtilsTest.java	18 Feb 2004 23:06:19 -0000	1.12
  +++ ObjectUtilsTest.java	1 Jun 2004 21:08:48 -0000	1.13
  @@ -84,46 +84,51 @@
           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));
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org