From 56613858845facc6db249efeb1e666c037282afe Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andreas=20M=C3=BCller?= Date: Thu, 21 Jan 2021 20:15:11 +0100 Subject: [PATCH] ref #9356 clean up SourceComparator --- .../section/description/SourceComparator.java | 109 +++++++++--------- 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/SourceComparator.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/SourceComparator.java index 3e37e3acd..8f4e885cb 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/SourceComparator.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/SourceComparator.java @@ -1,6 +1,13 @@ +/** +* Copyright (C) 2007 EDIT +* European Distributed Institute of Taxonomy +* http://www.e-taxonomy.eu +* +* The contents of this file are subject to the Mozilla Public License Version 1.1 +* See LICENSE.TXT at the top of this package for the full license terms. +*/ package eu.etaxonomy.taxeditor.ui.section.description; - import java.util.Comparator; import eu.etaxonomy.cdm.model.description.DescriptionElementSource; @@ -11,22 +18,29 @@ import eu.etaxonomy.cdm.model.reference.Reference; /** * @author pplitzner * @date Apr 13, 2016 - * */ public class SourceComparator implements Comparator { + @Override public int compare(T o1, T o2) { - int id1 = o1.getId(); - int id2 = o2.getId(); + + //same and null compare, to be on the save side + if (o1 == o2){ + return 0; + }else if (o1 == null){ + return -1; + }else if (o2 == null){ + return 1; + } + boolean isDescriptionElementSource1 = false; boolean isDescriptionElementSource2 = false; - if (o1 instanceof DescriptionElementSource){ + if (o1.isInstanceOf(DescriptionElementSource.class)){ isDescriptionElementSource1 = true; } - if (o2 instanceof DescriptionElementSource){ + if (o2.isInstanceOf(DescriptionElementSource.class)){ isDescriptionElementSource2 = true; } - int result = 0; if (isDescriptionElementSource1 != isDescriptionElementSource2){ if (isDescriptionElementSource1){ return -1; @@ -34,72 +48,63 @@ public class SourceComparator implements Compara return 1; } } - OriginalSourceType type1 = o1.getType(); - OriginalSourceType type2 = o2.getType(); - Reference citation1 = o1.getCitation(); - Reference citation2 = o2.getCitation(); - - // the newly created should always be on top - if (id1 == 0){ - if(id2!=0) { - return -1; - } - else{ - result = 0; - } - } else if(id2==0){ + if (!o1.isPersited() && o2.isPersited()) { + return -1; + } else if(o1.isPersited() && !o2.isPersited()){ return 1; } // sort by type (Primary taxonomic > Primary Media > others // alphabetically by reference title cache) - else if (type1 == null){ - if(type2==null){ - result = 0; - } - else{ - result = -1; + OriginalSourceType type1 = o1.getType(); + OriginalSourceType type2 = o2.getType(); + if (type1 == null){ + if (type2!=null){ + return -1; } } else if (type2 == null){ - result = 1; + return 1; } else if(type1.equals(type2)){ - result = 0; + //continue with citation compare } else if (type1.equals(OriginalSourceType.PrimaryTaxonomicSource)){ - result = 1; + return 1; } else if (type2.equals(OriginalSourceType.PrimaryTaxonomicSource)){ - result = -1; + return -1; } else if (type1.equals(OriginalSourceType.PrimaryMediaSource)){ - result = 1; + return 1; } else if (type2.equals(OriginalSourceType.PrimaryMediaSource)){ - result = -1; + return -1; } + int result; + //sort by citation title cache if types are equal - if (result == 0){ - if(citation1!=null && citation2!=null){ - result = citation1.getTitleCache().compareTo(citation2.getTitleCache()); + Reference citation1 = o1.getCitation(); + Reference citation2 = o2.getCitation(); + if(citation1!=null && citation2!=null){ + result = citation1.getTitleCache().compareTo(citation2.getTitleCache()); + if (result != 0){ + return result; } + } - if(o2.getCreated()!=null && o1.getCreated()!=null){ - result = o1.getCreated().compareTo(o2.getCreated()); - }else if (o1.getCreated() == null ){ - if (o2.getCreated() == null){ - result = 0; - }else{ - return -1; - } - }else if (o2.getCreated() == null){ - return 1; + //sort by created + if(o2.getCreated()!=null && o1.getCreated()!=null){ + result = o1.getCreated().compareTo(o2.getCreated()); + if (result != 0){ + return result; } - if (result == 0){ - //default fallback - return o1.getUuid().compareTo(o2.getUuid()); + }else if (o1.getCreated() == null){ + if (o2.getCreated() != null){ + return -1; } + }else if (o2.getCreated() == null){ + return 1; } - return result; - + //default fallback + return o1.getUuid().compareTo(o2.getUuid()); } -} +} \ No newline at end of file -- 2.34.1