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
.ITaxonNodeService
;
14 import eu
.etaxonomy
.cdm
.api
.service
.dto
.RowWrapperDTO
;
15 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
16 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
17 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
18 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
19 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
20 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDtoByNameComparator
;
21 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDtoByRankAndNameComparator
;
22 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDtoNaturalComparator
;
23 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
24 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
31 public class MatrixRowComparator
implements Comparator
<Object
>{
33 private Comparator
<TaxonNodeDto
> comparator
;
34 private ITaxonNodeService taxonNodeService
;
36 public MatrixRowComparator() {
37 taxonNodeService
= CdmStore
.getService(ITaxonNodeService
.class);
38 if (PreferencesUtil
.isNodesSortedNaturally()){
39 comparator
= new TaxonNodeDtoNaturalComparator();
40 } else if (PreferencesUtil
.isNodesSortedByName()){
41 comparator
= new TaxonNodeDtoByNameComparator();
43 comparator
= new TaxonNodeDtoByRankAndNameComparator();
48 public int compare(Object o1
, Object o2
) {
49 if(o1
instanceof TaxonNodeDto
){
50 if(o2
instanceof RowWrapperDTO
){
55 if(o2
instanceof TaxonNodeDto
){
59 if(o1
instanceof TaxonNodeDto
&& o2
instanceof TaxonNodeDto
){
60 return comparator
.compare((TaxonNodeDto
)o1
, (TaxonNodeDto
)o2
);
62 if(o1
instanceof RowWrapperDTO
&& o2
instanceof RowWrapperDTO
){
63 RowWrapperDTO rowWrapper1
= (RowWrapperDTO
)o1
;
64 RowWrapperDTO rowWrapper2
= (RowWrapperDTO
)o2
;
66 if(rowWrapper1
.equals(rowWrapper2
)){
69 TaxonNodeDto node1
= rowWrapper1
.getTaxonNode();
70 TaxonNodeDto node2
= rowWrapper2
.getTaxonNode();
71 if(node1
!=null && node2
!=null){
72 //compare by taxon node
73 int compare
= comparator
.compare(node1
, node2
);
77 DescriptionBase description1
= rowWrapper1
.getDescription();
78 DescriptionBase description2
= rowWrapper2
.getDescription();
79 //compare by taxon description type
80 if(description1
instanceof TaxonDescription
){
81 if(description2
instanceof SpecimenDescription
){
86 if(description2
instanceof TaxonDescription
){
90 //both descriptions are of the same type
91 if(description1
instanceof SpecimenDescription
){
92 //description2 has to also be a SpecimenDescription
93 SpecimenDescription specimenDescription1
= (SpecimenDescription
)description1
;
94 SpecimenDescription specimenDescription2
= (SpecimenDescription
)description2
;
95 int id1
= specimenDescription1
.getDescribedSpecimenOrObservation().getId();
96 int id2
= specimenDescription2
.getDescribedSpecimenOrObservation().getId();
99 else if(description1
instanceof TaxonDescription
){
100 //description2 has to also be a TaxonDescription
101 TaxonDescription taxonDescription1
= (TaxonDescription
)description1
;
102 TaxonDescription taxonDescription2
= (TaxonDescription
)description2
;
103 boolean isComputed1
= taxonDescription1
.getMarkers().stream()
104 .anyMatch(marker
-> marker
.getMarkerType().equals(MarkerType
.COMPUTED()));
105 boolean isComputed2
= taxonDescription2
.getMarkers().stream()
106 .anyMatch(marker
-> marker
.getMarkerType().equals(MarkerType
.COMPUTED()));
115 //TODO: implement compare for different description types
119 return o1
.hashCode()-o2
.hashCode();