Project

General

Profile

Download (8.77 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.model.taxon;
11

    
12
import java.util.Comparator;
13
import java.util.HashMap;
14
import java.util.HashSet;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.model.name.NameRelationship;
21
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
22
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
23

    
24
/**
25
 * This class orders synonyms of a homotypic group,
26
 * first by
27
 * <ul>
28
 *  <li>Basionym groups (the basionym and all names derived from this basionym)
29
 *      should be kept together in a subgroup</li>
30
 *  <li>The order of the subgroups is defined by the ordering of their
31
 *       basionyms (according to the following ordering)</li>
32
 *  <li>If a name is illegitimate or not does play a role for ordering</li>
33
 *  <li>Names with publication year should always come first</li>
34
 *  <li>Names with no publication year 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 http://dev.e-taxonomy.eu/trac/ticket/3338<BR>
40
 *
41
 * @author a.mueller
42
 * @created 02.03.2016
43
 */
44
public class HomotypicGroupTaxonComparator extends TaxonComparator {
45
    private static final long serialVersionUID = -5088210641256430878L;
46
    @SuppressWarnings("unused")
47
	private static final Logger logger = Logger.getLogger(HomotypicGroupTaxonComparator.class);
48

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

    
52
    /**
53
     * @param firstNameInGroup
54
     */
55
    public HomotypicGroupTaxonComparator(@SuppressWarnings("rawtypes") TaxonBase firstTaxonInGroup) {
56
        this.firstTaxonInGroup = firstTaxonInGroup;
57
        this.firstNameInGroup = firstTaxonInGroup == null ? null: firstTaxonInGroup.getName();
58
    }
59

    
60
    /**
61
     *
62
     * @see TaxonComparator#compare(TaxonBase, TaxonBase)
63
     * @see java.lang.String#compareTo(String)
64
     * @see	java.util.Comparator#compare(java.lang.Object, java.lang.Object)
65
     */
66
    @Override
67
    public int compare(
68
            @SuppressWarnings("rawtypes") TaxonBase taxonBase1,
69
            @SuppressWarnings("rawtypes") TaxonBase taxonBase2) {
70

    
71
        TaxonNameBase<?,?> name1 = taxonBase1.getName();
72
        TaxonNameBase<?,?> name2 = taxonBase2.getName();
73
        System.out.print(name1.getTitleCache() +" : "+ name2.getTitleCache());
74

    
75

    
76
        int compareStatus = compareStatus(name1, name2);
77
        if (compareStatus != 0){
78
            return compareStatus;
79
        }
80

    
81
        //not same homotypical group -
82
        //NOTE: this comparator should usually not be used
83
        //      for comparing names of different homotypcial groups.
84
        //      The following is only to have a defined compare behavior
85
        //      which follows the contract of Comparator#compare.
86
        if (name1 == null ||
87
            name2 == null ||
88
            ! name1.getHomotypicalGroup().equals(name2.getHomotypicalGroup())){
89

    
90
            int result = name1.getHomotypicalGroup().getUuid().toString()
91
                    .compareTo(name2.getHomotypicalGroup().getUuid().toString());
92
            System.out.println(": t" + result);
93
            System.out.println(name1.getHomotypicalGroup().getUuid());
94
            System.out.println(name2.getHomotypicalGroup().getUuid());
95
            return result;
96
        }
97

    
98
        //same homotypical group ...
99
        //one taxon is first in group
100
        if (taxonBase1.equals(firstTaxonInGroup)){
101
            return -1;
102
        }else if (taxonBase2.equals(firstTaxonInGroup)){
103
            return 1;
104
        }
105

    
106
        System.out.print("_");
107
        //same name => compare on taxon level
108
        if (name1.equals(name2)){
109
            return super.compare(taxonBase1, taxonBase2);  //if name is the same compare on taxon level
110
        }
111
        System.out.print("_");
112

    
113
        TaxonNameBase<?,?> basionym1 = getPreferredInBasionymGroup(name1);
114
        TaxonNameBase<?,?> basionym2 = getPreferredInBasionymGroup(name2);
115

    
116
        int compareResult;
117
        if (basionym1.equals(basionym2)){
118
            //both names belong to same basionym sub-group
119
            compareResult = handleSameBasionym(basionym1, name1, name2);
120
        }else{
121
            compareResult = compareBasionyms(basionym1, basionym2);
122
        }
123

    
124
        if (compareResult != 0){
125
            System.out.println(": " + compareResult);
126
            return compareResult;
127
        }else{
128
            //names are uncomparable on name level (except for uuid, id, etc.)
129

    
130
            int result = super.compare(taxonBase1, taxonBase2);
131
            System.out.println(": = " + result);
132
            return result;
133
        }
134
    }
135

    
136
//    /**
137
//     * @param homotypicalGroup
138
//     * @return
139
//     */
140
//    private TaxonBase<?> getFirstInHomotypicalGroup(HomotypicalGroup homotypicalGroup, Collection<TaxonBase<?>> existing) {
141
//        List<TaxonBase<?>> candidates =  new ArrayList<TaxonBase<?>>();
142
//        for (TaxonBase<?> candidate : existing){
143
//            if (homotypicalGroup.getTypifiedNames().contains(candidate.getName())){
144
//                candidates.add(candidate);
145
//            }
146
//        }
147
//        Collections.sort(candidates, this);
148
//        return candidates.isEmpty() ? null : candidates.get(0);
149
//    }
150

    
151
    /**
152
     * Compare 2 names which have the same basionym.
153
     * The names must not be equal to each other but may be equal
154
     * to the basionym.
155
     * @param basionym the basionym
156
     * @param name1 first name to compare
157
     * @param name2 second name to compare
158
     * @return compare value according to the {@link Comparator#compare(Object, Object)} contract.
159
     */
160
    private int handleSameBasionym(TaxonNameBase<?, ?> basionym,
161
            TaxonNameBase<?, ?> name1,
162
            TaxonNameBase<?, ?> name2) {
163

    
164
        if (basionym.equals(name1)){
165
            return -1;
166
        }else if (basionym.equals(name2)){
167
            return 1;
168
        }else{
169
            super.compare(name1, name2);
170
        }
171
        return 0;
172
    }
173

    
174
    /**
175
     * @param basionym1
176
     * @param basionym2
177
     * @return
178
     */
179
    private int compareBasionyms(TaxonNameBase<?,?> basionym1Orig, TaxonNameBase<?,?> basionym2Orig) {
180
        //one taxon is first in group
181
        TaxonNameBase<?,?> basionym1 = getFirstNameInGroup(basionym1Orig);
182
        TaxonNameBase<?,?> basionym2 = getFirstNameInGroup(basionym2Orig);
183

    
184
        if (basionym1.equals(firstNameInGroup)){
185
            return -1;
186
        }else if (basionym2.equals(firstTaxonInGroup)){
187
            return 1;
188
        }
189

    
190
        if (getReplacedSynonymClosure(basionym1).contains(basionym2)){
191
            return 1;
192
        }else if (getReplacedSynonymClosure(basionym2).contains(basionym1)){
193
            return -1;
194
        }
195

    
196
        int result = super.compare(basionym1, basionym2);
197
        return result;
198

    
199

    
200
    }
201

    
202
    /**
203
     * @param basionym
204
     * @return
205
     */
206
    private TaxonNameBase<?, ?> getFirstNameInGroup(TaxonNameBase<?, ?> basionym) {
207
        for (NameRelationship nameRel : basionym.getRelationsFromThisName()){
208
            if (nameRel.getType() != null && nameRel.getType().equals(NameRelationshipType.BASIONYM())){
209
                if (nameRel.getToName().equals(firstNameInGroup)){
210
                    return firstNameInGroup;
211
                }
212
            }
213
        }
214
        return basionym;
215
    }
216

    
217
    /**
218
     * @param basionym1
219
     * @return
220
     */
221
    @SuppressWarnings("rawtypes")
222
    private Set<TaxonNameBase> getReplacedSynonymClosure(TaxonNameBase<?, ?> name) {
223
        Set<TaxonNameBase> set = name.getReplacedSynonyms();
224
        if (set.isEmpty()){
225
            return set;
226
        }
227
        Set<TaxonNameBase> result = new HashSet<TaxonNameBase>();
228
        for (TaxonNameBase<?,?> replSyn : set){
229
            boolean notYetContained = result.add(replSyn);
230
            if (notYetContained){
231
                result.addAll(replSyn.getReplacedSynonyms());
232
            }
233
        }
234
        return result;
235
    }
236

    
237
    /**
238
     * @param name1
239
     * @return
240
     */
241
    private TaxonNameBase<?,?> getPreferredInBasionymGroup(TaxonNameBase<?,?> name) {
242
        HashMap<UUID, TaxonNameBase<?,?>> candidates = new HashMap<UUID, TaxonNameBase<?,?>>();
243
        for (TaxonNameBase<?,?> candidate : name.getBasionyms()){
244
            if (candidate != null && candidate.getHomotypicalGroup().equals(name.getHomotypicalGroup())){
245
                candidates.put(candidate.getUuid(), candidate);
246
            }
247
        }
248

    
249
        if (candidates.isEmpty()){
250
            return name;
251
        }else{
252
            return candidates.get(candidates.keySet().iterator().next());
253
        }
254
    }
255

    
256

    
257
}
(3-3/21)