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 if (taxonBase2
.getName().getStatus() == null){ // both are null, continue checking
135 String date1
= getDate(taxonBase1
);;
136 String date2
= getDate(taxonBase2
);
137 if (date1
== null && date2
== null){
139 }else if (date1
== null){
141 }else if (date2
== null){
144 result
= date1
.compareTo(date2
);
148 TaxonNameBase taxName1
= taxonBase1
.getName();
149 TaxonNameBase taxName2
= taxonBase2
.getName();
151 return taxName1
.compareTo(taxName2
);
156 DateTime date11
= taxonBase1
.getCreated();
157 DateTime date12
= taxonBase2
.getCreated();
158 if (date11
== null && date12
== null) {
161 if (date11
== null) {
164 if (date12
== null) {
167 result
= date11
.compareTo(date12
);
170 //for ticket #393 if the publication year is the same, the order is alphabetically
173 TaxonNameBase taxName1
= taxonBase1
.getName();
174 TaxonNameBase taxName2
= taxonBase2
.getName();
176 return taxName1
.compareTo(taxName2
);
186 @SuppressWarnings("unchecked")
187 private String
getDate(TaxonBase taxonBase
){
188 String result
= null;
189 if (taxonBase
== null){
192 TaxonNameBase name
= taxonBase
.getName();
196 if (name
instanceof ZoologicalName
){
198 result
= String
.valueOf(((ZoologicalName
)name
).getPublicationYear());
200 INomenclaturalReference ref
= name
.getNomenclaturalReference();
204 result
= ref
.getYear();
210 result
= result
.trim();
212 if ("".equals(result
)){