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.io.Serializable;
|
13
|
import java.util.Comparator;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.joda.time.DateTime;
|
18
|
|
19
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
|
20
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
|
21
|
import eu.etaxonomy.cdm.model.name.Rank;
|
22
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
23
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
24
|
|
25
|
/**
|
26
|
* This class makes available a method to compare two {@link TaxonBase taxa} by
|
27
|
* comparing the publication dates of the corresponding
|
28
|
* {@link eu.etaxonomy.cdm.model.name.TaxonName taxon names}.
|
29
|
*
|
30
|
* @author a.mueller
|
31
|
* @since 11.06.2008
|
32
|
*/
|
33
|
public class TaxonComparator implements Comparator<TaxonBase>, Serializable {
|
34
|
|
35
|
private static final long serialVersionUID = -1433623743189043446L;
|
36
|
@SuppressWarnings("unused")
|
37
|
private static final Logger logger = Logger.getLogger(TaxonComparator.class);
|
38
|
|
39
|
private boolean includeRanks = false;
|
40
|
|
41
|
/**
|
42
|
* @param includeRanks
|
43
|
*/
|
44
|
public TaxonComparator() {
|
45
|
super();
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* @param includeRanks
|
50
|
*/
|
51
|
public TaxonComparator(boolean includeRanks) {
|
52
|
super();
|
53
|
this.includeRanks = includeRanks;
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Returns an integer generated by comparing first the nomenclatural status and then the
|
58
|
* {@link eu.etaxonomy.cdm.model.name.INomenclaturalReference#getYear() publication years}
|
59
|
* of both {@link eu.etaxonomy.cdm.model.name.TaxonName taxon names}
|
60
|
* used in the given {@link TaxonBase taxa}.
|
61
|
* If 1 name has status of type nom. inval. or nom. nudum the name is put to the end of a
|
62
|
* list (returns +1 for a status in taxon1 and -1 for a status in taxon2). If both do have
|
63
|
* no status or the same status, the publication date is taken for comparison.
|
64
|
* Nom. nudum is handled as more "severe" status then nom.inval.
|
65
|
*
|
66
|
* Returns a negative value if the publication year corresponding to the
|
67
|
* first given taxon precedes the publication year corresponding to the
|
68
|
* second given taxon. Returns a positive value if the contrary is true and
|
69
|
* 0 if both publication years and the date, when they are created, are identical.
|
70
|
* In case one of the publication
|
71
|
* years is "null" and the other is not, the "empty" publication year will
|
72
|
* be considered to be always preceded by the "not null" publication year.
|
73
|
* If both publication years are "null" the creation date is used for the comparison
|
74
|
*
|
75
|
* @see java.lang.String#compareTo(String)
|
76
|
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
77
|
*/
|
78
|
@Override
|
79
|
public int compare(
|
80
|
@SuppressWarnings("rawtypes") TaxonBase taxonBase1,
|
81
|
@SuppressWarnings("rawtypes") TaxonBase taxonBase2) {
|
82
|
int result;
|
83
|
|
84
|
if (taxonBase1.equals(taxonBase2)){
|
85
|
return 0;
|
86
|
}
|
87
|
|
88
|
TaxonName name1 = taxonBase1.getName();
|
89
|
TaxonName name2 = taxonBase2.getName();
|
90
|
|
91
|
//set to end if a taxon has nomenclatural status "nom. inval." or "nom. nud."
|
92
|
int statusCompareWeight = compareStatus(name1, name2);
|
93
|
|
94
|
if (statusCompareWeight != 0){
|
95
|
return Integer.signum(statusCompareWeight);
|
96
|
}
|
97
|
|
98
|
//TODO discuss if we should also include nom. illeg. here on taxon level comparison
|
99
|
result = compare(name1, name2, false);
|
100
|
|
101
|
if (result == 0){
|
102
|
DateTime date11 = taxonBase1.getCreated();
|
103
|
DateTime date12 = taxonBase2.getCreated();
|
104
|
if (date11 == null && date12 == null) {
|
105
|
result = 0;
|
106
|
}else if (date11 == null) {
|
107
|
return 1;
|
108
|
}else if (date12 == null) {
|
109
|
return -1;
|
110
|
}else{
|
111
|
result = date11.compareTo(date12);
|
112
|
}
|
113
|
}
|
114
|
if (result == 0){
|
115
|
//the Comparator contract
|
116
|
return taxonBase1.getUuid().compareTo(taxonBase1.getUuid());
|
117
|
}else{
|
118
|
return result;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
protected int compareStatus(TaxonName taxonName, TaxonName taxonName2) {
|
123
|
int statusCompareWeight = 0;
|
124
|
statusCompareWeight += computeStatusCompareWeight(taxonName);
|
125
|
statusCompareWeight -= computeStatusCompareWeight(taxonName2);
|
126
|
return statusCompareWeight;
|
127
|
}
|
128
|
|
129
|
private int computeStatusCompareWeight(TaxonName taxonName) {
|
130
|
int result = 0;
|
131
|
|
132
|
if (taxonName == null || taxonName.isValid()){
|
133
|
return 0;
|
134
|
}
|
135
|
Set<NomenclaturalStatus> status1 = taxonName.getStatus();
|
136
|
for (NomenclaturalStatus nomStatus1 : status1){
|
137
|
NomenclaturalStatusType type = nomStatus1.getType();
|
138
|
if (type != null && type.isInvalid()){
|
139
|
//NOTE: not clear were this order comes from, it is partly mentioned
|
140
|
//in #5794, it is unclear if it fits to the longer list mentioned in #9272
|
141
|
if(type.equals(NomenclaturalStatusType.PROVISIONAL())){
|
142
|
result += 1;
|
143
|
}else if (type.equals(NomenclaturalStatusType.INVALID())){
|
144
|
result += 2;
|
145
|
}else if(type.equals(NomenclaturalStatusType.COMBINATION_INVALID())){
|
146
|
result += 2;
|
147
|
}else if (type.equals(NomenclaturalStatusType.OPUS_UTIQUE_OPPR())){
|
148
|
result += 2;
|
149
|
}else if(type.equals(NomenclaturalStatusType.NUDUM())){
|
150
|
result += 3;
|
151
|
}
|
152
|
result += 1;
|
153
|
}
|
154
|
}
|
155
|
return result;
|
156
|
}
|
157
|
|
158
|
protected int compareNomIlleg(TaxonName taxonName1, TaxonName taxonName2) {
|
159
|
int isNomIlleg1 = isNomIlleg(taxonName1);
|
160
|
int isNomIlleg2 = isNomIlleg(taxonName2);
|
161
|
return isNomIlleg1 - isNomIlleg2;
|
162
|
}
|
163
|
|
164
|
private int isNomIlleg(TaxonName taxonName) {
|
165
|
if (taxonName == null || taxonName.getStatus() == null){
|
166
|
return 0;
|
167
|
}
|
168
|
Set<NomenclaturalStatus> status = taxonName.getStatus();
|
169
|
for (NomenclaturalStatus nomStatus : status){
|
170
|
if (nomStatus.getType() != null){
|
171
|
if (nomStatus.getType().equals(NomenclaturalStatusType.ILLEGITIMATE())){
|
172
|
return 1;
|
173
|
}
|
174
|
}
|
175
|
}
|
176
|
return 0;
|
177
|
}
|
178
|
|
179
|
private Integer getIntegerDate(TaxonName name){
|
180
|
Integer result;
|
181
|
|
182
|
if (name == null){
|
183
|
result = null;
|
184
|
}else{
|
185
|
if (name.isZoological()){
|
186
|
result = name.getPublicationYear();
|
187
|
}else{
|
188
|
Reference ref = name.getNomenclaturalReference();
|
189
|
if (ref == null){
|
190
|
result = null;
|
191
|
}else{
|
192
|
if (ref.getDatePublished() == null){
|
193
|
Reference inRef = ref.getInReference();
|
194
|
if (inRef == null){
|
195
|
result = null;
|
196
|
}else{
|
197
|
if (inRef.getDatePublished() == null){
|
198
|
result = null;
|
199
|
}else{
|
200
|
result = ref.getInReference().getDatePublished().getStartYear();
|
201
|
}
|
202
|
}
|
203
|
}else{
|
204
|
result = ref.getDatePublished().getStartYear();
|
205
|
}
|
206
|
}
|
207
|
}
|
208
|
}
|
209
|
|
210
|
return result;
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
*
|
215
|
* @param name1
|
216
|
* @param name2
|
217
|
* @param includeNomIlleg
|
218
|
* if <code>true</code> and if both names have no date or same date, the only
|
219
|
* name having nom. illeg. state is handled as if the name was published later than the name
|
220
|
* without status nom. illeg.
|
221
|
* @return the compare value
|
222
|
*/
|
223
|
protected int compare(TaxonName name1, TaxonName name2, boolean includeNomIlleg) {
|
224
|
int result;
|
225
|
|
226
|
//dates
|
227
|
Integer intDate1 = getIntegerDate(name1);
|
228
|
Integer intDate2 = getIntegerDate(name2);
|
229
|
|
230
|
if (intDate1 == null && intDate2 == null){
|
231
|
result = 0;
|
232
|
}else if (intDate1 == null){
|
233
|
return 1;
|
234
|
}else if (intDate2 == null){
|
235
|
return -1;
|
236
|
}else{
|
237
|
result = intDate1.compareTo(intDate2);
|
238
|
}
|
239
|
|
240
|
//nom. illeg.
|
241
|
if (result == 0 && includeNomIlleg){
|
242
|
result = compareNomIlleg(name1, name2);
|
243
|
if (result != 0){
|
244
|
return result;
|
245
|
}
|
246
|
}
|
247
|
|
248
|
if (result == 0 && includeRanks){
|
249
|
Rank rank1 = name1 == null? null : name1.getRank();
|
250
|
Rank rank2 = name2 == null? null : name2.getRank();
|
251
|
|
252
|
if (rank1 == null && rank2 == null){
|
253
|
result = 0;
|
254
|
}else if (rank1 == null){
|
255
|
return 1;
|
256
|
}else if (rank2 == null){
|
257
|
return -1;
|
258
|
}else{
|
259
|
//for some strange reason compareTo for ranks returns 1 if rank2 is lower. So we add minus (-)
|
260
|
result = - rank1.compareTo(rank2);
|
261
|
}
|
262
|
}
|
263
|
|
264
|
if (result == 0 && name1 != null && name2 != null){
|
265
|
result = name1.compareToName(name2);
|
266
|
if (result != 0){
|
267
|
return result;
|
268
|
}
|
269
|
}
|
270
|
return result;
|
271
|
}
|
272
|
}
|