I thought I should have added the equals and hashCode methods, so I could shorten it by using the library of ʻorg.apache.commons`.
Hoge.java
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
~~abridgement~~
@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
If you are using gradle, see below.
build.gradle
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
Since it was only an old article, I looked up other things and it seemed that the following things could be realized.
Hoge.java
@Override
public int hashCode(){
return Objects.hash(name, length, children);
}
@Override
public boolean equals(final Object obj){
if(obj instanceof Bean){
final Bean other = (Bean) obj;
return Objects.equals(name, other.name)
&& length == other.length // special handling for primitives
&& Objects.equals(children, other.children);
} else{
return false;
}
}
ʻObjects` was taken care of by null check, but I didn't know that there was such a method.
I feel that it could be done automatically by Eclipse, but I personally think that it is fine if it works.
Recommended Posts