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
|
|
10
|
package eu.etaxonomy.cdm.model.taxon;
|
11
|
|
12
|
import java.util.Comparator;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
19
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
20
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
21
|
|
22
|
/**
|
23
|
* This class orders synonyms of a homotypic group,
|
24
|
* first by
|
25
|
* <ul>
|
26
|
* <li>Basionym groups (the basionym and all names derived from this basionym)
|
27
|
* should be kept together in a subgroup</li>
|
28
|
* <li>The order of the subgroups is defined by the ordering of their
|
29
|
* basionyms (according to the following ordering)</li>
|
30
|
* <li>If a name is illegitimate or not does play a role for ordering</li>
|
31
|
* <li>Names with publication year should always come first</li>
|
32
|
* <li>Names with no publication year are sorted by rank</li>
|
33
|
* <li>Names with no publication year and equal rank are sorted alphabetically</li>
|
34
|
* <li>If 2 names have a replaced synonym relationship the replaced synonym comes first,
|
35
|
* the replacement name comes later as this reflects the order of publication</li>
|
36
|
* </ul>
|
37
|
*
|
38
|
* Details on ordering are explained at http://dev.e-taxonomy.eu/trac/ticket/3338<BR>
|
39
|
*
|
40
|
* @author a.mueller
|
41
|
* @created 02.03.2016
|
42
|
*/
|
43
|
public class HomotypicGroupTaxonComparator extends TaxonComparator {
|
44
|
private static final long serialVersionUID = -5088210641256430878L;
|
45
|
@SuppressWarnings("unused")
|
46
|
private static final Logger logger = Logger.getLogger(HomotypicGroupTaxonComparator.class);
|
47
|
|
48
|
private final TaxonBase<?> firstTaxonInGroup;
|
49
|
private final TaxonNameBase<?,?> firstNameInGroup;
|
50
|
// private final HomotypicalGroupComparator homotypicGroupComparator;
|
51
|
|
52
|
/**
|
53
|
* @param firstNameInGroup
|
54
|
*/
|
55
|
public HomotypicGroupTaxonComparator(@SuppressWarnings("rawtypes") TaxonBase firstTaxonInGroup) {
|
56
|
super(true);
|
57
|
this.firstTaxonInGroup = firstTaxonInGroup;
|
58
|
this.firstNameInGroup = firstTaxonInGroup == null ? null: firstTaxonInGroup.getName();
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* @param firstNameInGroup
|
63
|
*/
|
64
|
public HomotypicGroupTaxonComparator(@SuppressWarnings("rawtypes") TaxonBase firstTaxonInGroup, boolean includeRanks) {
|
65
|
super(includeRanks);
|
66
|
this.firstTaxonInGroup = firstTaxonInGroup;
|
67
|
this.firstNameInGroup = firstTaxonInGroup == null ? null: firstTaxonInGroup.getName();
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
*
|
72
|
* @see TaxonComparator#compare(TaxonBase, TaxonBase)
|
73
|
* @see java.lang.String#compareTo(String)
|
74
|
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
75
|
*/
|
76
|
@Override
|
77
|
public int compare(
|
78
|
@SuppressWarnings("rawtypes") TaxonBase taxonBase1,
|
79
|
@SuppressWarnings("rawtypes") TaxonBase taxonBase2) {
|
80
|
|
81
|
TaxonNameBase<?,?> name1 = taxonBase1.getName();
|
82
|
TaxonNameBase<?,?> name2 = taxonBase2.getName();
|
83
|
// System.out.println(name1.getTitleCache() +" : "+ name2.getTitleCache());
|
84
|
|
85
|
|
86
|
int compareStatus = compareStatus(name1, name2);
|
87
|
if (compareStatus != 0){
|
88
|
return compareStatus;
|
89
|
}
|
90
|
|
91
|
//not same homotypical group -
|
92
|
//NOTE: this comparator should usually not be used
|
93
|
// for comparing names of different homotypical groups.
|
94
|
// The following is only to have a defined compare behavior
|
95
|
// which follows the contract of Comparator#compare.
|
96
|
if (name1 == null ||
|
97
|
name2 == null ||
|
98
|
! name1.getHomotypicalGroup().equals(name2.getHomotypicalGroup())){
|
99
|
|
100
|
String compareString1 = name1 != null ?
|
101
|
name1.getHomotypicalGroup().getUuid().toString() :
|
102
|
taxonBase1.getUuid().toString();
|
103
|
String compareString2 = name2 != null ?
|
104
|
name2.getHomotypicalGroup().getUuid().toString() :
|
105
|
taxonBase2.getUuid().toString();
|
106
|
int result = compareString1.compareTo(compareString2);
|
107
|
return result;
|
108
|
}
|
109
|
|
110
|
//same homotypical group ...
|
111
|
//one taxon is first in group
|
112
|
if (taxonBase1.equals(firstTaxonInGroup)){
|
113
|
return -1;
|
114
|
}else if (taxonBase2.equals(firstTaxonInGroup)){
|
115
|
return 1;
|
116
|
}
|
117
|
|
118
|
//same name => compare on taxon level
|
119
|
if (name1.equals(name2)){
|
120
|
return super.compare(taxonBase1, taxonBase2); //if name is the same compare on taxon level
|
121
|
}
|
122
|
|
123
|
TaxonNameBase<?,?> basionym1 = getPreferredInBasionymGroup(name1);
|
124
|
TaxonNameBase<?,?> basionym2 = getPreferredInBasionymGroup(name2);
|
125
|
|
126
|
int compareResult;
|
127
|
if (basionym1.equals(basionym2)){
|
128
|
//both names belong to same basionym sub-group
|
129
|
compareResult = handleSameBasionym(basionym1, name1, name2);
|
130
|
}else{
|
131
|
compareResult = compareBasionyms(basionym1, basionym2);
|
132
|
}
|
133
|
|
134
|
if (compareResult != 0){
|
135
|
// System.out.println(": " + compareResult);
|
136
|
return compareResult;
|
137
|
}else{
|
138
|
//names are uncomparable on name level (except for uuid, id, etc.)
|
139
|
int result = super.compare(taxonBase1, taxonBase2);
|
140
|
// System.out.println(": = " + result);
|
141
|
return result;
|
142
|
}
|
143
|
}
|
144
|
|
145
|
// /**
|
146
|
// * @param homotypicalGroup
|
147
|
// * @return
|
148
|
// */
|
149
|
// private TaxonBase<?> getFirstInHomotypicalGroup(HomotypicalGroup homotypicalGroup, Collection<TaxonBase<?>> existing) {
|
150
|
// List<TaxonBase<?>> candidates = new ArrayList<TaxonBase<?>>();
|
151
|
// for (TaxonBase<?> candidate : existing){
|
152
|
// if (homotypicalGroup.getTypifiedNames().contains(candidate.getName())){
|
153
|
// candidates.add(candidate);
|
154
|
// }
|
155
|
// }
|
156
|
// Collections.sort(candidates, this);
|
157
|
// return candidates.isEmpty() ? null : candidates.get(0);
|
158
|
// }
|
159
|
|
160
|
|
161
|
/**
|
162
|
* Compare 2 names which have the same basionym.
|
163
|
* The names must not be equal to each other but may be equal
|
164
|
* to the basionym.
|
165
|
* @param basionym the basionym
|
166
|
* @param name1 first name to compare
|
167
|
* @param name2 second name to compare
|
168
|
* @return compare value according to the {@link Comparator#compare(Object, Object)} contract.
|
169
|
*/
|
170
|
private int handleSameBasionym(TaxonNameBase<?, ?> basionym,
|
171
|
TaxonNameBase<?, ?> name1,
|
172
|
TaxonNameBase<?, ?> name2) {
|
173
|
|
174
|
if (basionym.equals(name1)){
|
175
|
return -1;
|
176
|
}else if (basionym.equals(name2)){
|
177
|
return 1;
|
178
|
}else{
|
179
|
super.compare(name1, name2, false);
|
180
|
}
|
181
|
return 0;
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* @param basionym1
|
186
|
* @param basionym2
|
187
|
* @return
|
188
|
*/
|
189
|
private int compareBasionyms(TaxonNameBase<?,?> basionym1Orig, TaxonNameBase<?,?> basionym2Orig) {
|
190
|
//one taxon is first in group
|
191
|
TaxonNameBase<?,?> basionym1 = getFirstNameInGroup(basionym1Orig);
|
192
|
TaxonNameBase<?,?> basionym2 = getFirstNameInGroup(basionym2Orig);
|
193
|
|
194
|
//handle accepted taxon case
|
195
|
if (basionym1.equals(firstNameInGroup)){
|
196
|
return -1;
|
197
|
}else if (basionym2.equals(firstNameInGroup)){
|
198
|
return 1;
|
199
|
}
|
200
|
|
201
|
//handle replaced synonyms
|
202
|
boolean basio2IsReplacedSynForBasio1 = getReplacedSynonymClosure(basionym1).contains(basionym2);
|
203
|
boolean basio1IsReplacedSynForBasio2 = getReplacedSynonymClosure(basionym2).contains(basionym1);
|
204
|
|
205
|
if (basio2IsReplacedSynForBasio1 && !basio1IsReplacedSynForBasio2){
|
206
|
return 1;
|
207
|
}else if (basio1IsReplacedSynForBasio2 && !basio2IsReplacedSynForBasio1){
|
208
|
return -1;
|
209
|
}
|
210
|
|
211
|
//compare by date, nom. illeg., rank and alphabetically
|
212
|
return super.compare(basionym1, basionym2, true);
|
213
|
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* @param basionym
|
218
|
* @return
|
219
|
*/
|
220
|
private TaxonNameBase<?, ?> getFirstNameInGroup(TaxonNameBase<?, ?> basionym) {
|
221
|
for (NameRelationship nameRel : basionym.getRelationsFromThisName()){
|
222
|
if (nameRel.getType() != null && nameRel.getType().equals(NameRelationshipType.BASIONYM())){
|
223
|
if (nameRel.getToName().equals(firstNameInGroup)){
|
224
|
return firstNameInGroup;
|
225
|
}
|
226
|
}
|
227
|
}
|
228
|
return basionym;
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* @param basionym1
|
233
|
* @return
|
234
|
*/
|
235
|
@SuppressWarnings("rawtypes")
|
236
|
private Set<TaxonNameBase> getReplacedSynonymClosure(TaxonNameBase<?, ?> name) {
|
237
|
Set<TaxonNameBase> set = name.getReplacedSynonyms();
|
238
|
if (set.isEmpty()){
|
239
|
return set;
|
240
|
}
|
241
|
Set<TaxonNameBase> result = new HashSet<TaxonNameBase>();
|
242
|
for (TaxonNameBase<?,?> replSyn : set){
|
243
|
boolean notYetContained = result.add(replSyn);
|
244
|
if (notYetContained){
|
245
|
result.addAll(replSyn.getReplacedSynonyms());
|
246
|
}
|
247
|
}
|
248
|
return result;
|
249
|
}
|
250
|
|
251
|
/**
|
252
|
* @param name
|
253
|
* @return
|
254
|
*/
|
255
|
private TaxonNameBase<?,?> getPreferredInBasionymGroup(TaxonNameBase<?,?> name) {
|
256
|
Set<TaxonNameBase<?,?>> candidates = new HashSet<TaxonNameBase<?,?>>();
|
257
|
//get all final basionyms, except for those being part of a basionym circle
|
258
|
for (TaxonNameBase<?,?> candidate : name.getBasionyms()){
|
259
|
if (candidate != null
|
260
|
&& candidate.getHomotypicalGroup().equals(name.getHomotypicalGroup())
|
261
|
&& !hasBasionymCircle(candidate, null)){
|
262
|
candidate = getPreferredInBasionymGroup(candidate);
|
263
|
candidates.add(candidate);
|
264
|
}
|
265
|
}
|
266
|
|
267
|
if (candidates.isEmpty()){
|
268
|
return name;
|
269
|
}else if (candidates.size() == 1){
|
270
|
return candidates.iterator().next();
|
271
|
}else{
|
272
|
TaxonNameBase<?,?> result = candidates.iterator().next();
|
273
|
candidates.remove(result);
|
274
|
for (TaxonNameBase<?,?> candidate : candidates){
|
275
|
if (super.compare(result, candidate, false) > 0){
|
276
|
result = candidate;
|
277
|
}
|
278
|
}
|
279
|
return result;
|
280
|
}
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* @param candidate
|
285
|
* @return
|
286
|
*/
|
287
|
private boolean hasBasionymCircle(TaxonNameBase<?, ?> name, Set<TaxonNameBase<?,?>> existing) {
|
288
|
if (existing == null){
|
289
|
existing = new HashSet<TaxonNameBase<?,?>>();
|
290
|
}
|
291
|
if (existing.contains(name)){
|
292
|
return true;
|
293
|
}else{
|
294
|
Set<TaxonNameBase> basionyms = name.getBasionyms();
|
295
|
if (basionyms.isEmpty()){
|
296
|
return false;
|
297
|
}
|
298
|
existing.add(name);
|
299
|
for (TaxonNameBase basionym : basionyms){
|
300
|
if (hasBasionymCircle(basionym, existing)){
|
301
|
return true;
|
302
|
}
|
303
|
}
|
304
|
return false;
|
305
|
}
|
306
|
}
|
307
|
|
308
|
}
|