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.description.DescriptionBase;
|
15
|
import eu.etaxonomy.cdm.model.description.DescriptionType;
|
16
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
17
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
18
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
19
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByNameComparator;
|
20
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
|
21
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoNaturalComparator;
|
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<TaxonNodeDto> comparator;
|
32
|
|
33
|
public MatrixRowComparator() {
|
34
|
if (PreferencesUtil.isNodesSortedNaturally()){
|
35
|
comparator = new TaxonNodeDtoNaturalComparator();
|
36
|
} else if (PreferencesUtil.isNodesSortedByName()){
|
37
|
comparator = new TaxonNodeDtoByNameComparator();
|
38
|
}else {
|
39
|
comparator = new TaxonNodeDtoByRankAndNameComparator();
|
40
|
}
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
public int compare(Object o1, Object o2) {
|
45
|
if(o1 == null){
|
46
|
if(o2 != null){
|
47
|
return -1;
|
48
|
}
|
49
|
return 0;
|
50
|
}
|
51
|
else if(o2 == null){
|
52
|
return 1;
|
53
|
}
|
54
|
if(o1 instanceof TaxonNodeDto){
|
55
|
if(o2 instanceof RowWrapperDTO){
|
56
|
return 1;
|
57
|
}
|
58
|
}
|
59
|
else{
|
60
|
if(o2 instanceof TaxonNodeDto){
|
61
|
return -1;
|
62
|
}
|
63
|
}
|
64
|
if(o1 instanceof TaxonNodeDto && o2 instanceof TaxonNodeDto){
|
65
|
return comparator.compare((TaxonNodeDto)o1, (TaxonNodeDto)o2);
|
66
|
}
|
67
|
if(o1 instanceof RowWrapperDTO && o2 instanceof RowWrapperDTO){
|
68
|
RowWrapperDTO rowWrapper1 = (RowWrapperDTO)o1;
|
69
|
RowWrapperDTO rowWrapper2 = (RowWrapperDTO)o2;
|
70
|
//check for equality
|
71
|
if(rowWrapper1.equals(rowWrapper2)){
|
72
|
return 0;
|
73
|
}
|
74
|
TaxonNodeDto taxonTaxonNode = rowWrapper1.getTaxonNode();
|
75
|
TaxonNodeDto specimenTaxonNode = rowWrapper2.getTaxonNode();
|
76
|
int nodeCompare = comparator.compare(taxonTaxonNode, specimenTaxonNode);
|
77
|
if(nodeCompare!=0){
|
78
|
// different taxon nodes
|
79
|
return nodeCompare;
|
80
|
}
|
81
|
else{
|
82
|
// same taxon node
|
83
|
}
|
84
|
DescriptionBase description1 = rowWrapper1.getDescription();
|
85
|
DescriptionBase description2 = rowWrapper2.getDescription();
|
86
|
//compare by taxon description type
|
87
|
if(description1 instanceof TaxonDescription){
|
88
|
if(description2 instanceof SpecimenDescription){
|
89
|
return -1;
|
90
|
}
|
91
|
}
|
92
|
else{
|
93
|
if(description2 instanceof TaxonDescription){
|
94
|
return 1;
|
95
|
}
|
96
|
}
|
97
|
//both descriptions are of the same type
|
98
|
if(description1 instanceof SpecimenDescription){
|
99
|
//description2 has to also be a SpecimenDescription
|
100
|
return compareSpecimenDescriptions((SpecimenDescription)description1, (SpecimenDescription)description2);
|
101
|
}
|
102
|
else if(description1 instanceof TaxonDescription){
|
103
|
//description2 has to also be a TaxonDescription
|
104
|
return compareTaxonDescriptions((TaxonDescription)description1, (TaxonDescription)description2);
|
105
|
}
|
106
|
}
|
107
|
return o1.hashCode()-o2.hashCode();
|
108
|
}
|
109
|
|
110
|
private int compareTaxonDescriptions(TaxonDescription taxonDescription1, TaxonDescription taxonDescription2) {
|
111
|
boolean isComputed1 = taxonDescription1.getTypes().stream()
|
112
|
.anyMatch(type -> type.equals(DescriptionType.AGGREGATED_STRUC_DESC));
|
113
|
boolean isComputed2 = taxonDescription2.getTypes().stream()
|
114
|
.anyMatch(type -> type.equals(DescriptionType.AGGREGATED_STRUC_DESC));
|
115
|
if(isComputed1){
|
116
|
if(!isComputed2){
|
117
|
return -1;
|
118
|
}
|
119
|
}
|
120
|
if(isComputed2){
|
121
|
return 1;
|
122
|
}
|
123
|
// no aggregation description found
|
124
|
boolean isLiterature1 = taxonDescription1.getTypes().stream()
|
125
|
.anyMatch(type -> type.equals(DescriptionType.SECONDARY_DATA));
|
126
|
boolean isLiterature2 = taxonDescription2.getTypes().stream()
|
127
|
.anyMatch(type -> type.equals(DescriptionType.SECONDARY_DATA));
|
128
|
if(isLiterature1){
|
129
|
if(!isLiterature2){
|
130
|
return -1;
|
131
|
}
|
132
|
}
|
133
|
if(isLiterature2){
|
134
|
return 1;
|
135
|
}
|
136
|
return taxonDescription1.hashCode()-taxonDescription2.hashCode();
|
137
|
}
|
138
|
|
139
|
private int compareSpecimenDescriptions(SpecimenDescription specimenDescription1,
|
140
|
SpecimenDescription specimenDescription2) {
|
141
|
int id1 = specimenDescription1.getDescribedSpecimenOrObservation().getId();
|
142
|
int id2 = specimenDescription2.getDescribedSpecimenOrObservation().getId();
|
143
|
return id1-id2;
|
144
|
}
|
145
|
|
146
|
}
|