javaequalsxom

equals() for subclasses of XOM Element


I have created subclasses of XOM Element and would like to use them with Containers such as Set or HashMap. However these require an equals(Object obj) method. I have implemented an algorithm for equality but cannot use it as XOM.Node() declares equals(Object obj) to be final. Is there any way I can easily overcome this? [I could create a wrapper / delegate if that is the only way].


Solution

  • All XOM nodes have equals methods and hashCode methods, as do all Java objects, use them with Containers such as Set or HashMap. The decision to support only identity equality was deliberate, and that's why the equals method is final. The question is why do you need to use a different notion of equality? E.g. if two nodes look the same but are in different positions, or are in different documents, they are usually considered to be different elements. What's the use case for treating them as the same?