Project

General

Profile

« Previous | Next » 

Revision 1b50bc76

Added by Andreas Müller over 2 years ago

move model comparators to own package

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/compare/AlphabeticallyAscendingNamedAreaComparator.java
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.compare;
11

  
12
import java.util.Comparator;
13

  
14
import eu.etaxonomy.cdm.model.location.NamedArea;
15

  
16
/**
17
 * @author freimeier
18
 * @since 07.02.2018
19
 */
20
public class AlphabeticallyAscendingNamedAreaComparator implements Comparator<NamedArea>{
21

  
22
    @Override
23
    public int compare(NamedArea o1, NamedArea o2) {
24
        return o2.compareTo(o1);
25
    }
26
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/compare/OrderIndexComparator.java
1
/**
2
* Copyright (C) 2018 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
package eu.etaxonomy.cdm.compare;
10

  
11
import java.util.Comparator;
12

  
13
/**
14
 * @author k.luther
15
 * @since 07.12.2018
16
 */
17
public class OrderIndexComparator implements Comparator<Integer> {
18

  
19
    private static OrderIndexComparator instance;
20

  
21
    public static OrderIndexComparator instance(){
22
        if(instance == null){
23
            instance = new OrderIndexComparator();
24
        }
25
        return instance;
26
    }
27

  
28
    @Override
29
    public int compare(Integer orderIndex1, Integer orderIndex2) {
30
       if (orderIndex1 == orderIndex2){
31
           return 0;
32
       }
33
       if (orderIndex1 == null){
34
           return 1;
35
       }
36
       if (orderIndex2 == null){
37
           return -1;
38
       }
39
       if (orderIndex1<orderIndex2){
40
           return -1;
41
       }else{
42
           return 1;
43
       }
44
    }
45
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/compare/TreeIndexComparator.java
1
/**
2
* Copyright (C) 2016 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
package eu.etaxonomy.cdm.compare;
10

  
11
import java.util.Comparator;
12

  
13
import eu.etaxonomy.cdm.model.common.ITreeNode;
14
import eu.etaxonomy.cdm.model.common.TreeIndex;
15

  
16
/**
17
 * @author a.mueller
18
 * @since 05.07.2016
19
 *
20
 * Comparator for tree indexes.
21
 * Compares the tree indexes node by node, sorted by node number.
22
 * If one index is shorter than the other one but
23
 */
24
public class TreeIndexComparator implements Comparator<TreeIndex>{
25

  
26
    @Override
27
    public int compare(TreeIndex treeIndex1, TreeIndex treeIndex2) {
28
        if (treeIndex1 == null && treeIndex2 == null){
29
            return 0;
30
        }else if (treeIndex1 == null){
31
            return -1;
32
        }else if (treeIndex2 == null){
33
            return 1;
34
        }
35
        if (treeIndex1.equals(treeIndex2)){
36
            return 0;
37
        }
38

  
39
        String[] splits1 = treeIndex1.toString().split(ITreeNode.separator);
40
        String[] splits2 = treeIndex2.toString().split(ITreeNode.separator);
41

  
42

  
43
        for (int i=0; i < splits1.length; i++){
44
            if (splits2.length <= i){
45
                return 1;
46
            }
47
            int c = splits1[i].compareTo(splits2[i]);
48
            if (c != 0){
49
                return c;
50
            }
51
        }
52
        return -1;
53

  
54
    }
55

  
56
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/AlphabeticallyAscendingNamedAreaComparator.java
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.model.common;
11

  
12
import java.util.Comparator;
13

  
14
import eu.etaxonomy.cdm.model.location.NamedArea;
15

  
16
/**
17
 * @author freimeier
18
 * @since 07.02.2018
19
 *
20
 */
21
public class AlphabeticallyAscendingNamedAreaComparator implements Comparator<NamedArea>{
22

  
23
    @Override
24
    public int compare(NamedArea o1, NamedArea o2) {
25
        return o2.compareTo(o1);
26
    }
27
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/OrderIndexComparator.java
1
/**
2
* Copyright (C) 2018 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
package eu.etaxonomy.cdm.model.common;
10

  
11
import java.util.Comparator;
12

  
13
/**
14
 * @author k.luther
15
 * @since 07.12.2018
16
 *
17
 */
18
public class OrderIndexComparator implements Comparator<Integer> {
19

  
20
    private static OrderIndexComparator instance;
21

  
22

  
23

  
24
    public static OrderIndexComparator instance(){
25
        if(instance == null){
26
            instance = new OrderIndexComparator();
27
        }
28
        return instance;
29
    }
30

  
31
    /**
32
     * {@inheritDoc}
33
     */
34
    @Override
35
    public int compare(Integer orderIndex1, Integer orderIndex2) {
36
       if (orderIndex1 == orderIndex2){
37
           return 0;
38
       }
39
       if (orderIndex1 == null){
40
           return 1;
41
       }
42
       if (orderIndex2 == null){
43
           return -1;
44
       }
45
       if (orderIndex1<orderIndex2){
46
           return -1;
47
       }else{
48
           return 1;
49
       }
50

  
51
    }
52

  
53
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/TreeIndex.java
16 16
import java.util.Map;
17 17
import java.util.regex.Pattern;
18 18

  
19
import eu.etaxonomy.cdm.compare.TreeIndexComparator;
19 20
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
20 21
import eu.etaxonomy.cdm.model.term.TermNode;
21 22

  
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/TreeIndexComparator.java
1
/**
2
* Copyright (C) 2016 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
package eu.etaxonomy.cdm.model.common;
10

  
11
import java.util.Comparator;
12

  
13
/**
14
 * @author a.mueller
15
 * @since 05.07.2016
16
 *
17
 * Comparator for tree indexes.
18
 * Compares the tree indexes node by node, sorted by node number.
19
 * If one index is shorter than the other one but
20
 */
21
public class TreeIndexComparator implements Comparator<TreeIndex>{
22

  
23
    @Override
24
    public int compare(TreeIndex treeIndex1, TreeIndex treeIndex2) {
25
        if (treeIndex1 == null && treeIndex2 == null){
26
            return 0;
27
        }else if (treeIndex1 == null){
28
            return -1;
29
        }else if (treeIndex2 == null){
30
            return 1;
31
        }
32
        if (treeIndex1.equals(treeIndex2)){
33
            return 0;
34
        }
35

  
36
        String[] splits1 = treeIndex1.toString().split(ITreeNode.separator);
37
        String[] splits2 = treeIndex2.toString().split(ITreeNode.separator);
38

  
39

  
40
        for (int i=0; i < splits1.length; i++){
41
            if (splits2.length <= i){
42
                return 1;
43
            }
44
            int c = splits1[i].compareTo(splits2[i]);
45
            if (c != 0){
46
                return c;
47
            }
48
        }
49
        return -1;
50

  
51
    }
52

  
53
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dto/TaxonNodeDtoByRankAndNameComparator.java
11 11
import java.io.Serializable;
12 12
import java.util.Comparator;
13 13

  
14
import eu.etaxonomy.cdm.model.common.OrderIndexComparator;
14
import eu.etaxonomy.cdm.compare.OrderIndexComparator;
15 15
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
16 16
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
17 17
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TreeIndexComparatorTest.java
11 11
import org.junit.Assert;
12 12
import org.junit.Test;
13 13

  
14
import eu.etaxonomy.cdm.compare.TreeIndexComparator;
14 15
import eu.etaxonomy.cdm.model.common.TreeIndex;
15
import eu.etaxonomy.cdm.model.common.TreeIndexComparator;
16 16

  
17 17

  
18 18
/**

Also available in: Unified diff