| Author |
generic equals method
|
manish ahuja
Ranch Hand
Joined: Oct 23, 2003
Posts: 312
|
|
Hi All, I want to know if we can have a generic equals method at the Base class level or something like EqualsUtil class. What I am trying to achieve is a standard equals method for all the domain objects in our application. So say if I have two instances of class DomainA I should be able to just pass the two instances or just invoke equals on one of them. I am looking for a generic equals method which can work for all the domain objects. The domain object may even comprise of other instance level objects and not just primitive type instance variables. Please post your suggestions and pointers which can be of help. Thanks in Advance Manish
|
 |
Pat Farrell
Rancher
Joined: Aug 11, 2007
Posts: 4422
|
|
I'd simply override equals(HigestCommonDomain arg) in each class. You could use reflection, but that seems ugly, and you'll still have to implement a class-specific equals() function. Remember that if you override equals() you generally have to override hashcode() as well, and hashcode() can be painful if your objects are not immutable.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
Take a look at http://commons.apache.org/lang/api/org/apache/commons/lang/builder/EqualsBuilder.html
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: generic equals method
|
|
|