Project

General

Profile

« Previous | Next » 

Revision 56613858

Added by Andreas Müller about 3 years ago

ref #9356 clean up SourceComparator

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/SourceComparator.java
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
*/
1 9
package eu.etaxonomy.taxeditor.ui.section.description;
2 10

  
3

  
4 11
import java.util.Comparator;
5 12

  
6 13
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
......
11 18
/**
12 19
 * @author pplitzner
13 20
 * @date Apr 13, 2016
14
 *
15 21
 */
16 22
public class SourceComparator  <T extends OriginalSourceBase> implements Comparator<T> {
23

  
17 24
    @Override
18 25
    public int compare(T o1, T o2) {
19
        int id1 = o1.getId();
20
        int id2 = o2.getId();
26

  
27
        //same and null compare, to be on the save side
28
        if (o1 == o2){
29
            return 0;
30
        }else if (o1 == null){
31
            return -1;
32
        }else if (o2 == null){
33
            return 1;
34
        }
35

  
21 36
        boolean isDescriptionElementSource1 = false;
22 37
        boolean isDescriptionElementSource2 = false;
23
        if (o1 instanceof DescriptionElementSource){
38
        if (o1.isInstanceOf(DescriptionElementSource.class)){
24 39
            isDescriptionElementSource1 = true;
25 40
        }
26
        if (o2 instanceof DescriptionElementSource){
41
        if (o2.isInstanceOf(DescriptionElementSource.class)){
27 42
            isDescriptionElementSource2 = true;
28 43
        }
29
        int result = 0;
30 44
        if (isDescriptionElementSource1 != isDescriptionElementSource2){
31 45
            if (isDescriptionElementSource1){
32 46
                return -1;
......
34 48
                return 1;
35 49
            }
36 50
        }
37
        OriginalSourceType type1 = o1.getType();
38
        OriginalSourceType type2 = o2.getType();
39
        Reference citation1 = o1.getCitation();
40
        Reference citation2 = o2.getCitation();
41

  
42

  
43 51

  
44 52
        // the newly created should always be on top
45
        if (id1 == 0){
46
            if(id2!=0) {
47
                return -1;
48
            }
49
            else{
50
                result = 0;
51
            }
52
        } else if(id2==0){
53
        if (!o1.isPersited() && o2.isPersited()) {
54
            return -1;
55
        } else if(o1.isPersited() && !o2.isPersited()){
53 56
            return 1;
54 57
        }
55 58

  
56 59
        // sort by type (Primary taxonomic > Primary Media > others
57 60
        // alphabetically by reference title cache)
58
        else if (type1 == null){
59
            if(type2==null){
60
                result = 0;
61
            }
62
            else{
63
                result = -1;
61
        OriginalSourceType type1 = o1.getType();
62
        OriginalSourceType type2 = o2.getType();
63
        if (type1 == null){
64
            if (type2!=null){
65
                return -1;
64 66
            }
65 67
        } else if (type2 == null){
66
            result = 1;
68
            return 1;
67 69
        } else if(type1.equals(type2)){
68
            result = 0;
70
            //continue with citation compare
69 71
        } else if (type1.equals(OriginalSourceType.PrimaryTaxonomicSource)){
70
            result = 1;
72
            return 1;
71 73
        } else if (type2.equals(OriginalSourceType.PrimaryTaxonomicSource)){
72
            result = -1;
74
            return -1;
73 75
        } else if (type1.equals(OriginalSourceType.PrimaryMediaSource)){
74
            result = 1;
76
            return 1;
75 77
        } else if (type2.equals(OriginalSourceType.PrimaryMediaSource)){
76
            result = -1;
78
            return -1;
77 79
        }
78 80

  
81
        int result;
82

  
79 83
        //sort by citation title cache if types are equal
80
        if (result == 0){
81
            if(citation1!=null && citation2!=null){
82
                result = citation1.getTitleCache().compareTo(citation2.getTitleCache());
84
        Reference citation1 = o1.getCitation();
85
        Reference citation2 = o2.getCitation();
86
        if(citation1!=null && citation2!=null){
87
            result = citation1.getTitleCache().compareTo(citation2.getTitleCache());
88
            if (result != 0){
89
                return result;
83 90
            }
91
        }
84 92

  
85
            if(o2.getCreated()!=null && o1.getCreated()!=null){
86
                result = o1.getCreated().compareTo(o2.getCreated());
87
            }else if (o1.getCreated() == null ){
88
                if (o2.getCreated() == null){
89
                    result = 0;
90
                }else{
91
                    return -1;
92
                }
93
            }else if (o2.getCreated() == null){
94
                return 1;
93
        //sort by created
94
        if(o2.getCreated()!=null && o1.getCreated()!=null){
95
            result = o1.getCreated().compareTo(o2.getCreated());
96
            if (result != 0){
97
                return result;
95 98
            }
96
            if (result == 0){
97
                //default fallback
98
                return o1.getUuid().compareTo(o2.getUuid());
99
        }else if (o1.getCreated() == null){
100
            if (o2.getCreated() != null){
101
                return -1;
99 102
            }
103
        }else if (o2.getCreated() == null){
104
            return 1;
100 105
        }
101
        return result;
102

  
103 106

  
107
        //default fallback
108
        return o1.getUuid().compareTo(o2.getUuid());
104 109
    }
105
}
110
}

Also available in: Unified diff