This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
First of all, I don't like the name hasChanged() here, since you are actually comparing two different instances. A hasChanged() method implies that a single instance has changed its state since the last checkpoint, where checkpoints are defined in some way meaningful to your system. You could accomplish that sort of check by having every set method also flip on a "dirty" flag in the instance. In fact, we had to do something like this in the very early days of ORM, even with Entity Beans from EJB 1.0 if I remember right. Of course, you'll also have to have some way to turn off the dirty flag whenever that makes sense for your application.
What you've implemented looks more like an equals() method. Unfortunately, there's no better way to do that other than just writing out the code, but some IDEs have tools that will do the typing for you. I don't understand the 24 out of 30 requirement. Could you be clearer about what you're trying to do?
You could create a Set for the specific properties.
Everytime the setAttr() methods is called on an attribute add it to the Set.
When the size of the Set is 24 you have all 24 attributes changed.
The name of the attribute can be used as the object for the Set.
When the time is right you can remove all the elements from the Set.
This approach is different from what you wanted.
But it can be a workaround.
Hope this is helpful.
Joined: Jan 14, 2004
Thanks Greg & Preity.
Greg , you are right , what am comparing is the UI entity versus its old copy that was stored in the DB. They are different class types for sure , but have the same set of properties.
By 24 out of 30 I meant the dirty flag had to be set only when certain properties are updated and not all.
Preity , thanks for the input. I was trying to get to a generic way of coding this versus writing it all out in a block of If statement with multiple OR conditions.