Wednesday, March 7, 2007

tree traversal "equals" implementation

I tested the "tree traversal" approach to the implementation of equals.

It is not so straighforward to understand, but you don't need to do it to be sure it works. I just wrote unit tests that checks that simmetry, transitivity contract is respected combining class/subclasses and differnet branches of a same class herarchy. It is enough to prove thet it works.

Using this approach, if you extend any class A to class B, and you redefine the "equals" method for A, then for two object a1, and a2, belonging to A, initialized in the same way, a1.equals(a2) is true, and a1.equals(b1) is true for any b belonging to B initialized int the same way of a1 (and eventual extra fields that are in b and not in a, must have default values).

No comments: