File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes generic equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "generic equals method" Watch "generic equals method" New topic
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
    
    2

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: generic equals method
 
Similar Threads
Doubt in Collections
Hibernate question: persistent identity
Object locks
Chapter 2 K & B doubt
Are my answers correct ?