I have implemented the following solution for equals and hashCode, in this
example the object is a "Policy.class".
Can anyone see any problems with this solution?

        public boolean equals(final Object obj) {
                try {
                        return equals((Policy) obj);
                } catch (ClassCastException e) {
                        return false;
                }
        }

        public boolean equals(final Policy other) {
                if (this == other) {
                        return true;
                }

                if (other == null) {
                        return false;
                }

                if ((getId() == 0) || (other.getId() == 0)) {
                        return false;
                } else {
                        return (new EqualsBuilder().append(getId(),
other.getId()))
                                        .isEquals();
                }
        }

        private Integer unstoredHashCode = null;
        public int hashCode() {
                if (getId() == 0) {
                        if (unstoredHashCode == null) {
                                unstoredHashCode =
UniqueHashCodeGenerator.getInstance()
                                                .nextHashCodeInteger();
                        }
                        return unstoredHashCode.intValue();
                } else {
                        return new
HashCodeBuilder().append(getClass()).append(getId())
                                        .toHashCode();
                }
        }


The reason for the "unstoredHashCode" is that we have users of this class
that require use of hashCodes to indicate unique objects and this provides
an easier number than the stock standard hashCode on the super class
(Object).

 -- Cory Prowse

 -----Original Message-----
From:   Thomas Dudziak [mailto:[EMAIL PROTECTED] 
Sent:   Thursday, 10 November 2005 11:39 PM
To:     OJB Users List; [EMAIL PROTECTED]
Subject:        Re: When to overwrite equals() and hashCode()

On 11/10/05, werner <[EMAIL PROTECTED]> wrote:

> Thanks for your answer. The hashCode function is a little mystery to me.
> I use a standard database setup where every table has a primary key
> (auto increment integer). Is it a proper solution to add the hashcode of
> all fields except the primary and return the result as the new hashcode
> of the object? What happens if I create 2 new objects with empty fields.
> They would always be considered as equal until I put some data into the
> object. So I think it wouldn't work.

The basic contract defined in java.lang.Object is that if two objects
are consided equal by the equals() method than they shall have the
same hash code (have a look at the javadoc of Object).
The method that I generally use is to use the hashcode builder from
commons-lang:

http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/builder/H
ashCodeBuilder.html

The basic usage pattern is that you append all fields to the builder
that you also use in the equals method.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to