I’m trying to implement an object equality based on the following requirements:
class MyObject {
String x;
String y;
}
1) if both properties on the 2 objects being compared are non-null then they are equal if x.equals(x) && y.equals(y)
2) if at least one x is null on any of the objects being compared then they are equal if y.equals(y) (assuming both y are non-null)
3) if at least one y is null on any of the objects being compared then they are equal if x.equals(x) (assuming both x are non-null)
After a few attempts I’m starting to think that is not possible to achieve the transitive relationship of the equals method based on these requirements. Comments are welcome! Thks.
Just from those three requirements, you’re right; you don’t get transitivity.
Take these examples:
By what you say, the first and second are equal, the second and third are equal, but the first and third are not.
What you probably want is to add the restrictions that 1) if one x is null, the other must be as well for them to be equal and 2) same for the ys.