Project

General

Profile

Download (1.91 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
package eu.etaxonomy.cdm.model.taxon;
10

    
11
import java.io.Serializable;
12
import java.util.Comparator;
13

    
14
import eu.etaxonomy.cdm.model.name.Rank;
15
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
16

    
17
/**
18
 * @author k.luther
19
 * @date 18.03.2010
20
 *
21
 */
22
public class TaxonNodeByRankAndNameComparator implements Serializable, Comparator<TaxonNode> {
23
	private static final long serialVersionUID = 2596641007876609704L;
24
	
25
	public int compare(TaxonNode node1, TaxonNode node2) {
26
		
27
		
28
		if (node1.getUuid().equals(node2.getUuid())){
29
			return 0;
30
		}
31
		TaxonBase taxon1 = node1.getTaxon();
32
		TaxonBase taxon2 = node2.getTaxon();
33
		
34
		TaxonNameBase name1 = taxon1.getName();
35
		TaxonNameBase name2 = taxon2.getName();
36
		
37
		Rank rankTax1 = (name1 == null) ? null : name1.getRank();
38
		Rank rankTax2 = (name2 == null) ? null : name2.getRank();
39
		
40
		//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
41
		if (rankTax1 == null && rankTax2 != null){
42
			return 1;
43
		}else if(rankTax2 == null && rankTax1 != null){
44
			return -1;
45
		}else if (rankTax1 != null && rankTax1.isHigher(rankTax2)){
46
			return -1;
47
		}else if (rankTax1 == null && rankTax2 == null || rankTax1.equals(rankTax2)) {
48
			if (name1 != null && name2 != null){
49
				//same rank, order by name
50
				int result = name1.compareTo(name2);
51
				if (result == 0){
52
					return taxon1.getUuid().compareTo(taxon2.getUuid());
53
				}else{
54
					return result;
55
				}
56
			}else {
57
				//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
58
				return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
59
			}
60
		}else{
61
			//rankTax2.isHigher(rankTax1)
62
			return 1;
63
		}
64
	}
65

    
66
	
67

    
68
}
(17-17/21)