Project

General

Profile

Download (1.92 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.taxeditor.editor.descriptiveDataSet.matrix;
10

    
11
import java.util.Comparator;
12

    
13
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
14
import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
15
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
16
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
17
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator;
18
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
19

    
20
/**
21
 * @author pplitzner
22
 * @since Jan 25, 2018
23
 *
24
 */
25
public class MatrixRowComparator implements Comparator<Object>{
26

    
27
    private Comparator<TaxonNode> comparator;
28

    
29
    public MatrixRowComparator() {
30
        if (PreferencesUtil.getSortNodesNaturally()){
31
            comparator = new TaxonNaturalComparator();
32
        } else if (PreferencesUtil.getSortNodesStrictlyAlphabetically()){
33
            comparator = new TaxonNodeByNameComparator();
34
        }else {
35
            comparator = new TaxonNodeByRankAndNameComparator();
36
        }
37
    }
38

    
39
    @Override
40
    public int compare(Object o1, Object o2) {
41
        if(o1 instanceof TaxonNode && o2 instanceof TaxonNode){
42
            return comparator.compare((TaxonNode)o1, (TaxonNode)o2);
43
        }
44
        if(o1 instanceof RowWrapperDTO && o2 instanceof RowWrapperDTO){
45
            RowWrapperDTO rowWrapper1 = (RowWrapperDTO)o1;
46
            RowWrapperDTO rowWrapper2 = (RowWrapperDTO)o2;
47
            TaxonNode node1 = rowWrapper1.getTaxonNode().getParent();
48
            TaxonNode node2 = rowWrapper2.getTaxonNode().getParent();
49
            if(node1!=null && node2!=null){
50
                return comparator.compare(node1, node2);
51
            }
52
        }
53
        return o1.hashCode()-o2.hashCode();
54
    }
55

    
56
}
(15-15/20)