Project

General

Profile

Download (3.39 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.persistence.dto;
10

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

    
14
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
15

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

    
23
    public TaxonNodeDtoNaturalComparator(){
24
		super();
25

    
26
	}
27

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

    
43
	    if (node1.getParentUUID().equals(node2.getParentUUID())){
44
            return node1.getSortIndex().compareTo(node2.getSortIndex());
45
        }
46

    
47

    
48
	    if (node2.getTreeIndex().startsWith(node1.getTreeIndex())) {
49
            return -1;
50
        }
51
		if (node1.getTreeIndex().startsWith(node2.getTreeIndex())) {
52
            return 1;
53
        }
54

    
55

    
56
//		String[] splitNode1 = node1.getTreeIndex().split("#");
57
//		String[] splitNode2 = node2.getTreeIndex().split("#");
58

    
59

    
60

    
61
//		String lastEqualAncestorTreeIndex = "";
62
//		List<TaxonNodeDto> ancestorAndNode= new ArrayList<>();
63
//		ancestorAndNode.add(node1);
64
//		ancestorAndNode.addAll(node1.getAncestors());
65
//		java.util.Collections.sort(ancestorAndNode, new TreeIndexComparator());
66
//
67
//
68
//		List<TaxonNode> ancestorAndNode2= new ArrayList<>();
69
//        ancestorAndNode2.add(node2);
70
//        ancestorAndNode2.addAll(node2.getAncestors());
71
//        java.util.Collections.sort(ancestorAndNode2, new TreeIndexComparator());
72
//
73
//		for (int i = 0; i < splitNode1.length; i++){
74
//			if (!splitNode1[i].equals(splitNode2[i])){
75
//				// take the last equal ancestor and compare the sortindex
76
//				if (lastEqualAncestorTreeIndex != null){
77
//					TaxonNode lastEqualTreeIndexAncestorNode1 = null;
78
//					TaxonNode lastEqualTreeIndexAncestorNode2 = null;
79
//					for (TaxonNode next1 :ancestorAndNode){
80
//
81
//						if (next1.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode1[i]+ "#") ){
82
//						    lastEqualTreeIndexAncestorNode1 = next1;
83
//						}
84
//					}
85
//					for (TaxonNode next2 :ancestorAndNode2){
86
//
87
//						if (next2.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode2[i]+ "#")){
88
//						    lastEqualTreeIndexAncestorNode2 = next2;
89
//						}
90
//					}
91
//					return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex());
92
//				}
93
//			}
94
//			if (!splitNode1[i].equals("")){
95
//			    lastEqualAncestorTreeIndex = lastEqualAncestorTreeIndex+"#"+splitNode1[i];
96
//			}
97
//		}
98
		return 0;
99
	}
100

    
101
	private final class TreeIndexComparator implements Comparator<TaxonNodeDto> {
102
	    @Override
103
	    public int compare(TaxonNodeDto node1,TaxonNodeDto node2){
104
	        return node1.getTreeIndex().compareTo(node2.getTreeIndex());
105
	    }
106
	}
107
}
(12-12/16)