3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.util
.Comparator
;
13 import java
.util
.HashMap
;
15 import java
.util
.StringTokenizer
;
16 import java
.util
.regex
.Pattern
;
18 import org
.springframework
.stereotype
.Component
;
20 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
24 * Comparator that compares two TaxonNode instances by the titleCache of their referenced names.
25 * @author a.kohlbecker
28 * TODO DISCUSS: move into model ?
31 public class TaxonNodeByNameComparator
extends AbstractStringComparator
implements Comparator
<TaxonNode
>, ITaxonNodeComparator
<TaxonNode
> {
34 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
36 @SuppressWarnings("unchecked")
37 public int compare(TaxonNode o1
, TaxonNode o2
) {
39 String titleCache1
= null;
40 String titleCache2
= null;
42 if(o1
.getTaxon() != null && o1
.getTaxon().getName() != null ){
44 if (o1
.getTaxon().getName() instanceof NonViralName
){
45 NonViralName nonViralName
= (NonViralName
)o1
.getTaxon().getName();
46 if (nonViralName
.isInfraSpecific()){
47 if (nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
48 titleCache1
= nonViralName
.getNameCache() + " "+nonViralName
.getAuthorshipCache();
53 if (titleCache1
== null){
54 titleCache1
= o1
.getTaxon().getName().getTitleCache();
57 if(o2
.getTaxon() != null && o2
.getTaxon().getName() != null){
58 if (o2
.getTaxon().getName() instanceof NonViralName
){
59 NonViralName nonViralName
= (NonViralName
)o2
.getTaxon().getName();
60 if (nonViralName
.isInfraSpecific()){
61 if (nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
62 titleCache2
= nonViralName
.getNameCache() + " "+nonViralName
.getAuthorshipCache();
67 if (titleCache2
== null){
68 titleCache2
= o2
.getTaxon().getName().getTitleCache();
72 titleCache1
= applySubstitutionRules(titleCache1
);
73 titleCache2
= applySubstitutionRules(titleCache2
);
75 StringTokenizer s2
= new StringTokenizer(titleCache1
, "\"");
76 if (s2
.countTokens()>0){
79 while(s2
.hasMoreTokens()){
80 titleCache1
+= s2
.nextToken();
82 s2
= new StringTokenizer(titleCache2
, "\"");
83 if (s2
.countTokens()>0){
87 while(s2
.hasMoreTokens()){
88 titleCache2
+= s2
.nextToken();
91 return titleCache1
.compareTo(titleCache2
);