Project

General

Profile

Download (10.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9

    
10
package eu.etaxonomy.cdm.compare.taxon;
11

    
12
import java.util.Comparator;
13
import java.util.HashSet;
14
import java.util.Set;
15

    
16
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.model.name.NameRelationship;
19
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
20
import eu.etaxonomy.cdm.model.name.TaxonName;
21
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
22

    
23
/**
24
 * This class orders synonyms of a homotypic group,
25
 * first by
26
 * <ul>
27
 *  <li>Basionym groups (the basionym and all names derived from this basionym)
28
 *      should be kept together in a subgroup</li>
29
 *  <li>The order of the subgroups is defined by the ordering of their
30
 *       basionyms (according to the following ordering)</li>
31
 *  <li>If a name is illegitimate or not does play a role for ordering</li>
32
 *  <li>Names with publication year should always come first</li>
33
 *  <li>Names with no publication year are sorted by rank</li>
34
 *  <li>Names with no publication year and equal rank are sorted alphabetically</li>
35
 *  <li>If 2 names have a replaced synonym relationship the replaced synonym comes first,
36
 *      the replacement name comes later as this reflects the order of publication</li>
37
 *  </ul>
38
 *
39
 * Details on ordering are explained at https://dev.e-taxonomy.eu/redmine/issues/3338<BR>
40
 *
41
 * @author a.mueller
42
 * @since 02.03.2016
43
 */
44
public class HomotypicGroupTaxonComparator extends TaxonComparator {
45

    
46
    private static final long serialVersionUID = -5088210641256430878L;
47
	private static final Logger logger = LogManager.getLogger(HomotypicGroupTaxonComparator.class);
48

    
49
    private final TaxonBase<?> firstTaxonInGroup;
50
    private final TaxonName firstNameInGroup;
51

    
52
    public HomotypicGroupTaxonComparator(@SuppressWarnings("rawtypes") TaxonBase firstTaxonInGroup) {
53
        super(true);
54
        this.firstTaxonInGroup = firstTaxonInGroup;
55
        this.firstNameInGroup = firstTaxonInGroup == null ? null: firstTaxonInGroup.getName();
56
    }
57

    
58
    public HomotypicGroupTaxonComparator(@SuppressWarnings("rawtypes") TaxonBase firstTaxonInGroup, boolean includeRanks) {
59
        super(includeRanks);
60
        this.firstTaxonInGroup = firstTaxonInGroup;
61
        this.firstNameInGroup = firstTaxonInGroup == null ? null: firstTaxonInGroup.getName();
62
    }
63

    
64
    public HomotypicGroupTaxonComparator(TaxonName firstNameInGroup, boolean includeRanks) {
65
        super(includeRanks);
66
        firstTaxonInGroup = null;
67
        this.firstNameInGroup = firstNameInGroup;
68
    }
69

    
70
    /**
71
     *
72
     * @see TaxonComparator#compare(TaxonBase, TaxonBase)
73
     * @see java.lang.String#compareTo(String)
74
     * @see	java.util.Comparator#compare(java.lang.Object, java.lang.Object)
75
     */
76
    @Override
77
    public int compare(
78
            @SuppressWarnings("rawtypes") TaxonBase taxonBase1,
79
            @SuppressWarnings("rawtypes") TaxonBase taxonBase2) {
80

    
81
        TaxonName name1 = taxonBase1.getName();
82
        TaxonName name2 = taxonBase2.getName();
83

    
84
        return compareNames(name1, name2, taxonBase1, taxonBase2);
85
    }
86

    
87
    public int compareNames(TaxonName name1,  TaxonName name2, TaxonBase<?> taxonBase1, TaxonBase<?> taxonBase2) {
88
        if (logger.isDebugEnabled()){logger.debug(name1.getTitleCache() +" : "+ name2.getTitleCache());}
89
        if (name1 == null && taxonBase1 == null ||
90
                name2 == null && taxonBase2 == null){
91
            throw new IllegalArgumentException("There should always be either a name or a taxon to be compared");
92
        }
93

    
94
        int compareStatus = compareStatus(name1, name2);
95
        if (compareStatus != 0){
96
            return compareStatus;
97
        }
98

    
99
        //not same homotypical group -
100
        //NOTE: this comparator should usually not be used
101
        //      for comparing names of different homotypical groups.
102
        //      The following is only to have a defined compare behavior
103
        //      which follows the contract of Comparator#compare.
104
        if (name1 == null ||
105
            name2 == null ||
106
            ! name1.getHomotypicalGroup().equals(name2.getHomotypicalGroup())){
107

    
108
            String compareString1 = name1 != null ?
109
                    name1.getHomotypicalGroup().getUuid().toString() :
110
                    taxonBase1.getUuid().toString();
111
            String compareString2 = name2 != null ?
112
                    name2.getHomotypicalGroup().getUuid().toString() :
113
                    taxonBase2.getUuid().toString();
114
            int result = compareString1.compareTo(compareString2);
115
            return result;
116
        }
117

    
118
        //same homotypical group ...
119
        //one taxon is first in group
120
        if (isFirstInGroup(taxonBase1, name1)){
121
            return -1;
122
        }else if (taxonBase2 != null && taxonBase2.equals(firstTaxonInGroup)){
123
            return 1;
124
        }
125

    
126
        //same name => compare on taxon level
127
        if (name1.equals(name2)){
128
            return super.compare(taxonBase1, taxonBase2);  //if name is the same compare on taxon level
129
        }
130

    
131
        TaxonName basionym1 = getPreferredInBasionymGroup(name1);
132
        TaxonName basionym2 = getPreferredInBasionymGroup(name2);
133

    
134
        int compareResult;
135
        if (basionym1.equals(basionym2)){
136
            //both names belong to same basionym sub-group
137
            compareResult = handleSameBasionym(basionym1, name1, name2);
138
        }else{
139
            compareResult = compareBasionyms(basionym1, basionym2);
140
        }
141

    
142
        if (compareResult != 0){
143
//            if (logger.isDebugEnabled()){logger.debug(": " + compareResult);}
144
            return compareResult;
145
        }else{
146
            //names are uncomparable on name level (except for uuid, id, etc.)
147
            int result = super.compare(taxonBase1, taxonBase2);
148
            if (logger.isDebugEnabled()){logger.debug(": = " + result);}
149
            return result;
150
        }
151
    }
152

    
153
    private boolean isFirstInGroup(TaxonBase<?> taxonBase, TaxonName name) {
154
        if (taxonBase != null){
155
            return taxonBase.equals(firstTaxonInGroup);
156
        }else{
157
            return name.equals(firstNameInGroup);
158
        }
159
    }
160

    
161
    /**
162
     * Compare 2 names which have the same basionym.
163
     * The names must not be equal to each other but may be equal
164
     * to the basionym.
165
     *
166
     * @param basionym the basionym
167
     * @param name1 first name to compare
168
     * @param name2 second name to compare
169
     * @return compare value according to the {@link Comparator#compare(Object, Object)} contract.
170
     */
171
    private int handleSameBasionym(TaxonName basionym,
172
            TaxonName name1,
173
            TaxonName name2) {
174

    
175
        if (basionym.equals(name1)){
176
            return -1;
177
        }else if (basionym.equals(name2)){
178
            return 1;
179
        }else{
180
            return super.compare(name1, name2, false);
181
        }
182

    
183
    }
184

    
185
    private int compareBasionyms(TaxonName basionym1Orig, TaxonName basionym2Orig) {
186
        //one taxon is first in group
187
        TaxonName basionym1 = getFirstNameInGroup(basionym1Orig);
188
        TaxonName basionym2 = getFirstNameInGroup(basionym2Orig);
189

    
190
        //handle accepted taxon case
191
        if (basionym1.equals(firstNameInGroup)){
192
            return -1;
193
        }else if (basionym2.equals(firstNameInGroup)){
194
            return 1;
195
        }
196

    
197
        //handle replaced synonyms
198
        boolean basio2IsReplacedSynForBasio1 = getReplacedSynonymClosure(basionym1).contains(basionym2);
199
        boolean basio1IsReplacedSynForBasio2 = getReplacedSynonymClosure(basionym2).contains(basionym1);
200

    
201
        if (basio2IsReplacedSynForBasio1 && !basio1IsReplacedSynForBasio2){
202
            return 1;
203
        }else if (basio1IsReplacedSynForBasio2 && !basio2IsReplacedSynForBasio1){
204
            return -1;
205
        }
206

    
207
        //compare by date, nom. illeg., rank and alphabetically
208
        return super.compare(basionym1, basionym2, true);
209
    }
210

    
211
    private TaxonName getFirstNameInGroup(TaxonName basionym) {
212
        for (NameRelationship nameRel : basionym.getRelationsFromThisName()){
213
            if (nameRel.getType() != null && nameRel.getType().equals(NameRelationshipType.BASIONYM())){
214
                if (nameRel.getToName().equals(firstNameInGroup)){
215
                    return firstNameInGroup;
216
                }
217
            }
218
        }
219
        return basionym;
220
    }
221

    
222
    private Set<TaxonName> getReplacedSynonymClosure(TaxonName name) {
223
        Set<TaxonName> set = name.getReplacedSynonyms();
224
        if (set.isEmpty()){
225
            return set;
226
        }
227
        Set<TaxonName> result = new HashSet<>();
228
        for (TaxonName replSyn : set){
229
            boolean notYetContained = result.add(replSyn);
230
            if (notYetContained){
231
                result.addAll(replSyn.getReplacedSynonyms());
232
            }
233
        }
234
        return result;
235
    }
236

    
237
    private TaxonName getPreferredInBasionymGroup(TaxonName name) {
238
        Set<TaxonName> candidates = new HashSet<>();
239
        //get all final basionyms, except for those being part of a basionym circle
240
        for (TaxonName candidate : name.getBasionyms()){
241
            if (candidate != null
242
                    && candidate.getHomotypicalGroup().equals(name.getHomotypicalGroup())
243
                    && !hasBasionymCircle(candidate, null)){
244
                candidate = getPreferredInBasionymGroup(candidate);
245
                candidates.add(candidate);
246
            }
247
        }
248

    
249
        if (candidates.isEmpty()){
250
            return name;
251
        }else if (candidates.size() == 1){
252
            return candidates.iterator().next();
253
        }else{
254
            TaxonName result = candidates.iterator().next();
255
            candidates.remove(result);
256
            for (TaxonName candidate : candidates){
257
                if (super.compare(result, candidate, false) > 0){
258
                    result = candidate;
259
                }
260
            }
261
            return result;
262
        }
263
    }
264

    
265
    private boolean hasBasionymCircle(TaxonName name, Set<TaxonName> existing) {
266
        if (existing == null){
267
            existing = new HashSet<>();
268
        }
269
        if (existing.contains(name)){
270
            return true;
271
        }else{
272
            Set<TaxonName> basionyms = name.getBasionyms();
273
            if (basionyms.isEmpty()){
274
                return false;
275
            }
276
            existing.add(name);
277
            for (TaxonName basionym : basionyms){
278
                if (hasBasionymCircle(basionym, existing)){
279
                    return true;
280
                }
281
            }
282
            return false;
283
        }
284
    }
285
}
(1-1/7)