Project

General

Profile

Download (3.5 KB) Statistics
| Branch: | Tag: | Revision:
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
import org.joda.time.DateTime;
16

    
17
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
18
import eu.etaxonomy.cdm.model.name.ZoologicalName;
19
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
20
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
21

    
22
/**
23
 * This class makes available a method to compare two {@link TaxonBase taxa} by
24
 * comparing the publication dates of the corresponding {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names}.
25
 * 
26
 * @author a.mueller
27
 * @created 11.06.2008
28
 * @version 1.0
29
 */
30
public class TaxonComparator implements Comparator<TaxonBase> {
31
	private static final Logger logger = Logger.getLogger(TaxonComparator.class);
32

    
33
	/* (non-Javadoc)
34
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
35
	 */
36
	/** 
37
	 * Returns an integer generated by comparing the {@link eu.etaxonomy.cdm.model.name.INomenclaturalReference#getYear() publication years}
38
	 * of both {@link eu.etaxonomy.cdm.model.name.TaxonNameBase taxon names} used in the given {@link TaxonBase taxa}.
39
	 * Returns a negative value if the publication year corresponding to the
40
	 * first given taxon precedes the publication year corresponding to the
41
	 * second given taxon. Returns a positive value if the contrary is true and
42
	 * 0 if both publication years and the date, when they are created, are identical. In case one of the publication
43
	 * years is "null" and the other is not, the "empty" publication year will
44
	 * be considered to be always preceded by the "not null" publication year.
45
	 * If both publication years are "null" the creation date is used for the comparison
46
	 * 
47
	 *  
48
	 * @see		java.lang.String#compareTo(String)
49
	 * @see		java.util.Comparator#compare(java.lang.Object, java.lang.Object)
50
	 */
51
	public int compare(TaxonBase taxonBase1, TaxonBase taxonBase2) {
52
		int result;
53
		String date1 = getDate(taxonBase1);;
54
		String date2 = getDate(taxonBase2);
55
		if (date1 == null && date2 == null){
56
			result = 0;
57
		}else if (date1 == null){
58
			return 1;
59
		}else if (date2 == null){
60
			return -1;
61
		}else{
62
				result = date1.compareTo(date2);
63
		}
64
		if (result == 0){
65
			DateTime date11 = taxonBase1.getCreated();
66
			DateTime date12 = taxonBase2.getCreated();
67
			if (date11 == null && date12 == null) {
68
				return 0;
69
			}
70
			if (date11 == null) {
71
				return 1;
72
			}
73
			if (date12 == null) {
74
				return -1;
75
			}
76
			result = date11.compareTo(date12);
77
		}
78
		
79
		return result;
80
	}
81
	
82
	
83
	
84
	@SuppressWarnings("unchecked")
85
	private String getDate(TaxonBase taxonBase){
86
		String result = null;
87
		if (taxonBase == null){
88
			result = null;
89
		}else{
90
			TaxonNameBase name = taxonBase.getName();
91
			if (name == null){
92
				result = null;
93
			}else{
94
				if (name instanceof ZoologicalName){
95
					
96
					result = String.valueOf(((ZoologicalName)name).getPublicationYear());
97
				}else{
98
					 INomenclaturalReference ref = name.getNomenclaturalReference();
99
					if (ref == null){
100
						result = null;
101
					}else{
102
						result = ref.getYear();
103
					}
104
				}
105
			}
106
		}
107
		if (result != null){
108
			result = result.trim();
109
		}
110
		if ("".equals(result)){
111
			result = null;
112
		}
113
		return result;
114
	}
115
	
116
}
(9-9/15)