Project

General

Profile

Download (1.4 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import java.util.Comparator;
13

    
14
import eu.etaxonomy.cdm.model.common.ITreeNode;
15

    
16
/**
17
 * @author a.mueller
18
 * @date 05.07.2016
19
 *
20
 * Comparator for treeindexes.
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<String>{
25

    
26
    @Override
27
    public int compare(String treeIndex1, String 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.split(ITreeNode.separator);
40
        String[] splits2 = treeIndex2.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

    
57
}
(93-93/98)