Project

General

Profile

Revision 7229f9d4

ID7229f9d46ec015aa3b47562c42203b139c805bfb
Parent 20cfb7a6
Child 2070f523

Added by Katja Luther over 3 years ago

ref #4232: made some changes to fullfill the comparator contract

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/TermIdInVocabularyComparator.java
26 26
    public int compare(T term1, T term2) {
27 27
        String label1;
28 28
        String label2;
29
        if (term1 == term2){
29
        if (term1.equals(term2)){
30 30
            return 0;
31 31
        }
32
        if (term1 == null){
33
            return 1;
34
        }
35
        if (term2 == null){
36
            return -1;
37
        }
32

  
38 33
        if (term1.getIdInVocabulary() == null){
39 34
            label1 = CdmUtils.Nz(term1.getTitleCache());
40 35
        }else{
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/TermLanguageComparator.java
1 1
/**
2 2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
3
* European Distributed Institute of Taxonomy
4 4
* http://www.e-taxonomy.eu
5
* 
5
*
6 6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
* See LICENSE.TXT at the top of this package for the full license terms.
8 8
*/
......
25 25

  
26 26
	private Language defaultLanguage = Language.DEFAULT();
27 27
	private Language compareLanguage = Language.DEFAULT();
28
	
29
	
30
	
28

  
29

  
30

  
31 31
	/**
32
	 * 
32
	 *
33 33
	 */
34 34
	public TermLanguageComparator() {
35 35
		// TODO Auto-generated constructor stub
......
38 38
	/* (non-Javadoc)
39 39
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
40 40
	 */
41
	public int compare(T termbase1, T termbase2) throws RuntimeException{
41
	@Override
42
    public int compare(T termbase1, T termbase2) throws RuntimeException{
43

  
44
	    if (termbase1.equals(termbase2)){
45
	        return 0;
46
	    }
42 47
		String label1 = makeCompareLabel(termbase1);
43 48
		String label2 = makeCompareLabel(termbase2);
44 49

  
45 50
		return label1.compareTo(label2);
46 51
	}
47
	
52

  
48 53
	private String makeCompareLabel(T termbase){
49 54
		String result;
50 55
		if (termbase == null){
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/metadata/CdmMetaData.java
177 177
		@Override
178 178
        public int compare(String version1, String version2) {
179 179
			int result = 0;
180

  
181
			if (version1.equals(version2)){
182
			    return 0;
183
			}
184

  
180 185
			String[] version1Split = version1.split("\\.");
181 186
			String[] version2Split = version2.split("\\.");
182 187

  
......
188 193
			if(depth != null && (version1Split.length < depth || version2Split.length < depth )){
189 194
				throwException("Desired depth can not be achieved with the given strings. depth: " + depth  + ", version1: " + version1 + ", version2:" + version2);
190 195
			}
191

  
192
			int length = (depth == null ||version1Split.length < depth) ? version1Split.length : depth;
196
			//use the shorter version to avoid arrayOutOfBoundsException, if version2Split.length < version1Split.length but > depth
197
			int length = (version1Split.length < version2Split.length) ? version1Split.length: version2Split.length;
198
			if (depth != null){
199
			    length = length<depth?length:depth;
200
			}
193 201
			for (int i = 0; i < length; i++){
194 202
				Long version1Part = Long.valueOf(version1Split[i]);
195 203
				Long version2Part = Long.valueOf(version2Split[i]);
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/name/HomotypicalGroupComparator.java
27 27
	public int compare(HomotypicalGroup group1, HomotypicalGroup group2) {
28 28
		TaxonBase<?> firstTypified1 = null;
29 29
		TaxonBase<?> firstTypified2 = null;
30
		if (group1.equals(group2)){
31
		    return 0;
32
		}
30 33
		TaxonComparator taxComparator = new HomotypicGroupTaxonComparator(null);
31 34
		Set<TaxonNameBase> typifiedNames1 = group1.getTypifiedNames();
32 35
		List<TaxonBase> taxonBasesOfTypifiedNames = new ArrayList<TaxonBase>();
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/TaxonNaturalComparator.java
28 28
	public int compare(TaxonNode node1, TaxonNode node2) {
29 29
	   // System.out.println("compare node 1: "+ node1.getTaxon().getTitleCache() + " - node 2: " + node2.getTaxon().getTitleCache());
30 30
	    if (node1.equals(node2)) {
31

  
32 31
	        return 0;
33 32
        }
33
	    // if we do not check for null for the treeIndex we always return 1 if one of the nodes have no treeIndex
34
	    if (node1.treeIndex() == null){
35
	        return 1;
36
	    }
37
	    if (node2.treeIndex() == null){
38
            return -1;
39
        }
34 40

  
35 41
	    if (node1.isAncestor(node2)) {
36

  
37 42
            return -1;
38 43
        }
39 44
		if (node2.isAncestor(node1)) {
40

  
41 45
            return 1;
42 46
        }
43 47

  
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxon/TaxonDaoHibernateImpl.java
167 167
            List<TaxonBase> results = query.list();
168 168

  
169 169
            defaultBeanInitializer.initializeAll(results, propertyPaths);
170
            //TaxonComparatorSearch comp = new TaxonComparatorSearch();
170

  
171 171
            //Collections.sort(results, comp);
172 172
            return results;
173 173
        }
......
646 646

  
647 647
        taxonBase.removeSources();
648 648

  
649

  
649 650
        if (taxonBase instanceof Taxon){ // is Taxon
650 651
            Taxon taxon = ((Taxon)taxonBase);
651 652
            Set<Synonym> syns = new HashSet<>(taxon.getSynonyms());
......
1037 1038

  
1038 1039
        @Override
1039 1040
        public int compare(TaxonRelationship o1, TaxonRelationship o2) {
1040
            return o1.getFromTaxon().getTitleCache().compareTo(o2.getFromTaxon().getTitleCache());
1041
            if (o1.equals(o2)){
1042
                return 0;
1043
            }
1044
            int result = o1.getFromTaxon().getTitleCache().compareTo(o2.getFromTaxon().getTitleCache());
1045
            if (result == 0 ){
1046
                result = o1.getUuid().compareTo(o2.getUuid());
1047
            }
1048
            return result;
1041 1049
        }
1042 1050

  
1043 1051
    }

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)