powered by Jive Software

Bug in implemenation of JID.compareTo()

I tried to use a TreeMap<JID,Element> and spend hours for searching this bug:

node@domain compared with domain returns zero, which is defined as equal!

Same goes for node@domain/resource compared with node@domain.

(minor bug: typo "Object not instanceof JID: ")

The method compareTo is implemented as follows:

public int compareTo(Object o) {
     if (!(o instanceof JID)) {
          throw new ClassCastException("Ojbect not instanceof JID: " + o);
     }
     JID jid = (JID)o;      // Comparison order is domain, node, resource.
     int compare = domain.compareTo(jid.domain);
     if (compare == 0 && node != null && jid.node != null) {
          compare = node.compareTo(jid.node);
     }
     if (compare == 0 && resource != null && jid.resource != null) {
          compare = resource.compareTo(jid.resource);
     }
     return compare;
}

Another bug in JID class:

JID implements Comparable, not Comparable

So you will get an unchecked warning from your compiler when you e.g. do the following:

ArrayList<JID> list = new ArrayList<JID>(); // ... Collections.sort(list);

Coolcat