1
|
/**
|
2
|
* Copyright (C) 2007 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.model.taxon;
|
10
|
|
11
|
import java.io.Serializable;
|
12
|
import java.util.Comparator;
|
13
|
|
14
|
import eu.etaxonomy.cdm.model.name.Rank;
|
15
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
16
|
|
17
|
/**
|
18
|
* @author k.luther
|
19
|
* @date 18.03.2010
|
20
|
*
|
21
|
*/
|
22
|
public class TaxonNodeByRankAndNameComparator implements Serializable, Comparator<TaxonNode> {
|
23
|
private static final long serialVersionUID = 2596641007876609704L;
|
24
|
|
25
|
@Override
|
26
|
public int compare(TaxonNode node1, TaxonNode node2) {
|
27
|
|
28
|
|
29
|
if (node1.getUuid().equals(node2.getUuid())){
|
30
|
return 0;
|
31
|
}
|
32
|
TaxonBase<?> taxon1 = node1.getTaxon();
|
33
|
TaxonBase<?> taxon2 = node2.getTaxon();
|
34
|
|
35
|
TaxonName name1 = taxon1.getName();
|
36
|
TaxonName name2 = taxon2.getName();
|
37
|
|
38
|
Rank rankTax1 = (name1 == null) ? null : name1.getRank();
|
39
|
Rank rankTax2 = (name2 == null) ? null : name2.getRank();
|
40
|
|
41
|
//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
|
42
|
if (rankTax1 == null && rankTax2 != null){
|
43
|
return 1;
|
44
|
}else if(rankTax2 == null && rankTax1 != null){
|
45
|
return -1;
|
46
|
}else if (rankTax1 != null && rankTax1.isHigher(rankTax2)){
|
47
|
return -1;
|
48
|
}else if (rankTax1 == null && rankTax2 == null || rankTax1.equals(rankTax2)) {
|
49
|
if (name1 != null && name2 != null){
|
50
|
//same rank, order by name
|
51
|
int result = name1.compareToName(name2);
|
52
|
if (result == 0){
|
53
|
return taxon1.getUuid().compareTo(taxon2.getUuid());
|
54
|
}else{
|
55
|
return result;
|
56
|
}
|
57
|
}else {
|
58
|
//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
|
59
|
return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
|
60
|
}
|
61
|
}else{
|
62
|
//rankTax2.isHigher(rankTax1)
|
63
|
return 1;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
|
68
|
|
69
|
}
|