LANG-837 Add ObjectUtils.toIdentityString methods that support StringBuilder, StrBuilder, and Appendable
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1500545 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f291dea74f
commit
396afc3e46
|
@ -22,6 +22,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<release version="3.2" date="TBA" description="Next release">
|
<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-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-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>
|
<action issue="LANG-836" type="fix" due-to="Arnaud Brunet">StrSubstitutor does not support StringBuilder or CharSequence</action>
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
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.exception.CloneFailedException;
|
||||||
import org.apache.commons.lang3.mutable.MutableInt;
|
import org.apache.commons.lang3.mutable.MutableInt;
|
||||||
|
import org.apache.commons.lang3.text.StrBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Operations on {@code Object}.</p>
|
* <p>Operations on {@code Object}.</p>
|
||||||
|
@ -251,9 +253,58 @@ public class ObjectUtils {
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuilder builder = new StringBuilder();
|
||||||
identityToString(buffer, object);
|
identityToString(builder, object);
|
||||||
return buffer.toString();
|
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)));
|
.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
|
// ToString
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -25,6 +25,7 @@ import static org.junit.Assert.assertSame;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -36,6 +37,7 @@ import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.lang3.exception.CloneFailedException;
|
import org.apache.commons.lang3.exception.CloneFailedException;
|
||||||
import org.apache.commons.lang3.mutable.MutableObject;
|
import org.apache.commons.lang3.mutable.MutableObject;
|
||||||
|
import org.apache.commons.lang3.text.StrBuilder;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -179,20 +181,16 @@ public class ObjectUtilsTest {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIdentityToString() {
|
public void testIdentityToStringStringBuffer() {
|
||||||
assertEquals(null, ObjectUtils.identityToString(null));
|
final Integer i = Integer.valueOf(45);
|
||||||
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));
|
final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i));
|
||||||
assertEquals(expected, ObjectUtils.identityToString(i));
|
|
||||||
final StringBuffer buffer = new StringBuffer();
|
final StringBuffer buffer = new StringBuffer();
|
||||||
ObjectUtils.identityToString(buffer, i);
|
ObjectUtils.identityToString(buffer, i);
|
||||||
assertEquals(expected, buffer.toString());
|
assertEquals(expected, buffer.toString());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ObjectUtils.identityToString(null, "tmp");
|
ObjectUtils.identityToString((StringBuffer)null, "tmp");
|
||||||
fail("NullPointerException expected");
|
fail("NullPointerException expected");
|
||||||
} catch(final NullPointerException npe) {
|
} catch(final NullPointerException npe) {
|
||||||
}
|
}
|
||||||
|
@ -202,6 +200,84 @@ public class ObjectUtilsTest {
|
||||||
} catch(final NullPointerException npe) {
|
} 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 StringBuilder builder = new StringBuilder();
|
||||||
|
ObjectUtils.identityToString(builder, i);
|
||||||
|
assertEquals(expected, builder.toString());
|
||||||
|
|
||||||
|
try {
|
||||||
|
ObjectUtils.identityToString((StringBuilder)null, "tmp");
|
||||||
|
fail("NullPointerException expected");
|
||||||
|
} catch(final NullPointerException npe) {
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
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) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToString_Object() {
|
public void testToString_Object() {
|
||||||
|
|
Loading…
Reference in New Issue