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
|
import java.util.UUID;
|
14
|
|
15
|
import eu.etaxonomy.cdm.model.name.Rank;
|
16
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
17
|
|
18
|
/**
|
19
|
* @author k.luther
|
20
|
\* @since 18.03.2010
|
21
|
*
|
22
|
*/
|
23
|
public class TaxonNodeByRankAndNameComparator implements Serializable, Comparator<TaxonNode> {
|
24
|
private static final long serialVersionUID = 2596641007876609704L;
|
25
|
|
26
|
@Override
|
27
|
public int compare(TaxonNode node1, TaxonNode node2) {
|
28
|
|
29
|
boolean node1Excluded = node1.isExcluded();
|
30
|
boolean node2Excluded = node2.isExcluded();
|
31
|
boolean node1Unplaced = node1.isUnplaced();
|
32
|
boolean node2Unplaced = node2.isUnplaced();
|
33
|
|
34
|
|
35
|
if (node1.getUuid().equals(node2.getUuid())){
|
36
|
return 0;
|
37
|
}
|
38
|
//They should both be put to the end (first unplaced then excluded)
|
39
|
if (node2Excluded && !node1Excluded){
|
40
|
return -1;
|
41
|
}
|
42
|
if (node2Unplaced && !(node1Unplaced || node1Excluded)){
|
43
|
return -1;
|
44
|
}
|
45
|
|
46
|
if (node1Excluded && !node2Excluded){
|
47
|
return 1;
|
48
|
}
|
49
|
if (node1Unplaced && !(node2Unplaced || node2Excluded)){
|
50
|
return 1;
|
51
|
}
|
52
|
|
53
|
if (node1Unplaced && node2Excluded){
|
54
|
return -1;
|
55
|
}
|
56
|
if (node2Unplaced && node1Excluded){
|
57
|
return 1;
|
58
|
}
|
59
|
|
60
|
|
61
|
TaxonBase<?> taxon1 = node1.getTaxon();
|
62
|
TaxonBase<?> taxon2 = node2.getTaxon();
|
63
|
|
64
|
TaxonName name1 = (taxon1 == null) ? null : taxon1.getName();
|
65
|
TaxonName name2 = (taxon2 == null) ? null : taxon2.getName();
|
66
|
|
67
|
Rank rankTax1 = (name1 == null) ? null : name1.getRank();
|
68
|
Rank rankTax2 = (name2 == null) ? null : name2.getRank();
|
69
|
|
70
|
//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
|
71
|
if (rankTax1 == null && rankTax2 != null){
|
72
|
return 1;
|
73
|
}else if(rankTax2 == null && rankTax1 != null){
|
74
|
return -1;
|
75
|
}else if (rankTax1 != null && rankTax1.isHigher(rankTax2)){
|
76
|
return -1;
|
77
|
}else if (rankTax1 == null && rankTax2 == null || rankTax1.equals(rankTax2)) {
|
78
|
if (name1 != null && name2 != null){
|
79
|
//same rank, order by name
|
80
|
int result = name1.compareToName(name2);
|
81
|
if (result == 0){
|
82
|
return getTaxonUuid(taxon1, node1).compareTo(getTaxonUuid(taxon2, node2));
|
83
|
}else{
|
84
|
return result;
|
85
|
}
|
86
|
}else {
|
87
|
//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
|
88
|
return getTaxonTitle(taxon1, node1).compareTo(getTaxonTitle(taxon2, node2));
|
89
|
}
|
90
|
}else{
|
91
|
//rankTax2.isHigher(rankTax1)
|
92
|
return 1;
|
93
|
}
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* @param taxon1
|
98
|
* @return
|
99
|
*/
|
100
|
public String getTaxonTitle(TaxonBase<?> taxon, TaxonNode node) {
|
101
|
return (taxon == null) ? node.getUuid().toString(): taxon.getTitleCache();
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* @param taxon
|
106
|
* @param node
|
107
|
* @return
|
108
|
*/
|
109
|
private UUID getTaxonUuid(TaxonBase<?> taxon, TaxonNode node) {
|
110
|
return (taxon == null) ? node.getUuid(): taxon.getUuid();
|
111
|
}
|
112
|
|
113
|
|
114
|
|
115
|
}
|