CostLineDisplayTemplateComparator.java
package com.tradecloud.domain.costing.utils;
import com.tradecloud.domain.costing.CostGroup;
import com.tradecloud.domain.costing.CostLine;
import java.util.Comparator;
public class CostLineDisplayTemplateComparator implements Comparator<CostLine> {
@Override
public int compare(CostLine costLine1, CostLine costLine2) {
CostGroup costGroup1 = costLine1.getCostLineTemplate().getCostGroup();
CostGroup costGroup2 = costLine2.getCostLineTemplate().getCostGroup();
/*
* Java spec on Enum compareTo:
* ...The natural order implemented by this method is the order in which the constants are declared...
*/
int compareTo = costGroup1.compareTo(costGroup2);
if (compareTo == 0) {
return compareByName(costLine1, costLine2);
}
return compareTo;
}
private int compareByName(CostLine costLine1, CostLine costLine2) {
String costLineName1 = costLine1.getCostLineTemplate().getName();
String costLineName2 = costLine2.getCostLineTemplate().getName();
if (costLineName1 != null && costLineName2 != null) {
return costLineName1.compareTo(costLineName2);
} else if (costLineName1 == null && costLineName2 == null) {
return 0;
} else if (costLineName1 != null) {
return -1;
} else if (costLineName2 != null) {
return 1;
}
return 0;
}
}