try to fix NPE in TaxonNodeByRankAndNameComparator
authorAndreas Müller <a.mueller@bgbm.org>
Fri, 27 Oct 2017 21:16:25 +0000 (23:16 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Fri, 27 Oct 2017 21:18:31 +0000 (23:18 +0200)
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/TaxonNodeByRankAndNameComparator.java

index e4eb4c5bcb0978340a4c018d062ddfc9f69ead92..ba2bf9f0623406237d16854bfd815dee78cbc4a5 100644 (file)
@@ -10,6 +10,7 @@ package eu.etaxonomy.cdm.model.taxon;
 
 import java.io.Serializable;
 import java.util.Comparator;
+import java.util.UUID;
 
 import eu.etaxonomy.cdm.model.name.Rank;
 import eu.etaxonomy.cdm.model.name.TaxonName;
@@ -32,8 +33,8 @@ public class TaxonNodeByRankAndNameComparator implements Serializable, Comparato
                TaxonBase<?> taxon1 = node1.getTaxon();
                TaxonBase<?> taxon2 = node2.getTaxon();
 
-               TaxonName name1 = taxon1.getName();
-               TaxonName name2 = taxon2.getName();
+               TaxonName name1 = (taxon1 == null) ? null : taxon1.getName();
+               TaxonName name2 = (taxon2 == null) ? null : taxon2.getName();
 
                Rank rankTax1 = (name1 == null) ? null : name1.getRank();
                Rank rankTax2 = (name2 == null) ? null : name2.getRank();
@@ -50,13 +51,13 @@ public class TaxonNodeByRankAndNameComparator implements Serializable, Comparato
                                //same rank, order by name
                                int result = name1.compareToName(name2);
                                if (result == 0){
-                                       return taxon1.getUuid().compareTo(taxon2.getUuid());
+                                       return getTaxonUuid(taxon1, node1).compareTo(getTaxonUuid(taxon2, node2));
                                }else{
                                        return result;
                                }
                        }else {
                                //this is maybe not 100% correct, we need to compare name cases, but it is a very rare case
-                               return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
+                               return getTaxonTitle(taxon1, node1).compareTo(getTaxonTitle(taxon2, node2));
                        }
                }else{
                        //rankTax2.isHigher(rankTax1)
@@ -64,6 +65,23 @@ public class TaxonNodeByRankAndNameComparator implements Serializable, Comparato
                }
        }
 
+    /**
+     * @param taxon1
+     * @return
+     */
+    public String getTaxonTitle(TaxonBase<?> taxon, TaxonNode node) {
+        return (taxon == null) ? node.getUuid().toString(): taxon.getTitleCache();
+    }
+
+    /**
+     * @param taxon
+     * @param node
+     * @return
+     */
+    private UUID getTaxonUuid(TaxonBase<?> taxon, TaxonNode node) {
+        return (taxon == null) ? node.getUuid(): taxon.getUuid();
+    }
+
 
 
 }