2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.taxon
;
12 import java
.io
.Serializable
;
13 import java
.util
.Comparator
;
14 import java
.util
.Iterator
;
17 import org
.apache
.log4j
.Logger
;
19 import org
.joda
.time
.DateTime
;
21 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
22 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
23 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
24 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
26 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
29 * This class makes available a method to compare two {@link TaxonBase taxa} by
30 * comparing the publication dates of the corresponding {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names}.
36 public class TaxonComparator
implements Comparator
<TaxonBase
>, Serializable
{
37 private static final Logger logger
= Logger
.getLogger(TaxonComparator
.class);
40 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
43 * Returns an integer generated by comparing the {@link eu.etaxonomy.cdm.model.name.INomenclaturalReference#getYear() publication years}
44 * of both {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names} used in the given {@link TaxonBase taxa}.
45 * Returns a negative value if the publication year corresponding to the
46 * first given taxon precedes the publication year corresponding to the
47 * second given taxon. Returns a positive value if the contrary is true and
48 * 0 if both publication years and the date, when they are created, are identical. In case one of the publication
49 * years is "null" and the other is not, the "empty" publication year will
50 * be considered to be always preceded by the "not null" publication year.
51 * If both publication years are "null" the creation date is used for the comparison
54 * @see java.lang.String#compareTo(String)
55 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
57 public int compare(TaxonBase taxonBase1
, TaxonBase taxonBase2
) {
59 //boolean invalOrNudForTaxon1 = false;
60 //boolean invalOrNudForTaxon2 = false;
62 boolean invalTaxon1
= false;
63 boolean invalTaxon2
= false;
64 boolean nudumTaxon1
= false;
65 boolean nudumTaxon2
= false;
67 //if a taxon has nomenclatural status "nom. inval." or "nom. nud."
69 Set status
= taxonBase1
.getName().getStatus();
70 Iterator iterator
= status
.iterator();
71 if (iterator
.hasNext()){
72 NomenclaturalStatus nomStatus1
= (NomenclaturalStatus
) iterator
.next();
73 Set status2
= taxonBase2
.getName().getStatus();
74 iterator
= status2
.iterator(); // is that right? or better iterator = status2.iterator(); ???
75 if (iterator
.hasNext()){
76 NomenclaturalStatus nomStatus2
= (NomenclaturalStatus
)iterator
.next();
78 if (nomStatus1.getType().equals(NomenclaturalStatusType.NUDUM()) ||
79 nomStatus1.getType().equals(NomenclaturalStatusType.INVALID())){
80 invalOrNudForTaxon1 = true;
82 if (nomStatus2.getType().equals(NomenclaturalStatusType.NUDUM()) || nomStatus2.getType().equals(NomenclaturalStatusType.INVALID())){
83 invalOrNudForTaxon2 = true;
85 if (invalOrNudForTaxon1 && !invalOrNudForTaxon2){
87 }else if (!invalOrNudForTaxon1 && invalOrNudForTaxon2){
90 else{ // both taxon are invalid or nudum
95 if (nomStatus1
.getType().equals(NomenclaturalStatusType
.INVALID())){
98 if (nomStatus1
.getType().equals(NomenclaturalStatusType
.NUDUM())){
101 if (nomStatus2
.getType().equals(NomenclaturalStatusType
.INVALID())){
104 if (nomStatus2
.getType().equals(NomenclaturalStatusType
.NUDUM())){
107 if (nudumTaxon1
&& !nudumTaxon2
){
109 }else if (nudumTaxon1
&& nudumTaxon2
){
111 }else if (invalTaxon1
&& !nudumTaxon2
){
117 }else if (nudumTaxon2
&& !nudumTaxon1
){
119 }else if(invalTaxon2
){
125 }else{//if taxonbase2.getName().getStatus = NULL and taxonbase2 not
128 }else{//if taxonbase1.getName().getStatus = NULL
129 iterator
= taxonBase2
.getName().getStatus().iterator();
130 if (!iterator
.hasNext()){
131 //if (taxonBase2.getName().getStatus() == null){ // both are null, continue checking
137 Integer intDate1
= getIntegerDate(taxonBase1
);
138 Integer intDate2
= getIntegerDate(taxonBase2
);
140 if (intDate1
== null && intDate2
== null){
142 }else if (intDate1
== null){
144 }else if (intDate2
== null){
147 result
= intDate1
.compareTo(intDate2
);
151 TaxonNameBase taxName1
= taxonBase1
.getName();
152 TaxonNameBase taxName2
= taxonBase2
.getName();
154 return taxName1
.compareTo(taxName2
);
159 DateTime date11
= taxonBase1
.getCreated();
160 DateTime date12
= taxonBase2
.getCreated();
161 if (date11
== null && date12
== null) {
164 if (date11
== null) {
167 if (date12
== null) {
170 result
= date11
.compareTo(date12
);
173 //for ticket #393 if the publication year is the same, the order is alphabetically
176 TaxonNameBase taxName1
= taxonBase1
.getName();
177 TaxonNameBase taxName2
= taxonBase2
.getName();
179 return taxName1
.compareTo(taxName2
);
188 private Integer
getIntegerDate(TaxonBase taxonBase
){
191 if (taxonBase
== null){
194 TaxonNameBase name
= taxonBase
.getName();
198 if (name
instanceof ZoologicalName
){
200 result
= (((ZoologicalName
)name
).getPublicationYear());
202 ReferenceBase ref
= (ReferenceBase
) name
.getNomenclaturalReference();
206 if (ref
.getDatePublished() == null){
207 if (ref
.getInReference() == null){
210 result
= ref
.getInReference().getDatePublished().getStartYear();
213 result
= ref
.getDatePublished().getStartYear();
223 @SuppressWarnings("unchecked")
225 private String
getDate(TaxonBase taxonBase
){
226 String result
= null;
227 if (taxonBase
== null){
230 TaxonNameBase name
= taxonBase
.getName();
234 if (name
instanceof ZoologicalName
){
236 result
= String
.valueOf(((ZoologicalName
)name
).getPublicationYear());
238 INomenclaturalReference ref
= name
.getNomenclaturalReference();
242 result
= ref
.getYear();
248 result
= result
.trim();
250 if ("".equals(result
)){