Refactor such that the ObjectUtils.identityToString() method logic is only in ObjectUtils and not duplicated in the builder package.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137438 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2003-07-16 01:47:40 +00:00
parent 0dd253a756
commit 61dd85ca2f
4 changed files with 43 additions and 23 deletions

View File

@ -61,8 +61,9 @@
* @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a> * @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @author Stephen Colebourne * @author Stephen Colebourne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 1.0 * @since 1.0
* @version $Id: ObjectUtils.java,v 1.10 2003/07/16 00:39:05 scolebourne Exp $ * @version $Id: ObjectUtils.java,v 1.11 2003/07/16 01:47:39 ggregory Exp $
*/ */
public class ObjectUtils { public class ObjectUtils {
@ -141,11 +142,29 @@ public static String identityToString(Object object) {
if (object == null) { if (object == null) {
return 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(object.getClass().getName())
.append('@') .append('@')
.append(Integer.toHexString(System.identityHashCode(object))) .append(Integer.toHexString(System.identityHashCode(object)));
.toString();
} }
/** /**

View File

@ -54,6 +54,7 @@
package org.apache.commons.lang.builder; package org.apache.commons.lang.builder;
import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.ObjectUtils;
/** /**
* <p>Builds <code>toString()</code> values.</p> * <p>Builds <code>toString()</code> values.</p>
@ -123,7 +124,7 @@
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Gary Gregory * @author Gary Gregory
* @since 1.0 * @since 1.0
* @version $Id: ToStringBuilder.java,v 1.24 2003/07/14 22:25:03 bayard Exp $ * @version $Id: ToStringBuilder.java,v 1.25 2003/07/16 01:47:39 ggregory Exp $
*/ */
public class ToStringBuilder { public class ToStringBuilder {
@ -973,7 +974,7 @@ public ToStringBuilder append(String fieldName, short[] array, boolean fullDetai
* @param object the <code>Object</code> whose class name and id to output * @param object the <code>Object</code> whose class name and id to output
*/ */
public ToStringBuilder appendAsObjectToString(Object object) { public ToStringBuilder appendAsObjectToString(Object object) {
this.getStyle().appendAsObjectToString(this.getStringBuffer(), object); ObjectUtils.appendIdentityToString(this.getStringBuffer(), object);
return this; return this;
} }

View File

@ -58,6 +58,7 @@
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.SystemUtils; import org.apache.commons.lang.SystemUtils;
/** /**
* <p><code>ToStringStyle</code> works with <code>ToStringBuilder</code> * <p><code>ToStringStyle</code> works with <code>ToStringBuilder</code>
@ -83,7 +84,7 @@
* @author Stephen Colebourne * @author Stephen Colebourne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 1.0 * @since 1.0
* @version $Id: ToStringStyle.java,v 1.15 2003/07/14 22:25:04 bayard Exp $ * @version $Id: ToStringStyle.java,v 1.16 2003/07/16 01:47:39 ggregory Exp $
*/ */
public abstract class ToStringStyle implements Serializable { public abstract class ToStringStyle implements Serializable {
@ -333,7 +334,7 @@ public void append(StringBuffer buffer, String fieldName, Object value, Boolean
protected void appendInternal(StringBuffer buffer, String fieldName, Object value, boolean detail) { protected void appendInternal(StringBuffer buffer, String fieldName, Object value, boolean detail) {
if (ReflectionToStringBuilder.isRegistered(value) if (ReflectionToStringBuilder.isRegistered(value)
&& !(value instanceof Number || value instanceof Boolean || value instanceof Character)) { && !(value instanceof Number || value instanceof Boolean || value instanceof Character)) {
appendAsObjectToString(buffer, value); ObjectUtils.appendIdentityToString(buffer, value);
} else if (value instanceof Collection) { } else if (value instanceof Collection) {
if (detail) { if (detail) {
@ -1309,19 +1310,6 @@ protected void appendIdentityHashCode(StringBuffer buffer, Object 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);
}
/** /**
* <p>Append the content start to the buffer.</p> * <p>Append the content start to the buffer.</p>
* *

View File

@ -64,7 +64,8 @@
* @author <a href="mailto:jmcnally@collab.net">John McNally</a> * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</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:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: ObjectUtilsTest.java,v 1.4 2003/03/23 21:49:13 scolebourne Exp $ * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @version $Id: ObjectUtilsTest.java,v 1.5 2003/07/16 01:47:40 ggregory Exp $
*/ */
public class ObjectUtilsTest extends TestCase { public class ObjectUtilsTest extends TestCase {
private static final String FOO = "foo"; private static final String FOO = "foo";
@ -120,6 +121,17 @@ public void testIdentityToString() {
assertEquals(null, ObjectUtils.identityToString(null)); 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() { public void testNull() {
assertTrue(ObjectUtils.NULL != null); assertTrue(ObjectUtils.NULL != null);
assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null); assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null);