• Post Reply Bookmark Topic Watch Topic
  • New Topic

generic equals method  RSS feed

 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at http://commons.apache.org/lang/api/org/apache/commons/lang/builder/EqualsBuilder.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!