cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / compare / taxon / TaxonNodeNaturalComparator.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 */
9 package eu.etaxonomy.cdm.compare.taxon;
10
11 import java.util.ArrayList;
12 import java.util.Comparator;
13 import java.util.List;
14
15 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
16
17 /**
18 * Comparator to compare {@link TaxonNode taxon nodes} by its user defined ordering
19 *
20 * @author k.luther
21 */
22 public class TaxonNodeNaturalComparator implements Comparator<TaxonNode> {
23
24 @SuppressWarnings("null")
25 @Override
26 public int compare(TaxonNode node1, TaxonNode node2) {
27 // System.out.println("compare node 1: "+ node1.getTaxon().getTitleCache() + " - node 2: " + node2.getTaxon().getTitleCache());
28 if (node1.equals(node2)) {
29 return 0;
30 }
31 if (node1.treeIndex() == null){
32 if (node2.treeIndex() == null){
33 return 0;
34 }else{
35 return 1;
36 }
37 }else if (node2.treeIndex() == null){
38 return -1;
39 }
40
41 if (node1.isAncestor(node2)) {
42 return -1;
43 }
44 if (node2.isAncestor(node1)) {
45 return 1;
46 }
47
48 String[] splitNode1 = node1.treeIndex().split("#");
49 String[] splitNode2 = node2.treeIndex().split("#");
50
51 if (node1.getParent().equals(node2.getParent())){
52 return node1.getSortIndex().compareTo(node2.getSortIndex());
53 }
54 String lastEqualAncestorTreeIndex = "";
55 List<TaxonNode> ancestorAndNode1= new ArrayList<>();
56 ancestorAndNode1.add(node1);
57 ancestorAndNode1.addAll(node1.getAncestorList());
58 java.util.Collections.sort(ancestorAndNode1, new TreeIndexComparator());
59
60
61 List<TaxonNode> ancestorAndNode2= new ArrayList<>();
62 ancestorAndNode2.add(node2);
63 ancestorAndNode2.addAll(node2.getAncestorList());
64 java.util.Collections.sort(ancestorAndNode2, new TreeIndexComparator());
65
66 for (int i = 0; i < splitNode1.length; i++){
67 if (!splitNode1[i].equals(splitNode2[i])){
68 // take the last equal ancestor and compare the sortindex
69 if (lastEqualAncestorTreeIndex != null){
70 TaxonNode lastEqualTreeIndexAncestorNode1 = null;
71 TaxonNode lastEqualTreeIndexAncestorNode2 = null;
72 for (TaxonNode next1 :ancestorAndNode1){
73 if (next1.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode1[i]+ "#") ){
74 lastEqualTreeIndexAncestorNode1 = next1;
75 }
76 }
77 for (TaxonNode next2 :ancestorAndNode2){
78
79 if (next2.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode2[i]+ "#")){
80 lastEqualTreeIndexAncestorNode2 = next2;
81 }
82 }
83 return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex());
84 }
85 }
86 if (!splitNode1[i].equals("")){
87 lastEqualAncestorTreeIndex = lastEqualAncestorTreeIndex+"#"+splitNode1[i];
88 }
89 }
90 return 0;
91 }
92
93 private final class TreeIndexComparator implements Comparator<TaxonNode> {
94 @Override
95 public int compare(TaxonNode node1,TaxonNode node2){
96 return node1.treeIndex().compareTo(node2.treeIndex());
97 }
98 }
99 }