What do you mean by modified? As in have any properties changed?
One way that is possible that I've done before, is creating a deep clone of the object using the Serialization API.
ie: create an ObjectOutputStream, and write the object into a ByteArrayOutputStream. Now, you have a set of bytes which is a serialized version of the object at a given point in time.
When you want to see if it has changed, you can do the same thing, and compare the two byte arrays.
The potentional problems with this are:
1) The object needs to be serializable
2) If your object has another object as a property and that object changes, the parent object will be considered changed too. (Though, this may be desired.)
Here's some code:
private static byte[] getObjectBytes(Object object) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
new ObjectOutputStream (output).writeObject( object );
output.flush();
return output.toByteArray();
}
// These go in the object
private byte[] digest;
public void mark() {
digest = getObjectBytes(this);
}
public boolean hasChanged() {
return !Arrays.equals(getObjectBytes(this), digest);
}