Repository: commons-lang Updated Branches: refs/heads/master 10122741e -> e863dcb2e
[LANG-1367] ObjectUtils.identityToString(Object) and friends should allocate builders and buffers with a size Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/e863dcb2 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/e863dcb2 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/e863dcb2 Branch: refs/heads/master Commit: e863dcb2e7a654af6b8eb5865d8d1d8eee0a6d28 Parents: 1012274 Author: Gary Gregory <ggreg...@apache.org> Authored: Fri Nov 10 12:25:39 2017 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Fri Nov 10 12:25:39 2017 -0700 ---------------------------------------------------------------------- src/changes/changes.xml | 4 +++ .../org/apache/commons/lang3/ObjectUtils.java | 33 ++++++++++++++------ .../apache/commons/lang3/ObjectUtilsTest.java | 6 ++-- 3 files changed, 31 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e863dcb2/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7d10200..07461be 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -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> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e863dcb2/src/main/java/org/apache/commons/lang3/ObjectUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 1bcf72d..6b93a7d 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -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 http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e863dcb2/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index 06b97e2..661722d 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -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);