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.common.MarkerType;
|
15
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
16
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
17
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
18
|
import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
|
19
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
20
|
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
|
21
|
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator;
|
22
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
23
|
|
24
|
/**
|
25
|
* @author pplitzner
|
26
|
* @since Jan 25, 2018
|
27
|
*
|
28
|
*/
|
29
|
public class MatrixRowComparator implements Comparator<Object>{
|
30
|
|
31
|
private Comparator<TaxonNode> comparator;
|
32
|
|
33
|
public MatrixRowComparator() {
|
34
|
if (PreferencesUtil.getSortNodesNaturally()){
|
35
|
comparator = new TaxonNaturalComparator();
|
36
|
} else if (PreferencesUtil.getSortNodesStrictlyAlphabetically()){
|
37
|
comparator = new TaxonNodeByNameComparator();
|
38
|
}else {
|
39
|
comparator = new TaxonNodeByRankAndNameComparator();
|
40
|
}
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
public int compare(Object o1, Object o2) {
|
45
|
if(o1 instanceof TaxonNode && o2 instanceof TaxonNode){
|
46
|
return comparator.compare((TaxonNode)o1, (TaxonNode)o2);
|
47
|
}
|
48
|
if(o1 instanceof RowWrapperDTO && o2 instanceof RowWrapperDTO){
|
49
|
RowWrapperDTO rowWrapper1 = (RowWrapperDTO)o1;
|
50
|
RowWrapperDTO rowWrapper2 = (RowWrapperDTO)o2;
|
51
|
//check for equality
|
52
|
if(rowWrapper1.equals(rowWrapper2)){
|
53
|
return 0;
|
54
|
}
|
55
|
TaxonNode node1 = rowWrapper1.getTaxonNode().getParent();
|
56
|
TaxonNode node2 = rowWrapper2.getTaxonNode().getParent();
|
57
|
if(node1!=null && node2!=null){
|
58
|
//compare by taxon node
|
59
|
int compare = comparator.compare(node1, node2);
|
60
|
if(compare!=0){
|
61
|
return compare;
|
62
|
}
|
63
|
DescriptionBase description1 = rowWrapper1.getDescription();
|
64
|
DescriptionBase description2 = rowWrapper2.getDescription();
|
65
|
//compare by taxon description type
|
66
|
if(description1 instanceof TaxonDescription){
|
67
|
if(description2 instanceof SpecimenDescription){
|
68
|
return -1;
|
69
|
}
|
70
|
}
|
71
|
else{
|
72
|
if(description2 instanceof TaxonDescription){
|
73
|
return 1;
|
74
|
}
|
75
|
}
|
76
|
//both descriptions are of the same type
|
77
|
if(description1 instanceof SpecimenDescription){
|
78
|
//description2 has to also be a SpecimenDescription
|
79
|
SpecimenDescription specimenDescription1 = (SpecimenDescription)description1;
|
80
|
SpecimenDescription specimenDescription2 = (SpecimenDescription)description2;
|
81
|
int id1 = specimenDescription1.getDescribedSpecimenOrObservation().getId();
|
82
|
int id2 = specimenDescription2.getDescribedSpecimenOrObservation().getId();
|
83
|
return id1-id2;
|
84
|
}
|
85
|
else if(description1 instanceof TaxonDescription){
|
86
|
//description2 has to also be a TaxonDescription
|
87
|
TaxonDescription taxonDescription1 = (TaxonDescription)description1;
|
88
|
TaxonDescription taxonDescription2 = (TaxonDescription)description2;
|
89
|
boolean isComputed1 = taxonDescription1.getMarkers().stream()
|
90
|
.anyMatch(marker -> marker.getMarkerType().equals(MarkerType.COMPUTED()));
|
91
|
boolean isComputed2 = taxonDescription2.getMarkers().stream()
|
92
|
.anyMatch(marker -> marker.getMarkerType().equals(MarkerType.COMPUTED()));
|
93
|
if(isComputed1){
|
94
|
if(!isComputed2){
|
95
|
return -1;
|
96
|
}
|
97
|
}
|
98
|
if(isComputed2){
|
99
|
return 1;
|
100
|
}
|
101
|
//TODO: implement compare for different description types
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
return o1.hashCode()-o2.hashCode();
|
106
|
}
|
107
|
|
108
|
}
|