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