You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/09/01 14:16:46 UTC

[commons-lang] branch master updated: Add ObjectUtils.identityHashCodeHex(Object).

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 7f7c3d6  Add ObjectUtils.identityHashCodeHex(Object).
7f7c3d6 is described below

commit 7f7c3d63c895d52fafd602459e269b92a6c93791
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Sep 1 10:16:42 2021 -0400

    Add ObjectUtils.identityHashCodeHex(Object).
    
    Add ObjectUtils.hashCodeHex(Object).
---
 src/changes/changes.xml                            |  2 ++
 .../java/org/apache/commons/lang3/ObjectUtils.java | 39 +++++++++++++++++++---
 .../commons/lang3/builder/ToStringStyle.java       |  4 +--
 .../org/apache/commons/lang3/ObjectUtilsTest.java  | 23 +++++++++++++
 4 files changed, 61 insertions(+), 7 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ae1ea41..7709ece 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -82,6 +82,8 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add UncheckedInterruptedException.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add TimeZones.GMT.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_16.</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ObjectUtils.identityHashCodeHex(Object).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ObjectUtils.hashCodeHex(Object).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.2.3 #735.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, #764.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index c9da955..5d442f3 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -765,6 +765,21 @@ public class ObjectUtils {
     }
 
     /**
+     * Returns the hex hash code for the given object per {@link Objects#hashCode(Object)}.
+     * <p>
+     * Short hand for {@code Integer.toHexString(Objects.hashCode(object))}.
+     * </p>
+     *
+     * @param object object for which the hashCode is to be calculated
+     * @return Hash code in hexadecimal format.
+     * @since 3.13.0
+     */
+    public static String hashCodeHex(final Object object) {
+        return Integer.toHexString(Objects.hashCode(object));
+    }
+
+
+    /**
      * <p>Gets the hash code for multiple objects.</p>
      *
      * <p>This allows a hash code to be rapidly calculated for a number of objects.
@@ -818,7 +833,21 @@ public class ObjectUtils {
         Validate.notNull(object, "object");
         appendable.append(object.getClass().getName())
               .append(AT_SIGN)
-              .append(Integer.toHexString(System.identityHashCode(object)));
+              .append(identityHashCodeHex(object));
+    }
+
+    /**
+     * Returns the hex hash code for the given object per {@link System#identityHashCode(Object)}.
+     * <p>
+     * Short hand for {@code Integer.toHexString(System.identityHashCode(object))}.
+     * </p>
+     *
+     * @param object object for which the hashCode is to be calculated
+     * @return Hash code in hexadecimal format.
+     * @since 3.13.0
+     */
+    public static String identityHashCodeHex(final Object object) {
+        return Integer.toHexString(System.identityHashCode(object));
     }
 
     /**
@@ -842,7 +871,7 @@ public class ObjectUtils {
             return null;
         }
         final String name = object.getClass().getName();
-        final String hexString = Integer.toHexString(System.identityHashCode(object));
+        final String hexString = identityHashCodeHex(object);
         final StringBuilder builder = new StringBuilder(name.length() + 1 + hexString.length());
         // @formatter:off
         builder.append(name)
@@ -873,7 +902,7 @@ public class ObjectUtils {
     public static void identityToString(final StrBuilder builder, final Object object) {
         Validate.notNull(object, "object");
         final String name = object.getClass().getName();
-        final String hexString = Integer.toHexString(System.identityHashCode(object));
+        final String hexString = identityHashCodeHex(object);
         builder.ensureCapacity(builder.length() +  name.length() + 1 + hexString.length());
         builder.append(name)
               .append(AT_SIGN)
@@ -898,7 +927,7 @@ public class ObjectUtils {
     public static void identityToString(final StringBuffer buffer, final Object object) {
         Validate.notNull(object, "object");
         final String name = object.getClass().getName();
-        final String hexString = Integer.toHexString(System.identityHashCode(object));
+        final String hexString = identityHashCodeHex(object);
         buffer.ensureCapacity(buffer.length() + name.length() + 1 + hexString.length());
         buffer.append(name)
               .append(AT_SIGN)
@@ -923,7 +952,7 @@ public class ObjectUtils {
     public static void identityToString(final StringBuilder builder, final Object object) {
         Validate.notNull(object, "object");
         final String name = object.getClass().getName();
-        final String hexString = Integer.toHexString(System.identityHashCode(object));
+        final String hexString = identityHashCodeHex(object);
         builder.ensureCapacity(builder.length() +  name.length() + 1 + hexString.length());
         builder.append(name)
               .append(AT_SIGN)
diff --git a/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java b/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java
index c2860d2..50a92e7 100644
--- a/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java
+++ b/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java
@@ -1490,10 +1490,10 @@ public abstract class ToStringStyle implements Serializable {
      * @param object  the {@code Object} whose id to output
      */
     protected void appendIdentityHashCode(final StringBuffer buffer, final Object object) {
-        if (this.isUseIdentityHashCode() && object!=null) {
+        if (this.isUseIdentityHashCode() && object != null) {
             register(object);
             buffer.append('@');
-            buffer.append(Integer.toHexString(System.identityHashCode(object)));
+            buffer.append(ObjectUtils.identityHashCodeHex(object));
         }
     }
 
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index a4cd46d..2c053b6 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -41,6 +41,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import java.util.function.Supplier;
 
@@ -63,6 +64,7 @@ public class ObjectUtilsTest {
         }
 
     }
+
     /**
      * String that is cloneable.
      */
@@ -77,6 +79,7 @@ public class ObjectUtilsTest {
             return (CloneableString) super.clone();
         }
     }
+
     static final class NonComparableCharSequence implements CharSequence {
         final String value;
 
@@ -110,6 +113,7 @@ public class ObjectUtilsTest {
             return value;
         }
     }
+
     /**
      * String that is not cloneable.
      */
@@ -119,6 +123,7 @@ public class ObjectUtilsTest {
             super(s);
         }
     }
+
     private static final String FOO = "foo";
     private static final String BAR = "bar";
     private static final String[] NON_EMPTY_ARRAY = { FOO, BAR, };
@@ -459,6 +464,15 @@ public class ObjectUtilsTest {
     }
 
     @Test
+    public void testHashCodeHex() {
+        final Integer i = Integer.valueOf(90);
+        assertEquals(Integer.toHexString(Objects.hashCode(i)), ObjectUtils.hashCodeHex(i));
+        final Integer zero = Integer.valueOf(0);
+        assertEquals(Integer.toHexString(Objects.hashCode(zero)), ObjectUtils.hashCodeHex(zero));
+        assertEquals(Integer.toHexString(Objects.hashCode(null)), ObjectUtils.hashCodeHex(null));
+    }
+
+    @Test
     public void testHashCodeMulti_multiple_emptyArray() {
         final Object[] array = {};
         assertEquals(1, ObjectUtils.hashCodeMulti(array));
@@ -486,6 +500,15 @@ public class ObjectUtilsTest {
     }
 
     @Test
+    public void testIdentityHashCodeHex() {
+        final Integer i = Integer.valueOf(90);
+        assertEquals(Integer.toHexString(System.identityHashCode(i)), ObjectUtils.identityHashCodeHex(i));
+        final Integer zero = Integer.valueOf(0);
+        assertEquals(Integer.toHexString(System.identityHashCode(zero)), ObjectUtils.identityHashCodeHex(zero));
+        assertEquals(Integer.toHexString(System.identityHashCode(null)), ObjectUtils.identityHashCodeHex(null));
+    }
+
+    @Test
     public void testIdentityToStringAppendable() throws IOException {
         final Integer i = Integer.valueOf(121);
         final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i));