Add ObjectUtils.identityHashCodeHex(Object).

Add ObjectUtils.hashCodeHex(Object).
This commit is contained in:
Gary Gregory 2021-09-01 10:16:42 -04:00
parent b652c477f6
commit 7f7c3d63c8
4 changed files with 61 additions and 7 deletions

View File

@ -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 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 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 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 --> <!-- 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, 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> <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>

View File

@ -764,6 +764,21 @@ public class ObjectUtils {
return Objects.hashCode(obj); return Objects.hashCode(obj);
} }
/**
* 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>Gets the hash code for multiple objects.</p>
* *
@ -818,7 +833,21 @@ public class ObjectUtils {
Validate.notNull(object, "object"); Validate.notNull(object, "object");
appendable.append(object.getClass().getName()) appendable.append(object.getClass().getName())
.append(AT_SIGN) .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; return null;
} }
final String name = object.getClass().getName(); 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()); final StringBuilder builder = new StringBuilder(name.length() + 1 + hexString.length());
// @formatter:off // @formatter:off
builder.append(name) builder.append(name)
@ -873,7 +902,7 @@ public class ObjectUtils {
public static void identityToString(final StrBuilder builder, final Object object) { public static void identityToString(final StrBuilder builder, final Object object) {
Validate.notNull(object, "object"); Validate.notNull(object, "object");
final String name = object.getClass().getName(); 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.ensureCapacity(builder.length() + name.length() + 1 + hexString.length());
builder.append(name) builder.append(name)
.append(AT_SIGN) .append(AT_SIGN)
@ -898,7 +927,7 @@ public class ObjectUtils {
public static void identityToString(final StringBuffer buffer, final Object object) { public static void identityToString(final StringBuffer buffer, final Object object) {
Validate.notNull(object, "object"); Validate.notNull(object, "object");
final String name = object.getClass().getName(); 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.ensureCapacity(buffer.length() + name.length() + 1 + hexString.length());
buffer.append(name) buffer.append(name)
.append(AT_SIGN) .append(AT_SIGN)
@ -923,7 +952,7 @@ public class ObjectUtils {
public static void identityToString(final StringBuilder builder, final Object object) { public static void identityToString(final StringBuilder builder, final Object object) {
Validate.notNull(object, "object"); Validate.notNull(object, "object");
final String name = object.getClass().getName(); 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.ensureCapacity(builder.length() + name.length() + 1 + hexString.length());
builder.append(name) builder.append(name)
.append(AT_SIGN) .append(AT_SIGN)

View File

@ -1490,10 +1490,10 @@ public abstract class ToStringStyle implements Serializable {
* @param object the {@code Object} whose id to output * @param object the {@code Object} whose id to output
*/ */
protected void appendIdentityHashCode(final StringBuffer buffer, final Object object) { protected void appendIdentityHashCode(final StringBuffer buffer, final Object object) {
if (this.isUseIdentityHashCode() && object!=null) { if (this.isUseIdentityHashCode() && object != null) {
register(object); register(object);
buffer.append('@'); buffer.append('@');
buffer.append(Integer.toHexString(System.identityHashCode(object))); buffer.append(ObjectUtils.identityHashCodeHex(object));
} }
} }

View File

@ -41,6 +41,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -63,6 +64,7 @@ public class ObjectUtilsTest {
} }
} }
/** /**
* String that is cloneable. * String that is cloneable.
*/ */
@ -77,6 +79,7 @@ public class ObjectUtilsTest {
return (CloneableString) super.clone(); return (CloneableString) super.clone();
} }
} }
static final class NonComparableCharSequence implements CharSequence { static final class NonComparableCharSequence implements CharSequence {
final String value; final String value;
@ -110,6 +113,7 @@ public class ObjectUtilsTest {
return value; return value;
} }
} }
/** /**
* String that is not cloneable. * String that is not cloneable.
*/ */
@ -119,6 +123,7 @@ public class ObjectUtilsTest {
super(s); super(s);
} }
} }
private static final String FOO = "foo"; private static final String FOO = "foo";
private static final String BAR = "bar"; private static final String BAR = "bar";
private static final String[] NON_EMPTY_ARRAY = { FOO, BAR, }; private static final String[] NON_EMPTY_ARRAY = { FOO, BAR, };
@ -458,6 +463,15 @@ public class ObjectUtilsTest {
assertEquals("a".hashCode(), ObjectUtils.hashCode("a")); assertEquals("a".hashCode(), ObjectUtils.hashCode("a"));
} }
@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 @Test
public void testHashCodeMulti_multiple_emptyArray() { public void testHashCodeMulti_multiple_emptyArray() {
final Object[] array = {}; final Object[] array = {};
@ -485,6 +499,15 @@ public class ObjectUtilsTest {
assertEquals(1, ObjectUtils.hashCodeMulti(array)); assertEquals(1, ObjectUtils.hashCodeMulti(array));
} }
@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 @Test
public void testIdentityToStringAppendable() throws IOException { public void testIdentityToStringAppendable() throws IOException {
final Integer i = Integer.valueOf(121); final Integer i = Integer.valueOf(121);