public class RootClass {
static final int
a1Default = 0,
a2Default = 0;
private int a1 = a1Default;
private int a2 = a2Default;
public boolean equals(Object other) {
// identical
if (other == this) return true;
// null
if (other == null) return false;
// incomparable types
if (! (other instanceof RootClass)) return false;
return _navigateClassHierarchy(other,false);
}
protected boolean _navigateClassHierarchy(Object other, boolean
reverseOrder) {
if (other instanceof RootClass && !reverseOrder)
{ // reverse order
return
((RootClass)other)._navigateClassHierarchy(this,true);
}
else
{ // compare my fields
if(!_compareFields(other)) return false;
// since we are the root, succeed
return true;
}
}
private boolean _compareFields(Object other) {
if (other instanceof RootClass) { // at least my type,
// check fields
RootClass myType = (RootClass)other;
if (a1 != myType.a1
|| a2 != myType.a2) return false;
} else { // check defaults
if (a1 != a1Default
|| a2 != a2Default) return false;
}
return true;
}
}
End of Listing