Skip to content
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
Closed

Non-transitive equals passes #75

GoogleCodeExporter opened this issue Mar 29, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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
Projects
None yet
Development

No branches or pull requests

1 participant