New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-transitive equals passes #75

Closed
GoogleCodeExporter opened this Issue Mar 29, 2015 · 1 comment

Comments

Projects
None yet
1 participant
@GoogleCodeExporter

GoogleCodeExporter commented Mar 29, 2015

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

@GoogleCodeExporter

This comment has been minimized.

Show comment
Hide comment
@GoogleCodeExporter

GoogleCodeExporter Mar 29, 2015

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

  • Changed state: Fixed
  • Added labels: ****
  • Removed labels: ****

GoogleCodeExporter commented Mar 29, 2015

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

  • Changed state: Fixed
  • Added labels: ****
  • Removed labels: ****
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment