Equalizer
Equalizer is a helper class in order to create nice equals method for your java classes.
There are some alternatives like commons-lang, guava or Object.equals, but all have the same problem:
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Data other = (Data) obj;
return Objects.equals(this.id, other.id) && Objects.equals(this.value, other.value);
}
There are some boilerplate code you have to include previously to the fields comparison.
Equalizer tries to solve this problem:
@Override
public boolean equals(Object obj) {
return equalizer(this)
.append((one, other) -> Objects.equals(one.id, other.id))
.append((one, other) -> Objects.equals(one.value, other.value))
.applyTo(obj);
}