commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ObjectUtilsTest.java
Date Wed, 16 Jul 2003 01:47:40 GMT
ggregory    2003/07/15 18:47:40

  Modified:    lang/src/java/org/apache/commons/lang/builder
                        ToStringStyle.java ToStringBuilder.java
               lang/src/java/org/apache/commons/lang ObjectUtils.java
               lang/src/test/org/apache/commons/lang ObjectUtilsTest.java
  Log:
  Refactor such that the ObjectUtils.identityToString() method logic is only in ObjectUtils
and not duplicated in the builder package.
  
  Revision  Changes    Path
  1.16      +3 -15     jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringStyle.java
  
  Index: ToStringStyle.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringStyle.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- ToStringStyle.java	14 Jul 2003 22:25:04 -0000	1.15
  +++ ToStringStyle.java	16 Jul 2003 01:47:39 -0000	1.16
  @@ -58,6 +58,7 @@
   import java.util.Collection;
   import java.util.Map;
   
  +import org.apache.commons.lang.ObjectUtils;
   import org.apache.commons.lang.SystemUtils;
   /**
    * <p><code>ToStringStyle</code> works with <code>ToStringBuilder</code>
  @@ -333,7 +334,7 @@
       protected void appendInternal(StringBuffer buffer, String fieldName, Object value,
boolean detail) {
           if (ReflectionToStringBuilder.isRegistered(value) 
                   && !(value instanceof Number || value instanceof Boolean || value
instanceof Character)) {
  -            appendAsObjectToString(buffer, value);
  +                ObjectUtils.appendIdentityToString(buffer, value);
               
           } else if (value instanceof Collection) {
               if (detail) {
  @@ -1307,19 +1308,6 @@
               buffer.append('@');
               buffer.append(Integer.toHexString(System.identityHashCode(object)));
           }
  -    }
  -
  -    /**
  -     * <p>Appends with the same format as the default <code>Object toString()
  -     * </code> method. Appends the class name followed by 
  -     * {@link System#identityHashCode(java.lang.Object)}.</p>
  -     * 
  -     * @param buffer  the <code>StringBuffer</code> to populate
  -     * @param object  the <code>Object</code> whose class name and id to output
  -     */
  -    protected void appendAsObjectToString(StringBuffer buffer, Object object) {
  -        this.appendClassName(buffer, object);
  -        this.appendIdentityHashCode(buffer, object);
       }
   
       /**
  
  
  
  1.25      +3 -2      jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringBuilder.java
  
  Index: ToStringBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringBuilder.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- ToStringBuilder.java	14 Jul 2003 22:25:03 -0000	1.24
  +++ ToStringBuilder.java	16 Jul 2003 01:47:39 -0000	1.25
  @@ -54,6 +54,7 @@
   package org.apache.commons.lang.builder;
   
   import org.apache.commons.lang.BooleanUtils;
  +import org.apache.commons.lang.ObjectUtils;
   
   /**
    * <p>Builds <code>toString()</code> values.</p>
  @@ -973,7 +974,7 @@
        * @param object  the <code>Object</code> whose class name and id to output
        */
       public ToStringBuilder appendAsObjectToString(Object object) {
  -        this.getStyle().appendAsObjectToString(this.getStringBuffer(), object);
  +        ObjectUtils.appendIdentityToString(this.getStringBuffer(), object);
           return this;
       }
   
  
  
  
  1.11      +23 -4     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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ObjectUtils.java	16 Jul 2003 00:39:05 -0000	1.10
  +++ ObjectUtils.java	16 Jul 2003 01:47:39 -0000	1.11
  @@ -61,6 +61,7 @@
    * @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    * @author Stephen Colebourne
  + * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
    * @since 1.0
    * @version $Id$
    */
  @@ -141,11 +142,29 @@
           if (object == null) {
               return null;
           }
  -        return new StringBuffer()
  +        return appendIdentityToString(new StringBuffer(), object).toString();
  +    }
  +
  +    /**
  +     * <p>Appends the toString that would be produced by <code>Object</code>
  +     * if a class did not override toString itself. <code>Null</code>
  +     * will return <code>null</code>.</p>
  +     *
  +     * @param buffer  the buffer to append to, may not be
  +     *  <code>null</code>
  +     * @param object  the object to create a toString for, may be
  +     *  <code>null</code>
  +     * @return the default toString text, or <code>null</code> if
  +     *  <code>null</code> passed in
  +     */
  +    public static StringBuffer appendIdentityToString(StringBuffer buffer, Object object)
{
  +        if (object == null) {
  +            return null;
  +        }
  +        return buffer
               .append(object.getClass().getName())
               .append('@')
  -            .append(Integer.toHexString(System.identityHashCode(object)))
  -            .toString();
  +            .append(Integer.toHexString(System.identityHashCode(object)));
       }
   
       /**
  
  
  
  1.5       +14 -2     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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ObjectUtilsTest.java	23 Mar 2003 21:49:13 -0000	1.4
  +++ ObjectUtilsTest.java	16 Jul 2003 01:47:40 -0000	1.5
  @@ -64,6 +64,7 @@
    * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
    * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
    * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
  + * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
    * @version $Id$
    */
   public class ObjectUtilsTest extends TestCase {
  @@ -120,10 +121,21 @@
           assertEquals(null, ObjectUtils.identityToString(null));
       }
   
  +    public void testAppendIdentityToString() {
  +        assertEquals(
  +            "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
  +            ObjectUtils.appendIdentityToString(new StringBuffer(), FOO).toString());
  +        Integer i = new Integer(90);
  +        assertEquals(
  +            "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)),
  +            ObjectUtils.appendIdentityToString(new StringBuffer(), i).toString());
  +        assertEquals(null, ObjectUtils.appendIdentityToString(new StringBuffer(), null));
  +    }
  +
       public void testNull() {
           assertTrue(ObjectUtils.NULL != null);
           assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null);
           assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL));
       }
  -   
  +
   }
  
  
  

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


Mime
View raw message