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