Project

General

Profile

Download (3.13 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.ArrayList;
12
import java.util.Comparator;
13
import java.util.List;
14

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

    
22
    public TaxonNaturalComparator(){
23
		super();
24

    
25
	}
26

    
27
	@SuppressWarnings("null")
28
    @Override
29
	public int compare(TaxonNode node1, TaxonNode node2) {
30
	   // System.out.println("compare node 1: "+ node1.getTaxon().getTitleCache() + " - node 2: " + node2.getTaxon().getTitleCache());
31
	    if (node1.equals(node2)) {
32
	        return 0;
33
        }
34
	    // if we do not check for null for the treeIndex we always return 1 if one of the nodes have no treeIndex
35
	    if (node1.treeIndex() == null){
36
	        return 1;
37
	    }
38
	    if (node2.treeIndex() == null){
39
            return -1;
40
        }
41

    
42
	    if (node1.isAncestor(node2)) {
43
            return -1;
44
        }
45
		if (node2.isAncestor(node1)) {
46
            return 1;
47
        }
48

    
49

    
50
		String[] splitNode1 = node1.treeIndex().split("#");
51
		String[] splitNode2 = node2.treeIndex().split("#");
52

    
53

    
54
		if (node1.getParent().equals(node2.getParent())){
55
			return node1.getSortIndex().compareTo(node2.getSortIndex());
56
		}
57
		String lastEqualAncestorTreeIndex = "";
58
		List<TaxonNode> ancestorAndNode= new ArrayList<>();
59
		ancestorAndNode.add(node1);
60
		ancestorAndNode.addAll(node1.getAncestors());
61
		java.util.Collections.sort(ancestorAndNode, new TreeIndexComparator());
62

    
63

    
64
		List<TaxonNode> ancestorAndNode2= new ArrayList<>();
65
        ancestorAndNode2.add(node2);
66
        ancestorAndNode2.addAll(node2.getAncestors());
67
        java.util.Collections.sort(ancestorAndNode2, new TreeIndexComparator());
68

    
69
		for (int i = 0; i < splitNode1.length; i++){
70
			if (!splitNode1[i].equals(splitNode2[i])){
71
				// take the last equal ancestor and compare the sortindex
72
				if (lastEqualAncestorTreeIndex != null){
73
					TaxonNode lastEqualTreeIndexAncestorNode1 = null;
74
					TaxonNode lastEqualTreeIndexAncestorNode2 = null;
75
					for (TaxonNode next1 :ancestorAndNode){
76

    
77
						if (next1.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode1[i]+ "#") ){
78
						    lastEqualTreeIndexAncestorNode1 = next1;
79
						}
80
					}
81
					for (TaxonNode next2 :ancestorAndNode2){
82

    
83
						if (next2.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode2[i]+ "#")){
84
						    lastEqualTreeIndexAncestorNode2 = next2;
85
						}
86
					}
87
					return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex());
88
				}
89
			}
90
			if (!splitNode1[i].equals("")){
91
			    lastEqualAncestorTreeIndex = lastEqualAncestorTreeIndex+"#"+splitNode1[i];
92
			}
93
		}
94
		return 0;
95
	}
96

    
97
	private final class TreeIndexComparator implements Comparator<TaxonNode> {
98
	    @Override
99
	    public int compare(TaxonNode node1,TaxonNode node2){
100
	        return node1.treeIndex().compareTo(node2.treeIndex());
101
	    }
102
	}
103
}
(12-12/21)