Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Type safe enums, class loaders and equals

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Elliotte Rusty Harold <elharo AT metalab.unc.edu>
  • To: xom-interest <xom-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] Type safe enums, class loaders and equals
  • Date: Fri, 17 Jan 2003 21:02:47 -0500

At 8:17 PM -0500 1/17/03, Elliotte Rusty Harold wrote:
Going over old messages about XOM, I thought I had fixed the
problems with class loading and type safety in the
Attribute.Type inner class. Turns out I was wrong.
Here's the currnet equals() method:

public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Type)) return false;
Type t = (Type) o;
return t.type == this.type;
}



On further reflection, I've figured out a relatively clean way to do this. As well as comparing the class and package names, I have to compare the hash codes. I have to use the hashCode() because I need a method in Object that's guaranteed to be available to both versions of the Attribute.Type class.

I just hope none of my students catch me doing this. Every semester I take off lots of points on homework from students who implement equals by comparing the results of either hahsCode() or toString() instead of implementing the equals() method directly. :-)
--

+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo AT metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
| Processing XML with Java (Addison-Wesley, 2002) |
| http://www.cafeconleche.org/books/xmljava |
| http://www.amazon.com/exec/obidos/ISBN%3D0201771861/cafeaulaitA |
+----------------------------------+---------------------------------+
| Read Cafe au Lait for Java News: http://www.cafeaulait.org/ |
| Read Cafe con Leche for XML News: http://www.cafeconleche.org/ |
+----------------------------------+---------------------------------+




Archive powered by MHonArc 2.6.24.

Top of Page