Project

General

Profile

« Previous | Next » 

Revision c1f24dc3

Added by Andreas Müller about 8 years ago

Some additional tests for sorting homotypical groups #3338

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/HomotypicGroupTaxonComparator.java
10 10
package eu.etaxonomy.cdm.model.taxon;
11 11

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

  
18 16
import org.apache.log4j.Logger;
19 17

  
......
82 80

  
83 81
        TaxonNameBase<?,?> name1 = taxonBase1.getName();
84 82
        TaxonNameBase<?,?> name2 = taxonBase2.getName();
85
//        System.out.print(name1.getTitleCache() +" : "+ name2.getTitleCache());
83
//      System.out.println(name1.getTitleCache() +" : "+ name2.getTitleCache());
86 84

  
87 85

  
88 86
        int compareStatus = compareStatus(name1, name2);
......
106 104
                    name2.getHomotypicalGroup().getUuid().toString() :
107 105
                    taxonBase2.getUuid().toString();
108 106
            int result = compareString1.compareTo(compareString2);
109
//            System.out.println(": t" + result);
110
//            System.out.println(name1.getHomotypicalGroup().getUuid());
111
//            System.out.println(name2.getHomotypicalGroup().getUuid());
112 107
            return result;
113 108
        }
114 109

  
......
120 115
            return 1;
121 116
        }
122 117

  
123
//        System.out.print("_");
124 118
        //same name => compare on taxon level
125 119
        if (name1.equals(name2)){
126 120
            return super.compare(taxonBase1, taxonBase2);  //if name is the same compare on taxon level
127 121
        }
128
//        System.out.print("_");
129 122

  
130 123
        TaxonNameBase<?,?> basionym1 = getPreferredInBasionymGroup(name1);
131 124
        TaxonNameBase<?,?> basionym2 = getPreferredInBasionymGroup(name2);
......
139 132
        }
140 133

  
141 134
        if (compareResult != 0){
142
//            System.out.println(": " + compareResult);
135
//          System.out.println(": " + compareResult);
143 136
            return compareResult;
144 137
        }else{
145 138
            //names are uncomparable on name level (except for uuid, id, etc.)
146

  
147 139
            int result = super.compare(taxonBase1, taxonBase2);
148
//            System.out.println(": = " + result);
140
//          System.out.println(": = " + result);
149 141
            return result;
150 142
        }
151 143
    }
......
163 155
//        }
164 156
//        Collections.sort(candidates, this);
165 157
//        return candidates.isEmpty() ? null : candidates.get(0);
166
//    }
167
//
168
//    /**
169
//     * @param name1
170
//     * @return
171
//     */
172
//    private TaxonNameBase<?, ?> getFirstNameInHomotypicalGroup(TaxonNameBase<?, ?> name1) {
173
//        TaxonNameBase<?, ?> prefName = getPreferredInBasionymGroup(name1);
174
//        return null;
175 158
//    }
176 159

  
160

  
177 161
    /**
178 162
     * Compare 2 names which have the same basionym.
179 163
     * The names must not be equal to each other but may be equal
......
218 202

  
219 203
        if (basio2IsReplacedSynForBasio1 && !basio1IsReplacedSynForBasio2){
220 204
            return 1;
221
        }else if (basio1IsReplacedSynForBasio2){  //&& !basio2IsReplacedSynForBasio1
205
        }else if (basio1IsReplacedSynForBasio2 && !basio2IsReplacedSynForBasio1){
222 206
            return -1;
223 207
        }
224 208

  
......
262 246
    }
263 247

  
264 248
    /**
265
     * @param name1
249
     * @param name
266 250
     * @return
267 251
     */
