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
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
17
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
18
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
19
|
|
20
|
/**
|
21
|
* This class makes available a method to compare two {@link TaxonBase taxa} by
|
22
|
* comparing the publication dates of the corresponding {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names}.
|
23
|
*
|
24
|
* @author a.mueller
|
25
|
* @created 11.06.2008
|
26
|
* @version 1.0
|
27
|
*/
|
28
|
public class TaxonComparator implements Comparator<TaxonBase> {
|
29
|
private static final Logger logger = Logger.getLogger(TaxonComparator.class);
|
30
|
|
31
|
/* (non-Javadoc)
|
32
|
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
33
|
*/
|
34
|
/**
|
35
|
* Returns an integer generated by comparing the {@link eu.etaxonomy.cdm.model.name.INomenclaturalReference#getYear() publication years}
|
36
|
* of both {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names} used in the given {@link TaxonBase taxa}.
|
37
|
* Returns a negative value if the publication year corresponding to the
|
38
|
* first given taxon precedes the publication year corresponding to the
|
39
|
* second given taxon. Returns a positive value if the contrary is true and
|
40
|
* 0 if both publication years are identical. In case one of the publication
|
41
|
* years is "null" and the other is not, the "empty" publication year will
|
42
|
* be considered to be always preceded by the "not null" publication year.
|
43
|
*
|
44
|
* @see java.lang.String#compareTo(String)
|
45
|
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
46
|
*/
|
47
|
public int compare(TaxonBase taxonBase1, TaxonBase taxonBase2) {
|
48
|
int result;
|
49
|
String date1 = getDate(taxonBase1);;
|
50
|
String date2 = getDate(taxonBase2);
|
51
|
if (date1 == null && date2 == null){
|
52
|
return 0;
|
53
|
}else if (date1 == null){
|
54
|
return 1;
|
55
|
}else if (date2 == null){
|
56
|
return -1;
|
57
|
}
|
58
|
result = date1.compareTo(date2);
|
59
|
return result;
|
60
|
}
|
61
|
|
62
|
|
63
|
|
64
|
@SuppressWarnings("unchecked")
|
65
|
private String getDate(TaxonBase taxonBase){
|
66
|
String result = null;
|
67
|
if (taxonBase == null){
|
68
|
result = null;
|
69
|
}else{
|
70
|
TaxonNameBase name = taxonBase.getName();
|
71
|
if (name == null){
|
72
|
result = null;
|
73
|
}else{
|
74
|
if (name instanceof ZoologicalName){
|
75
|
|
76
|
result = String.valueOf(((ZoologicalName)name).getPublicationYear());
|
77
|
}else{
|
78
|
ReferenceBase ref = name.getNomenclaturalReference();
|
79
|
if (ref == null){
|
80
|
result = null;
|
81
|
}else{
|
82
|
result = ref.getYear();
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
if (result != null){
|
88
|
result = result.trim();
|
89
|
}
|
90
|
if ("".equals(result)){
|
91
|
result = null;
|
92
|
}
|
93
|
return result;
|
94
|
}
|
95
|
|
96
|
}
|