I learned about the Comparable interface, for which a class must implement compareTo method. A project I am using that method as:
public class EmployeeAssignmentTotal implements Comparable<EmployeeAssignmentTotal>, Serializable {
private Employee employee;
private int total;
....
public int compareTo(EmployeeAssignmentTotal other) {
return new CompareToBuilder()
.append(employee, other.employee)
.append(total, other.total)
.toComparison();
}
What exacly does CompareToBuilder do here? And how is it interacting with the employee and total attributes?
I did read the javadocs, but I cant make head or tail of what they are doing with the constructor and the multiple appends. Does this question indicate unclear intentions and zero research?
This class is meant to assist you in building
compareTo()-methods. Imagine you had more than just 2 fields in your class – manually coding your comparison-method could be quite cumbersome.CompareToBuilder is doing that for you – each
append()method is adding a new comparison, and all comparisons are&&ed.So the code you posted runs
equals()on the employee object andtotal.