Author: brentworden Date: Sun Jul 7 22:07:46 2013 New Revision: 1500545 URL: http://svn.apache.org/r1500545 Log: LANG-837 Add ObjectUtils.toIdentityString methods that support StringBuilder, StrBuilder, and Appendable
Modified: commons/proper/lang/trunk/src/changes/changes.xml commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java Modified: commons/proper/lang/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1500545&r1=1500544&r2=1500545&view=diff ============================================================================== --- commons/proper/lang/trunk/src/changes/changes.xml (original) +++ commons/proper/lang/trunk/src/changes/changes.xml Sun Jul 7 22:07:46 2013 @@ -22,6 +22,7 @@ <body> <release version="3.2" date="TBA" description="Next release"> + <action issue="LANG-837" type="add" due-to="Sebb">Add ObjectUtils.toIdentityString methods that support StringBuilder, StrBuilder, and Appendable</action> <action issue="LANG-896" type="fix" due-to="Mark Bryan Yu">BooleanUtils.toBoolean(String str) javadoc is not updated</action> <action issue="LANG-879" type="fix">LocaleUtils test fails with new Locale "ja_JP_JP_#u-ca-japanese" of JDK7</action> <action issue="LANG-836" type="fix" due-to="Arnaud Brunet">StrSubstitutor does not support StringBuilder or CharSequence</action> Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java?rev=1500545&r1=1500544&r2=1500545&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java Sun Jul 7 22:07:46 2013 @@ -16,6 +16,7 @@ */ package org.apache.commons.lang3; +import java.io.IOException; import java.io.Serializable; import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; @@ -28,6 +29,7 @@ import java.util.TreeSet; import org.apache.commons.lang3.exception.CloneFailedException; import org.apache.commons.lang3.mutable.MutableInt; +import org.apache.commons.lang3.text.StrBuilder; /** * <p>Operations on {@code Object}.</p> @@ -251,9 +253,58 @@ public class ObjectUtils { if (object == null) { return null; } - final StringBuffer buffer = new StringBuffer(); - identityToString(buffer, object); - return buffer.toString(); + final StringBuilder builder = new StringBuilder(); + identityToString(builder, object); + return builder.toString(); + } + + /** + * <p>Appends the toString that would be produced by {@code Object} + * if a class did not override toString itself. {@code null} + * will throw a NullPointerException for either of the two parameters. </p> + * + * <pre> + * ObjectUtils.identityToString(appendable, "") = appendable.append("java.lang.String@1e23" + * ObjectUtils.identityToString(appendable, Boolean.TRUE) = appendable.append("java.lang.Boolean@7fa" + * ObjectUtils.identityToString(appendable, Boolean.TRUE) = appendable.append("java.lang.Boolean@7fa") + * </pre> + * + * @param appendable the appendable to append to + * @param object the object to create a toString for + * @throws IOException + * @since 3.2 + */ + public static void identityToString(final Appendable appendable, final Object object) throws IOException { + if (object == null) { + throw new NullPointerException("Cannot get the toString of a null identity"); + } + appendable.append(object.getClass().getName()) + .append('@') + .append(Integer.toHexString(System.identityHashCode(object))); + } + + /** + * <p>Appends the toString that would be produced by {@code Object} + * if a class did not override toString itself. {@code null} + * will throw a NullPointerException for either of the two parameters. </p> + * + * <pre> + * ObjectUtils.identityToString(builder, "") = builder.append("java.lang.String@1e23" + * ObjectUtils.identityToString(builder, Boolean.TRUE) = builder.append("java.lang.Boolean@7fa" + * ObjectUtils.identityToString(builder, Boolean.TRUE) = builder.append("java.lang.Boolean@7fa") + * </pre> + * + * @param builder the builder to append to + * @param object the object to create a toString for + * @since 3.2 + */ + public static void identityToString(final StrBuilder builder, final Object object) { + if (object == null) { + throw new NullPointerException("Cannot get the toString of a null identity"); + } + builder.append(object.getClass().getName()) + .append('@') + .append(Integer.toHexString(System.identityHashCode(object))); } /** @@ -280,6 +331,30 @@ public class ObjectUtils { .append(Integer.toHexString(System.identityHashCode(object))); } + /** + * <p>Appends the toString that would be produced by {@code Object} + * if a class did not override toString itself. {@code null} + * will throw a NullPointerException for either of the two parameters. </p> + * + * <pre> + * ObjectUtils.identityToString(builder, "") = builder.append("java.lang.String@1e23" + * ObjectUtils.identityToString(builder, Boolean.TRUE) = builder.append("java.lang.Boolean@7fa" + * ObjectUtils.identityToString(builder, Boolean.TRUE) = builder.append("java.lang.Boolean@7fa") + * </pre> + * + * @param builder the builder to append to + * @param object the object to create a toString for + * @since 3.2 + */ + public static void identityToString(final StringBuilder builder, final Object object) { + if (object == null) { + throw new NullPointerException("Cannot get the toString of a null identity"); + } + builder.append(object.getClass().getName()) + .append('@') + .append(Integer.toHexString(System.identityHashCode(object))); + } + // ToString //----------------------------------------------------------------------- /** Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java?rev=1500545&r1=1500544&r2=1500545&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java (original) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java Sun Jul 7 22:07:46 2013 @@ -25,6 +25,7 @@ import static org.junit.Assert.assertSam import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.util.ArrayList; @@ -36,6 +37,7 @@ import java.util.List; import org.apache.commons.lang3.exception.CloneFailedException; import org.apache.commons.lang3.mutable.MutableObject; +import org.apache.commons.lang3.text.StrBuilder; import org.junit.Test; /** @@ -179,27 +181,101 @@ public class ObjectUtilsTest { // } @Test - public void testIdentityToString() { + public void testIdentityToStringStringBuffer() { + final Integer i = Integer.valueOf(45); + final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + final StringBuffer buffer = new StringBuffer(); + ObjectUtils.identityToString(buffer, i); + assertEquals(expected, buffer.toString()); + + try { + ObjectUtils.identityToString((StringBuffer)null, "tmp"); + fail("NullPointerException expected"); + } catch(final NullPointerException npe) { + } + try { + ObjectUtils.identityToString(new StringBuffer(), null); + fail("NullPointerException expected"); + } catch(final NullPointerException npe) { + } + } + + @Test + public void testIdentityToStringStringBuilder() { assertEquals(null, ObjectUtils.identityToString(null)); assertEquals( "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)), ObjectUtils.identityToString(FOO)); final Integer i = Integer.valueOf(90); final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + assertEquals(expected, ObjectUtils.identityToString(i)); - final StringBuffer buffer = new StringBuffer(); - ObjectUtils.identityToString(buffer, i); - assertEquals(expected, buffer.toString()); + + final StringBuilder builder = new StringBuilder(); + ObjectUtils.identityToString(builder, i); + assertEquals(expected, builder.toString()); try { - ObjectUtils.identityToString(null, "tmp"); + ObjectUtils.identityToString((StringBuilder)null, "tmp"); fail("NullPointerException expected"); } catch(final NullPointerException npe) { } + try { - ObjectUtils.identityToString(new StringBuffer(), null); + ObjectUtils.identityToString(new StringBuilder(), null); + fail("NullPointerException expected"); + } catch(final NullPointerException npe) { + } + } + + @Test + public void testIdentityToStringStrBuilder() { + final Integer i = Integer.valueOf(102); + final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + final StrBuilder builder = new StrBuilder(); + ObjectUtils.identityToString(builder, i); + assertEquals(expected, builder.toString()); + + try { + ObjectUtils.identityToString((StrBuilder)null, "tmp"); + fail("NullPointerException expected"); + } catch(final NullPointerException npe) { + } + + try { + ObjectUtils.identityToString(new StrBuilder(), null); + fail("NullPointerException expected"); + } catch(final NullPointerException npe) { + } + } + + @Test + public void testIdentityToStringAppendable() { + final Integer i = Integer.valueOf(121); + final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + try { + final Appendable appendable = new StringBuilder(); + ObjectUtils.identityToString(appendable, i); + assertEquals(expected, appendable.toString()); + } catch(IOException ex) { + fail("IOException unexpected"); + } + + try { + ObjectUtils.identityToString((Appendable)null, "tmp"); + fail("NullPointerException expected"); + } catch(final NullPointerException npe) { + } catch (IOException ex) { + } + + try { + ObjectUtils.identityToString((Appendable)(new StringBuilder()), null); fail("NullPointerException expected"); } catch(final NullPointerException npe) { + } catch (IOException ex) { } }