Project

General

Profile

« Previous | Next » 

Revision 8497c5f5

Added by Andreas Kohlbecker almost 7 years ago

refactoring NodeSortMode

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/NodeSortMode.java
1 1
package eu.etaxonomy.cdm.api.service;
2 2

  
3
import java.util.Comparator;
4

  
5
import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
6
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
7
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
8
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator;
9

  
3 10
public enum NodeSortMode {
4
	NaturalOrder,
5
	RankAndAlphabeticalOrder,
6
	AlphabeticalOrder
11

  
12
	NaturalOrder(TaxonNaturalComparator.class),
13
	/**
14
     * sorts by TaxonName titleCaches and rank associated with the taxonNodes
15
     */
16
	RankAndAlphabeticalOrder(TaxonNodeByRankAndNameComparator.class),
17
	/**
18
	 * sorts by TaxonName titleCaches associated with the taxonNodes
19
	 */
20
	AlphabeticalOrder(TaxonNodeByNameComparator.class);
21

  
22
	private Class<? extends Comparator<TaxonNode>> type;
23

  
24
    NodeSortMode(Class<? extends Comparator<TaxonNode>> type){
25
	    this.type = type;
26
	}
27

  
28
    /**
29
     * @return
30
     */
31
    public Comparator<TaxonNode> newComparator() {
32
        try {
33
            return type.newInstance();
34
        } catch (InstantiationException e) {
35
            throw new RuntimeException(e);
36
        } catch (IllegalAccessException e) {
37
            throw new RuntimeException(e);
38
        }
39
    }
7 40
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TaxonNodeServiceImpl.java
44 44
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
45 45
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
46 46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
48 47
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
49 48
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
50 49
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
51
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator;
52 50
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
53 51
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
54 52
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
......
92 90
        	childNodes = new ArrayList<TaxonNode>(taxonNode.getChildNodes());
93 91
        }
94 92
        if (sortMode != null){
95
            Comparator<TaxonNode> comparator = null;
96
            if (sortMode.equals(NodeSortMode.NaturalOrder)){
97
                comparator = new TaxonNaturalComparator();
98
            } else if (sortMode.equals(NodeSortMode.AlphabeticalOrder)){
99
            	comparator = new TaxonNodeByNameComparator();
100
            } else if (sortMode.equals(NodeSortMode.RankAndAlphabeticalOrder)){
101
            	comparator = new TaxonNodeByRankAndNameComparator();
102
            }
93
            Comparator<TaxonNode> comparator = sortMode.newComparator();
103 94
        	Collections.sort(childNodes, comparator);
104 95
        }
105 96
        defaultBeanInitializer.initializeAll(childNodes, propertyPaths);

Also available in: Unified diff