DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

An option to insert the identity hashCode in ToStringHelper #1491

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Sometimes when debugging from the logs it's useful to distinguish two identical 
but different objects.

I suggest an option for ToStringHelper (as a factory method, or as a 
ToStringHelper option method like omitNulls()) to insert the identity hashCode 
as returned by System.identityHashCode().

This way we can construct the following String:
"ClassName@1f2e3d4c{x=1}"

Currently, I usually do it like this:

  return Objects.toStringHelper(getClass().getSimpleName() + '@' + Integer.toHexString(System.identityHashCode()))
    .add("x", 1)
    .toString();

or something like this:

  return Objects.toStringHelper(this)
    .add("@", System.identityHashCode(this))
    .add("x", 1)
    .toString(); 

Both of which are inelegant and/or underperforming.

Original issue reported on code.google.com by ogregoire on 1 Aug 2013 at 10:40

GoogleCodeExporter commented 9 years ago
Hmm... I don't feel like your current solutions are too bad, and this doesn't 
feel like a very common need to me. A quick search has shown 2 places in our 
codebase using the identity hash code with toStringHelper. Still, I don't have 
a very strong opinion on this.

Original comment by cgdecker@google.com on 2 Aug 2013 at 9:21

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08