1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.persistence.dto;
|
10
|
|
11
|
import java.io.Serializable;
|
12
|
import java.util.Comparator;
|
13
|
|
14
|
import eu.etaxonomy.cdm.model.name.Rank;
|
15
|
|
16
|
/**
|
17
|
* @author k.luther
|
18
|
* @since 06.12.2017
|
19
|
*/
|
20
|
public class SortableTaxonNodeQueryResultComparator implements Serializable, Comparator<SortableTaxonNodeQueryResult>{
|
21
|
|
22
|
private static final long serialVersionUID = 6000794425983318091L;
|
23
|
|
24
|
@Override
|
25
|
public int compare(SortableTaxonNodeQueryResult o1, SortableTaxonNodeQueryResult o2) {
|
26
|
//same UUID
|
27
|
|
28
|
if (o1.taxonNodeUuid.equals(o2.taxonNodeUuid)){
|
29
|
return 0;
|
30
|
}
|
31
|
|
32
|
//Rank
|
33
|
Rank rankName1 = Rank.UNKNOWN_RANK();
|
34
|
if (o1.nameRank != null){
|
35
|
rankName1 = o1.nameRank;
|
36
|
}
|
37
|
Rank rankName2 = Rank.UNKNOWN_RANK();
|
38
|
if (o2.nameRank != null){
|
39
|
rankName2 = o2.nameRank;
|
40
|
}
|
41
|
|
42
|
//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
|
43
|
// TODO can't we use OrderedTermBase.performCompareTo here?
|
44
|
if (rankName1 == null && rankName2 != null){
|
45
|
return 1;
|
46
|
}else if(rankName2 == null && rankName1 != null){
|
47
|
return -1;
|
48
|
}else if (rankName1 != null && rankName1.isHigher(rankName2)){
|
49
|
return -1;
|
50
|
}else if (rankName1 == null && rankName2 == null || rankName1.equals(rankName2)) {
|
51
|
if (o1.taxonTitleCache != null && o2.taxonTitleCache != null){
|
52
|
//same rank, order by titleCache
|
53
|
int result = o1.taxonTitleCache.compareTo(o2.taxonTitleCache);
|
54
|
if (result == 0){
|
55
|
return o1.taxonNodeUuid.compareTo(o2.taxonNodeUuid);
|
56
|
}else{
|
57
|
return result;
|
58
|
}
|
59
|
}
|
60
|
}else{
|
61
|
return 1;
|
62
|
}
|
63
|
return 0;
|
64
|
}
|
65
|
}
|