[josm-dev] [PATCH 01/26] Enhance OsmPrimitive a bit

Dave Hansen dave at sr71.net
Wed Apr 30 20:19:07 BST 2008


On Wed, 2008-04-30 at 10:55 +0200, Petr Nejedly wrote:
> 
> BTW: There's _no_ semantic change anyway, taking into account the
> semantics
> of Object.equals (as Object is the direct superclass).
> Both implementations would return false.
> Anyway delegating to super implementation in case it will always
> return false
> still makes the code obscure and consider my implementation (in
> josm-ng) clearer:
> 
>     public @Override boolean equals(Object obj) {
>         if (id == 0) return obj == this;
>         if (obj instanceof OsmPrimitive) { // not null too
>             return ((OsmPrimitive)obj).id == id && obj.getClass() ==
> getClass();
>         }
>         return false;
>     }

I'm perfectly fine with doing that.  Could we try and make it a bit
readable, though?  What about this:

public @Override boolean equals(Object obj)
{
	// Unassigned id, only equal if exact same object
        if (id == 0)
		return obj == this;
	// Never equal to different types
        if (!(obj instanceof OsmPrimitive)) // covers null
		return false;
	// Isn't this somewhat redundant with the above?
	// How about an explicit null check along with this:
	if (this.getClass() != obj.getClass())
		return false;
        return ((OsmPrimitive)obj).id == id;
}

-- Dave





More information about the josm-dev mailing list