268 252
    private TaxonNameBase<?,?> getPreferredInBasionymGroup(TaxonNameBase<?,?> name) {
269
        HashMap<UUID, TaxonNameBase<?,?>> candidates = new HashMap<UUID, TaxonNameBase<?,?>>();
253
        Set<TaxonNameBase<?,?>> candidates = new HashSet<TaxonNameBase<?,?>>();
254
        //get all final basionyms, except for those being part of a basionym circle
270 255
        for (TaxonNameBase<?,?> candidate : name.getBasionyms()){
271
            if (candidate != null && candidate.getHomotypicalGroup().equals(name.getHomotypicalGroup())){
272
                candidates.put(candidate.getUuid(), candidate);
256
            if (candidate != null
257
                    && candidate.getHomotypicalGroup().equals(name.getHomotypicalGroup())
258
                    && !hasBasionymCircle(candidate, null)){
259
                candidate = getPreferredInBasionymGroup(candidate);
260
                candidates.add(candidate);
273 261
            }
274 262
        }
275 263

  
276 264
        if (candidates.isEmpty()){
277 265
            return name;
266
        }else if (candidates.size() == 1){
267
            return candidates.iterator().next();
268
        }else{
269
            TaxonNameBase<?,?> result = candidates.iterator().next();
270
            candidates.remove(result);
271
            for (TaxonNameBase<?,?> candidate : candidates){
272
                if (super.compare(result, candidate) > 0){
273
                    result = candidate;
274
                }
275
            }
276
            return result;
277
        }
278
    }
279

  
280
    /**
281
     * @param candidate
282
     * @return
283
     */
284
    private boolean hasBasionymCircle(TaxonNameBase<?, ?> name, Set<TaxonNameBase<?,?>> existing) {
285
        if (existing == null){
286
            existing = new HashSet<TaxonNameBase<?,?>>();
287
        }
288
        if (existing.contains(name)){
289
            return true;
278 290
        }else{
279
            return candidates.get(candidates.keySet().iterator().next());
291
            Set<TaxonNameBase> basionyms = name.getBasionyms();
292
            if (basionyms.isEmpty()){
293
                return false;
294
            }
295
            existing.add(name);
296
            for (TaxonNameBase basionym : basionyms){
297
                if (hasBasionymCircle(basionym, existing)){
298
                    return true;
299
                }
300
            }
301
            return false;
280 302
        }
281 303
    }
282 304

  
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/taxon/HomotypicGroupTaxonComparatorTest.java
327 327

  
328 328
    @Test
329 329
    public void testCompare_NoCircularProblemsWithBasionyms() {
330
        //TODO not yet fully implemented
331 330

  
332 331
        taxon1.addHomotypicSynonymName(botName3, null, null);
333 332
        taxon1.addHomotypicSynonymName(botName5, null, null);
334 333
        botName3.addBasionym(botName5);
335 334

  
336
//        taxon1.addHomotypicSynonymName(botName2, null, null);
337
//        taxon1.addHomotypicSynonymName(botName4, null, null);
338
//        botName2.addBasionym(botName4);
339

  
340 335
        Assert.assertEquals(botName1.getHomotypicalGroup(), botName5.getHomotypicalGroup());
341 336
        botName5.addBasionym(botName1);
342 337
        botName1.addBasionym(botName3);
......
345 340
        list.add(taxon1);
346 341
        Collections.sort(list, new HomotypicGroupTaxonComparator(null));
347 342

  
348
//        Assert.assertEquals(botName1, list.get(0).getName());
349
//        Assert.assertEquals(botName3, list.get(1).getName());
350
//        Assert.assertEquals(botName5, list.get(2).getName());
343
        Assert.assertEquals(botName1, list.get(0).getName());
344
        Assert.assertEquals(botName3, list.get(1).getName());
345
        Assert.assertEquals(botName5, list.get(2).getName());
346

  
347
        //additional basionym for botName3
348
        taxon1.addHomotypicSynonymName(botName2, null, null);
349
        Assert.assertEquals(botName1.getHomotypicalGroup(), botName2.getHomotypicalGroup());
350
        botName3.addBasionym(botName2);
351
        list.clear();
352
        list.addAll(taxon1.getSynonyms());
353
        list.add(taxon1);
354
        Collections.sort(list, new HomotypicGroupTaxonComparator(null));
355

  
356
        Assert.assertEquals(botName1, list.get(0).getName());
357
        Assert.assertEquals(botName2, list.get(1).getName());
358
        Assert.assertEquals(botName3, list.get(2).getName());
359
        Assert.assertEquals(botName5, list.get(3).getName());
351 360

  
352 361
    }
353 362

  

Also available in: Unified diff