2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.descriptiveDataSet
.matrix
;
11 import java
.util
.Comparator
;
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
;
29 public class MatrixRowComparator
implements Comparator
<Object
>{
31 private Comparator
<TaxonNode
> comparator
;
33 public MatrixRowComparator() {
34 if (PreferencesUtil
.getSortNodesNaturally()){
35 comparator
= new TaxonNaturalComparator();
36 } else if (PreferencesUtil
.getSortNodesStrictlyAlphabetically()){
37 comparator
= new TaxonNodeByNameComparator();
39 comparator
= new TaxonNodeByRankAndNameComparator();
44 public int compare(Object o1
, Object o2
) {
45 if(o1
instanceof TaxonNode
){
46 if(o2
instanceof RowWrapperDTO
){
51 if(o2
instanceof TaxonNode
){
55 if(o1
instanceof TaxonNode
&& o2
instanceof TaxonNode
){
56 return comparator
.compare((TaxonNode
)o1
, (TaxonNode
)o2
);
58 if(o1
instanceof RowWrapperDTO
&& o2
instanceof RowWrapperDTO
){
59 RowWrapperDTO rowWrapper1
= (RowWrapperDTO
)o1
;
60 RowWrapperDTO rowWrapper2
= (RowWrapperDTO
)o2
;
62 if(rowWrapper1
.equals(rowWrapper2
)){
65 TaxonNode node1
= rowWrapper1
.getTaxonNode().getParent();
66 TaxonNode node2
= rowWrapper2
.getTaxonNode().getParent();
67 if(node1
!=null && node2
!=null){
68 //compare by taxon node
69 int compare
= comparator
.compare(node1
, node2
);
73 DescriptionBase description1
= rowWrapper1
.getDescription();
74 DescriptionBase description2
= rowWrapper2
.getDescription();
75 //compare by taxon description type
76 if(description1
instanceof TaxonDescription
){
77 if(description2
instanceof SpecimenDescription
){
82 if(description2
instanceof TaxonDescription
){
86 //both descriptions are of the same type
87 if(description1
instanceof SpecimenDescription
){
88 //description2 has to also be a SpecimenDescription
89 SpecimenDescription specimenDescription1
= (SpecimenDescription
)description1
;
90 SpecimenDescription specimenDescription2
= (SpecimenDescription
)description2
;
91 int id1
= specimenDescription1
.getDescribedSpecimenOrObservation().getId();
92 int id2
= specimenDescription2
.getDescribedSpecimenOrObservation().getId();
95 else if(description1
instanceof TaxonDescription
){
96 //description2 has to also be a TaxonDescription
97 TaxonDescription taxonDescription1
= (TaxonDescription
)description1
;
98 TaxonDescription taxonDescription2
= (TaxonDescription
)description2
;
99 boolean isComputed1
= taxonDescription1
.getMarkers().stream()
100 .anyMatch(marker
-> marker
.getMarkerType().equals(MarkerType
.COMPUTED()));
101 boolean isComputed2
= taxonDescription2
.getMarkers().stream()
102 .anyMatch(marker
-> marker
.getMarkerType().equals(MarkerType
.COMPUTED()));
111 //TODO: implement compare for different description types
115 return o1
.hashCode()-o2
.hashCode();