You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider this class:
public final class School {
private final String name;
private final String nickname;
public School(String name, String nickname) {
this.name = name;
this.nickname = nickname;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof School)) {
return false;
}
School other = (School)obj;
return nullSafeEqual(name, other.name) ||
nullSafeEqual(nickname, other.nickname);
}
private boolean nullSafeEqual(String a, String b) {
return a == null ? b == null : a.equals(b);
}
@Override
public int hashCode() {
return 42;
}
@Override
public String toString() {
return "School: name=" + name + ", nickname=" + nickname;
}
}
This equals method is non-transitive, as the following code demonstrates:
School red = new School("A", "1");
School green = new School("A", "2");
School blue = new School("B", "2");
assertTrue(red.equals(green));
assertTrue(green.equals(blue));
assertTrue(red.equals(blue)); // fails
However, EqualsVerifier doesn't fail on this.
Original issue reported on code.google.com by jan.ouw...@gmail.com on 16 Feb 2013 at 7:45
The text was updated successfully, but these errors were encountered:
This has been fixed in version 1.2. Also, I've blogged about the solution:
* http://www.jqno.nl/post/2013/02/17/reaction-to-cedric-beusts-equals-challenge/
* http://www.jqno.nl/post/2013/03/26/on-transitivity/
Original comment by jan.ouw...@gmail.com on 26 Mar 2013 at 6:57
Original issue reported on code.google.com by
jan.ouw...@gmail.com
on 16 Feb 2013 at 7:45The text was updated successfully, but these errors were encountered: