Project

General

Profile

Download (2.79 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.util.Comparator;
12
import java.util.Iterator;
13

    
14
/**
15
 * Comparator to compare {@link TaxonNode taxon nodes} by its user defined ordering
16
 * @author k.luther
17
 */
18
public class TaxonNaturalComparator implements Comparator<TaxonNode> {
19

    
20
    public TaxonNaturalComparator(){
21
		super();
22

    
23
	}
24

    
25
	@Override
26
	public int compare(TaxonNode node1, TaxonNode node2) {
27
//		TaxonNode node1 = null;
28
//		TaxonNode node2 = null;
29
//		if (taxon1.getTaxonNodes().size()>0 && taxon2.getTaxonNodes().size()>0){
30
//			for (TaxonNode node: taxon1.getTaxonNodes()){
31
//				if (node.getClassification().equals(classification)){
32
//					node1= node;
33
//					break;
34
//				}
35
//			}
36
//			for (TaxonNode node: taxon2.getTaxonNodes()){
37
//				if (node.getClassification().equals(classification)){
38
//					node2= node;
39
//					break;
40
//				}
41
//			}
42
//		}else{
43
//			return 0;
44
//		}
45
		if (node1.isAncestor(node2)) {
46
            return 1;
47
        }
48
		if (node2.isAncestor(node1)) {
49
            return -1;
50
        }
51
		if (node1.equals(node2)) {
52
            return 0;
53
        }
54

    
55
		String[] splitNode1 = node1.treeIndex().split("#");
56
		String[] splitNode2 = node2.treeIndex().split("#");
57
		if (splitNode1.length < splitNode2.length) {
58
            return 1;
59
        } else if (splitNode2.length < splitNode1.length) {
60
            return -1;
61
        }
62

    
63
		if (node1.getParent().equals(node2.getParent())){
64
			return node1.getSortIndex().compareTo(node2.getSortIndex());
65
		}
66
		String lastEqualAncestorTreeIndex = null;
67

    
68
		Iterator<TaxonNode> ancestorsNode1 = node1.getAncestors().iterator();
69
		Iterator<TaxonNode> ancestorsNode2 = node2.getAncestors().iterator();
70
		for (int i = 0; i < splitNode1.length; i++){
71
			if (!splitNode1[i].equals(splitNode2[i])){
72
				// take the last equal ancestor and compare the sortindex
73
				if (lastEqualAncestorTreeIndex != null){
74
					TaxonNode lastEqualTreeIndexAncestorNode1 = null;
75
					TaxonNode lastEqualTreeIndexAncestorNode2 = null;
76
					while (ancestorsNode1.hasNext()){
77
						TaxonNode next1 = ancestorsNode1.next();
78
						if (next1.treeIndex().equals(lastEqualAncestorTreeIndex)){
79
							lastEqualTreeIndexAncestorNode1 = next1;
80
						}
81
					}
82
					while (ancestorsNode2.hasNext()){
83
						TaxonNode next2 = ancestorsNode2.next();
84
						if (next2.treeIndex().equals(lastEqualAncestorTreeIndex)){
85
							lastEqualTreeIndexAncestorNode2 = next2;
86
						}
87
					}
88
					return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex());
89

    
90
				}
91
			}
92
			lastEqualAncestorTreeIndex = splitNode1[i];
93
		}
94

    
95
		return 0;
96
	}
97

    
98
}
(13-13/21)