import java.util.Comparator;
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
-import eu.etaxonomy.cdm.compare.OrderIndexComparator;
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByNameComparator;
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
/**
* @author k.luther
*/
public class TaxonDistributionDtoComparator implements Comparator<TaxonDistributionDTO> {
- @Override
- public int compare(TaxonDistributionDTO arg0, TaxonDistributionDTO arg1) {
- if (arg0 == arg1){
- return 0;
- }
-
- if (arg0.getTaxonUuid().equals(arg1.getTaxonUuid())){
- return 0;
- }
+ Comparator<TaxonNodeDto> comparator1 = new TaxonNodeDtoByRankAndNameComparator();
+ Comparator<TaxonNodeDto> comparator2 = new TaxonNodeDtoByNameComparator();
- String name1 = arg0.getNameCache();
- String name2 = arg1.getNameCache();
- Integer rankTax1 = arg0.getRankOrderIndex();
- Integer rankTax2 = arg1.getRankOrderIndex();
+ public TaxonDistributionDtoComparator(){
+ super();
+ }
- int rankOrder = OrderIndexComparator.instance().compare(rankTax1, rankTax2);
+ @Override
+ public int compare(TaxonDistributionDTO taxonDistr0, TaxonDistributionDTO taxonDistr1) {
- if (rankOrder == 0) {
- //same rank, order by name
- return name1.compareTo(name2);
+ //if parent is not the same sort by name
+ if ((taxonDistr0.getTaxonNodeDto() != null && taxonDistr1.getTaxonNodeDto() != null) && !taxonDistr0.getTaxonNodeDto().getParentUUID().equals(taxonDistr1.getTaxonNodeDto().getParentUUID()) ){
+ return comparator2.compare(taxonDistr0.getTaxonNodeDto(), taxonDistr1.getTaxonNodeDto());
+ }
+ // if parent is the same sort by rank and name
+ if (taxonDistr0.getTaxonNodeDto() != null && taxonDistr1.getTaxonNodeDto() != null && taxonDistr0.getTaxonNodeDto().getParentUUID().equals( taxonDistr1.getTaxonNodeDto().getParentUUID())){
+ return this.comparator1.compare(taxonDistr0.getTaxonNodeDto(), taxonDistr1.getTaxonNodeDto());
+ }else if (taxonDistr0 == taxonDistr1 || (taxonDistr0.getTaxonNodeDto() == null) && (taxonDistr1.getTaxonNodeDto() == null)){
+ return 0;
+ } else if (taxonDistr0.getTaxonNodeDto() != null && taxonDistr1.getTaxonNodeDto() == null){
+ return -1;
+ } else if (taxonDistr0.getTaxonNodeDto() == null && taxonDistr1.getTaxonNodeDto() != null){
+ return 1;
+ }else{
+ return taxonDistr0.getTaxonUuid().compareTo(taxonDistr1.getTaxonUuid());
}
- return rankOrder;
-
-
}
}