Project

General

Profile

Download (2.32 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.UUID;
13

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

    
17
/**
18
 * @author k.luther
19
 * @since 18.03.2010
20
 *
21
 */
22
public class TaxonNodeByRankAndNameComparator extends TaxonNodeByNameComparator implements Serializable {
23
	private static final long serialVersionUID = 2596641007876609704L;
24

    
25
	@Override
26
    public int compare(TaxonNode node1, TaxonNode node2) {
27
	    if (node1.getUuid().equals(node2.getUuid())){
28
            return 0;
29
        }
30
	    compareNodes(node1, node2);
31

    
32
		TaxonBase<?> taxon1 = node1.getTaxon();
33
		TaxonBase<?> taxon2 = node2.getTaxon();
34

    
35
		TaxonName name1 = (taxon1 == null) ? null : taxon1.getName();
36
		TaxonName name2 = (taxon2 == null) ? null : taxon2.getName();
37

    
38
		Rank rankTax1 = (name1 == null) ? null : name1.getRank();
39
		Rank rankTax2 = (name2 == null) ? null : name2.getRank();
40

    
41
		//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null
42
		if (rankTax1 == null && rankTax2 != null){
43
			return 1;
44
		}else if(rankTax2 == null && rankTax1 != null){
45
			return -1;
46
		}else if (rankTax1 != null && rankTax1.isHigher(rankTax2)){
47
			return -1;
48
		}else if (rankTax1 == null && rankTax2 == null || rankTax1.equals(rankTax2)) {
49
			if (name1 != null && name2 != null){
50
				//same rank, order by name
51
				return compareNames(node1, node2);
52

    
53
			}else {
54
				//this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
55
				return getTaxonTitle(taxon1, node1).compareTo(getTaxonTitle(taxon2, node2));
56
			}
57
		}else{
58
			//rankTax2.isHigher(rankTax1)
59
			return 1;
60
		}
61
	}
62

    
63
    /**
64
     * @param taxon1
65
     * @return
66
     */
67
    public String getTaxonTitle(TaxonBase<?> taxon, TaxonNode node) {
68
        return (taxon == null) ? node.getUuid().toString(): taxon.getTitleCache();
69
    }
70

    
71
    /**
72
     * @param taxon
73
     * @param node
74
     * @return
75
     */
76
    private UUID getTaxonUuid(TaxonBase<?> taxon, TaxonNode node) {
77
        return (taxon == null) ? node.getUuid(): taxon.getUuid();
78
    }
79

    
80

    
81

    
82
}
(16-16/21)