June 23, 2012

Indentify instances

Sometimes, you may need to identify object instances in a log. You can use Class<?>.getSimpleName() with System.instanceHashCode(instance) for this, but then log looks like this:
DEBUG Instance MainPage-52219193 serialized.
DEBUG Instance MainPage-43312674 deserialized.
DEBUG Instance MainPage-52219193 touched.
Not very readable, especially in case of mixing more instances of same class. I want to identify separate instances by their order in log. Just like this:
DEBUG Instance MainPage-1 serialized.
DEBUG Instance MainPage-2 deserialized.
DEBUG Instance MainPage-1 touched.
In short I want similar feature to Eclipse instance counter in Debug view, but there is no such thing in Java API, so I try implement it in very simple way.

I use outstanding Google Guava library, namely beautiful MapMaker class and computing maps: