I would like to use a clean/automatic way to check if a Java Object has been modified.
My specific problem is the following:
In my Java application, I use XStream library to deserialize XML to Java Objects, then the user can modify or change them. I'd like a way to check if these Objects in memory are at some point different from the serialized ones, so I can inform the user and ask him if he want to save the changes (i.e. serialize using XStream) or not.
In my application there are many Objects and are quite complex.
Please consider that I don't use databases in my application, so I'm not interested in solutions like using hibernate.
Asked
Active
Viewed 1.0k times
5

dawww
- 371
- 1
- 4
- 16
-
Take a look at the memento pattern http://en.wikipedia.org/wiki/Memento_pattern – Evgeni Dimitrov Mar 22 '14 at 13:09
-
implement a counter, after every change increment the value. – Klemens Morbe Mar 22 '14 at 13:10
-
What do you mean by 'different'? Class changes New/removed fields, or just different values in the same fields? – lreeder Mar 22 '14 at 13:19
-
1Maybe `extends Observable` and implement the needed `Observer`(s)? – fge Mar 22 '14 at 13:24
2 Answers
5
Two approaches:
- Implement a hashcode for your objects, and compare the hashcode of the in-memory objects against the hashcode of the serialized objects to see if they've been changed. This is has a low impact on your class design, but performance will go down as O(n^2) as the number of objects increases. Note that two objects might return the same hashcode, but a good hashing implementation will make this very unlikely. If you are concerned about this, implement and use your own
equals()
method. - Have your objects implement the Observer pattern and have each setter method, or any other method that modifies the object, notify the observer when it's called. Performance will be better for large numbers of objects (as long as they aren't changing constantly), but it requires you to introduce Observer code into possibly lightweight classes. Java provides a utility interface for Observable, but you'll still need to do most of the work.

lreeder
- 12,047
- 2
- 56
- 65
-
Thanks, the Java documentation for the hashcode method says that the integer resulting from the method need not remain consistent from one execution of an application to another execution of the same application. Is there a way to guarantee that it remains the same? – dawww Mar 22 '14 at 13:52
-
1This is true for autogenerated hashcodes which use the object memory address for the hash. You can implement your own hashcode so that it generates the same hash everytime for an object with the same internal fields. Note that a hashing algorithm might return the same hashcode for two different objects. A good hashing algorithm will make this unlikely buy not impossible. I updated my answer with this caveat. See http://stackoverflow.com/questions/113511/hash-code-implementation for ways to implement your own good hashcode. – lreeder Mar 22 '14 at 14:11
1
You can store a version field in this object, whenever the object changed it should update its version field (increment it), you can then compare the version field with the serialized object version field

bennyl
- 2,886
- 2
- 29
- 43
-
Thanks, I had already thought about it but I wanted to know if there were more automatic way. In fact I have to take into account also changes that are made and unmade, and it is complex. – dawww Mar 22 '14 at 13:26