If you want to check class equivalence in Java, you need to implement ʻequals () and hashCode () `properly [^ 1]. Normally, I don't implement it myself, so I think there is no mistake, but I'm worried about not testing it.
When I was looking for an efficient method, I found a library called EqualsVerifier [].
Getting Started describes how to set in Maven. For other tools, see Mvn Repository.
For example, there is the following Name class. These ʻequals () and hashCode () `are automatically generated by IntelliJ IDEA.
import java.util.Objects;
public final class Name {
private final String name;
public Name of(String name) {
return new Name(name);
}
public Name(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Name name1 = (Name) o;
return Objects.equals(name, name1.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
Here's a test for this (using JUnit 4.12.
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;
import java.sql.Timestamp;
public final class EqualsVerifierTest {
@Test
public void test_name() {
EqualsVerifier.forClass(Name.class).verify();
}
}
Only this. It's easy.
Of course, I don't know if I really tested it, so for example, I test it with the Timestamp [] class, which does not follow the general contract of ʻequals ()`.
EqualsVerifier.forClass(Timestamp.class).verify();
I got the following error.
java.lang.AssertionError: Overloaded: More than one equals method found. Signature should be: public boolean equals(Object obj) For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
It can be found in Ignoring fields, but can be excluded, for example: See the documentation for details.
withIgnoredFields methodtransientI think it's better to use transient for myself.
It is written at the bottom of Why, what, how?, but it is roughly as follows. It's a little black magic.
and hashCode () `get the expected results for these.I thought it was important to have a feeling of comfort (´ω`)
[^ 1]: Effective Java 2nd Edition See "Item 8 Follow the general contract when overriding equals" and "Item 9 Always override hashCode when overriding equls".
Recommended Posts