[LANG-1367] ObjectUtils.identityToString(Object) and friends should

allocate builders and buffers with a size
This commit is contained in:
Gary Gregory 2017-11-10 12:25:39 -07:00
parent 10122741ea
commit e863dcb2e7
3 changed files with 31 additions and 12 deletions

View File

@ -45,6 +45,10 @@ The <action> type attribute can be add,update,fix,remove.
</properties>
<body>
<release version="3.8" date="2017-MM-DD" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10.">
<action issue="LANG-1367" type="update" dev="ggregory" due-to="Gary Gregory">ObjectUtils.identityToString(Object) and friends should allocate builders and buffers with a size</action>
</release>
<release version="3.7" date="2017-11-04" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10.">
<action issue="LANG-1362" type="fix" dev="ggregory" due-to="Stephen Colebourne">Fix tests DateUtilsTest for Java 9 with en_GB locale</action>
<action issue="LANG-1365" type="fix" dev="ggregory" due-to="Gary Gregory">Fix NullPointerException in isJavaVersionAtLeast on Java 10, add SystemUtils.IS_JAVA_10, add JavaVersion.JAVA_10</action>

View File

@ -47,7 +47,7 @@ import org.apache.commons.lang3.text.StrBuilder;
public class ObjectUtils {
private static final char AT_SIGN = '@';
/**
* <p>Singleton used as a {@code null} placeholder where
* {@code null} has another meaning.</p>
@ -333,8 +333,14 @@ public class ObjectUtils {
if (object == null) {
return null;
}
final StringBuilder builder = new StringBuilder();
identityToString(builder, object);
final String name = object.getClass().getName();
final String hexString = Integer.toHexString(System.identityHashCode(object));
final StringBuilder builder = new StringBuilder(name.length() + 1 + hexString.length());
// @formatter:off
builder.append(name)
.append(AT_SIGN)
.append(hexString);
// @formatter:off
return builder.toString();
}
@ -381,9 +387,12 @@ public class ObjectUtils {
@Deprecated
public static void identityToString(final StrBuilder builder, final Object object) {
Validate.notNull(object, "Cannot get the toString of a null object");
builder.append(object.getClass().getName())
final String name = object.getClass().getName();
final String hexString = Integer.toHexString(System.identityHashCode(object));
builder.ensureCapacity(builder.length() + name.length() + 1 + hexString.length());
builder.append(name)
.append(AT_SIGN)
.append(Integer.toHexString(System.identityHashCode(object)));
.append(hexString);
}
/**
@ -403,9 +412,12 @@ public class ObjectUtils {
*/
public static void identityToString(final StringBuffer buffer, final Object object) {
Validate.notNull(object, "Cannot get the toString of a null object");
buffer.append(object.getClass().getName())
final String name = object.getClass().getName();
final String hexString = Integer.toHexString(System.identityHashCode(object));
buffer.ensureCapacity(buffer.length() + name.length() + 1 + hexString.length());
buffer.append(name)
.append(AT_SIGN)
.append(Integer.toHexString(System.identityHashCode(object)));
.append(hexString);
}
/**
@ -425,9 +437,12 @@ public class ObjectUtils {
*/
public static void identityToString(final StringBuilder builder, final Object object) {
Validate.notNull(object, "Cannot get the toString of a null object");
builder.append(object.getClass().getName())
final String name = object.getClass().getName();
final String hexString = Integer.toHexString(System.identityHashCode(object));
builder.ensureCapacity(builder.length() + name.length() + 1 + hexString.length());
builder.append(name)
.append(AT_SIGN)
.append(Integer.toHexString(System.identityHashCode(object)));
.append(hexString);
}
// ToString

View File

@ -195,7 +195,7 @@ public class ObjectUtilsTest {
public void testIdentityToStringObjectNull() {
assertNull(ObjectUtils.identityToString(null));
}
@Test
public void testIdentityToStringInteger() {
final Integer i = Integer.valueOf(90);
@ -203,14 +203,14 @@ public class ObjectUtilsTest {
assertEquals(expected, ObjectUtils.identityToString(i));
}
@Test
public void testIdentityToStringString() {
assertEquals(
"java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
ObjectUtils.identityToString(FOO));
}
@Test
public void testIdentityToStringStringBuilder() {
final Integer i = Integer.valueOf(90);