Project

General

Profile

Download (1.89 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 TaxonComparatorSearch implements Serializable, Comparator<TaxonBase> {
23
	private static final long serialVersionUID = 2596641007876609704L;
24

    
25
	public int compare(TaxonBase taxon1, TaxonBase taxon2) {
26
		
27
		if (taxon1.getUuid().equals(taxon2.getUuid())){
28
			return 0;
29
		}
30
		TaxonNameBase<?,?> name1 = taxon1.getName();
31
		TaxonNameBase<?,?> name2 = taxon2.getName();
32
		
33
		Rank rankTax1 = (name1 == null) ? null : name1.getRank();
34
		Rank rankTax2 = (name2 == null) ? null : name2.getRank();
35
		
36
		//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
37
		if (rankTax1 == null && rankTax2 != null){
38
			return 1;
39
		}else if(rankTax2 == null && rankTax1 != null){
40
			return -1;
41
		}else if (rankTax1 != null && rankTax1.isHigher(rankTax2)){
42
			return -1;
43
		}else if (rankTax1 == null && rankTax2 == null || rankTax1.equals(rankTax2)) {
44
			if (name1 != null && name2 != null){
45
				//same rank, order by name
46
				int result = name1.compareTo(name2);
47
				if (result == 0){
48
					return taxon1.getUuid().compareTo(taxon2.getUuid());
49
				}else{
50
					return result;
51
				}
52
			}else {
53
				//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
54
				return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
55
			}
56
		}else{
57
			//rankTax2.isHigher(rankTax1)
58
			return 1;
59
		}
60
	}
61

    
62
}
(12-12/18)