1
|
/**
|
2
|
* Copyright (C) 2016 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.common;
|
10
|
|
11
|
import java.util.Comparator;
|
12
|
|
13
|
/**
|
14
|
* @author a.mueller
|
15
|
* @since 05.07.2016
|
16
|
*
|
17
|
* Comparator for tree indexes.
|
18
|
* Compares the tree indexes node by node, sorted by node number.
|
19
|
* If one index is shorter than the other one but
|
20
|
*/
|
21
|
public class TreeIndexComparator implements Comparator<TreeIndex>{
|
22
|
|
23
|
@Override
|
24
|
public int compare(TreeIndex treeIndex1, TreeIndex treeIndex2) {
|
25
|
if (treeIndex1 == null && treeIndex2 == null){
|
26
|
return 0;
|
27
|
}else if (treeIndex1 == null){
|
28
|
return -1;
|
29
|
}else if (treeIndex2 == null){
|
30
|
return 1;
|
31
|
}
|
32
|
if (treeIndex1.equals(treeIndex2)){
|
33
|
return 0;
|
34
|
}
|
35
|
|
36
|
String[] splits1 = treeIndex1.toString().split(ITreeNode.separator);
|
37
|
String[] splits2 = treeIndex2.toString().split(ITreeNode.separator);
|
38
|
|
39
|
|
40
|
for (int i=0; i < splits1.length; i++){
|
41
|
if (splits2.length <= i){
|
42
|
return 1;
|
43
|
}
|
44
|
int c = splits1[i].compareTo(splits2[i]);
|
45
|
if (c != 0){
|
46
|
return c;
|
47
|
}
|
48
|
}
|
49
|
return -1;
|
50
|
|
51
|
}
|
52
|
|
53
|
}
|