Revision d5d79aea
Added by Andreas Müller about 8 years ago
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/TaxonNaturalComparator.java | ||
---|---|---|
1 |
package eu.etaxonomy.cdm.model.taxon; |
|
2 |
|
|
3 |
import java.util.Comparator; |
|
4 |
import java.util.Iterator; |
|
5 |
|
|
6 |
public class TaxonNaturalComparator implements Comparator<TaxonNode> { |
|
7 |
Classification classification; |
|
8 |
|
|
9 |
public TaxonNaturalComparator(){ |
|
10 |
super(); |
|
11 |
|
|
12 |
} |
|
13 |
|
|
14 |
@Override |
|
15 |
public int compare(TaxonNode node1, TaxonNode node2) { |
|
16 |
// TaxonNode node1 = null; |
|
17 |
// TaxonNode node2 = null; |
|
18 |
// if (taxon1.getTaxonNodes().size()>0 && taxon2.getTaxonNodes().size()>0){ |
|
19 |
// for (TaxonNode node: taxon1.getTaxonNodes()){ |
|
20 |
// if (node.getClassification().equals(classification)){ |
|
21 |
// node1= node; |
|
22 |
// break; |
|
23 |
// } |
|
24 |
// } |
|
25 |
// for (TaxonNode node: taxon2.getTaxonNodes()){ |
|
26 |
// if (node.getClassification().equals(classification)){ |
|
27 |
// node2= node; |
|
28 |
// break; |
|
29 |
// } |
|
30 |
// } |
|
31 |
// }else{ |
|
32 |
// return 0; |
|
33 |
// } |
|
34 |
if (node1.isAncestor(node2)) { |
|
35 |
return 1; |
|
36 |
} |
|
37 |
if (node2.isAncestor(node1)) { |
|
38 |
return -1; |
|
39 |
} |
|
40 |
if (node1.equals(node2)) { |
|
41 |
return 0; |
|
42 |
} |
|
43 |
|
|
44 |
String[] splitNode1 = node1.treeIndex().split("#"); |
|
45 |
String[] splitNode2 = node2.treeIndex().split("#"); |
|
46 |
if (splitNode1.length < splitNode2.length) { |
|
47 |
return 1; |
|
48 |
} else if (splitNode2.length < splitNode1.length) { |
|
49 |
return -1; |
|
50 |
} |
|
51 |
|
|
52 |
if (node1.getParent().equals(node2.getParent())){ |
|
53 |
return node1.getSortIndex().compareTo(node2.getSortIndex()); |
|
54 |
} |
|
55 |
String lastEqualAncestorTreeIndex = null; |
|
56 |
|
|
57 |
Iterator<TaxonNode> ancestorsNode1 = node1.getAncestors().iterator(); |
|
58 |
Iterator<TaxonNode> ancestorsNode2 = node2.getAncestors().iterator(); |
|
59 |
for (int i = 0; i < splitNode1.length; i++){ |
|
60 |
if (!splitNode1[i].equals(splitNode2[i])){ |
|
61 |
// take the last equal ancestor and compare the sortindex |
|
62 |
if (lastEqualAncestorTreeIndex != null){ |
|
63 |
TaxonNode lastEqualTreeIndexAncestorNode1 = null; |
|
64 |
TaxonNode lastEqualTreeIndexAncestorNode2 = null; |
|
65 |
while (ancestorsNode1.hasNext()){ |
|
66 |
TaxonNode next1 = ancestorsNode1.next(); |
|
67 |
if (next1.treeIndex().equals(lastEqualAncestorTreeIndex)){ |
|
68 |
lastEqualTreeIndexAncestorNode1 = next1; |
|
69 |
} |
|
70 |
} |
|
71 |
while (ancestorsNode2.hasNext()){ |
|
72 |
TaxonNode next2 = ancestorsNode2.next(); |
|
73 |
if (next2.treeIndex().equals(lastEqualAncestorTreeIndex)){ |
|
74 |
lastEqualTreeIndexAncestorNode2 = next2; |
|
75 |
} |
|
76 |
} |
|
77 |
return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex()); |
|
78 |
|
|
79 |
} |
|
80 |
} |
|
81 |
lastEqualAncestorTreeIndex = splitNode1[i]; |
|
82 |
} |
|
83 |
|
|
84 |
return 0; |
|
85 |
} |
|
86 |
|
|
87 |
} |
|
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 |
*/ |
|
9 |
package eu.etaxonomy.cdm.model.taxon; |
|
10 |
|
|
11 |
import java.util.Comparator; |
|
12 |
import java.util.Iterator; |
|
13 |
|
|
14 |
/** |
|
15 |
* Comparator to compare {@link TaxonNode taxon nodes} by its user defined ordering |
|
16 |
* @author k.luther |
|
17 |
*/ |
|
18 |
public class TaxonNaturalComparator implements Comparator<TaxonNode> { |
|
19 |
|
|
20 |
public TaxonNaturalComparator(){ |
|
21 |
super(); |
|
22 |
|
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public int compare(TaxonNode node1, TaxonNode node2) { |
|
27 |
// TaxonNode node1 = null; |
|
28 |
// TaxonNode node2 = null; |
|
29 |
// if (taxon1.getTaxonNodes().size()>0 && taxon2.getTaxonNodes().size()>0){ |
|
30 |
// for (TaxonNode node: taxon1.getTaxonNodes()){ |
|
31 |
// if (node.getClassification().equals(classification)){ |
|
32 |
// node1= node; |
|
33 |
// break; |
|
34 |
// } |
|
35 |
// } |
|
36 |
// for (TaxonNode node: taxon2.getTaxonNodes()){ |
|
37 |
// if (node.getClassification().equals(classification)){ |
|
38 |
// node2= node; |
|
39 |
// break; |
|
40 |
// } |
|
41 |
// } |
|
42 |
// }else{ |
|
43 |
// return 0; |
|
44 |
// } |
|
45 |
if (node1.isAncestor(node2)) { |
|
46 |
return 1; |
|
47 |
} |
|
48 |
if (node2.isAncestor(node1)) { |
|
49 |
return -1; |
|
50 |
} |
|
51 |
if (node1.equals(node2)) { |
|
52 |
return 0; |
|
53 |
} |
|
54 |
|
|
55 |
String[] splitNode1 = node1.treeIndex().split("#"); |
|
56 |
String[] splitNode2 = node2.treeIndex().split("#"); |
|
57 |
if (splitNode1.length < splitNode2.length) { |
|
58 |
return 1; |
|
59 |
} else if (splitNode2.length < splitNode1.length) { |
|
60 |
return -1; |
|
61 |
} |
|
62 |
|
|
63 |
if (node1.getParent().equals(node2.getParent())){ |
|
64 |
return node1.getSortIndex().compareTo(node2.getSortIndex()); |
|
65 |
} |
|
66 |
String lastEqualAncestorTreeIndex = null; |
|
67 |
|
|
68 |
Iterator<TaxonNode> ancestorsNode1 = node1.getAncestors().iterator(); |
|
69 |
Iterator<TaxonNode> ancestorsNode2 = node2.getAncestors().iterator(); |
|
70 |
for (int i = 0; i < splitNode1.length; i++){ |
|
71 |
if (!splitNode1[i].equals(splitNode2[i])){ |
|
72 |
// take the last equal ancestor and compare the sortindex |
|
73 |
if (lastEqualAncestorTreeIndex != null){ |
|
74 |
TaxonNode lastEqualTreeIndexAncestorNode1 = null; |
|
75 |
TaxonNode lastEqualTreeIndexAncestorNode2 = null; |
|
76 |
while (ancestorsNode1.hasNext()){ |
|
77 |
TaxonNode next1 = ancestorsNode1.next(); |
|
78 |
if (next1.treeIndex().equals(lastEqualAncestorTreeIndex)){ |
|
79 |
lastEqualTreeIndexAncestorNode1 = next1; |
|
80 |
} |
|
81 |
} |
|
82 |
while (ancestorsNode2.hasNext()){ |
|
83 |
TaxonNode next2 = ancestorsNode2.next(); |
|
84 |
if (next2.treeIndex().equals(lastEqualAncestorTreeIndex)){ |
|
85 |
lastEqualTreeIndexAncestorNode2 = next2; |
|
86 |
} |
|
87 |
} |
|
88 |
return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex()); |
|
89 |
|
|
90 |
} |
|
91 |
} |
|
92 |
lastEqualAncestorTreeIndex = splitNode1[i]; |
|
93 |
} |
|
94 |
|
|
95 |
return 0; |
|
96 |
} |
|
97 |
|
|
98 |
} |
Also available in: Unified diff
Improve sort order within a homotypic group (new Comparator) #3338