views:

90

answers:

3

I am looking for a library that, given a tree of objects, performs a message digest or hashing on the entire structure.

I want to see if an object passed to a method gets modified or not (this object contains other objects, who contain objects and so on – none are immutable).

Is there a way to check if the state of any object in the structure changes during the call?

+1  A: 

You could implement hashCode() for all the objects in the tree. If the hashCode of the root object has changed you know that the state of some object in the tree has changed.

Mark
There are a lot of classes that must be modified to do this. I also have Maps that contain objects (not always the same objects). I will have to either see what are all the objects that can make part of the tree and modify them all to add the hash code computation, or to see what the code (from all the methods that use the tree) does to the tree. I could also use reflection to extract data recursively from the tree, but I am looking for a faster way. Does such a tool exist?
dpb
A: 

The problem with using hashing to detect changes is that it doesn't provide an absolute "no" answer. It just tells you that something probably hasn't been changed.

If you're okay with missing some changes, then sure, hash over the tree.

If not, you'd have to consider other options, but I'd need to hear more about your situation before I could suggest something.

CPerkins
A: 

I've done that using the standard MessageDigest and MD5 provided with the JRE.

One link

Concerning Collisions, when I have 2 objects with the same MD5, I know they're potentially the same, so I make a complete recursive comparison. Still, collisions are unlikely and it's really worth implementing this technique.

LB