Project

General

Profile

Revision 889c7e91

ID889c7e91874f1f51e76a2b87378d6fd2ef7c323a
Parent aa478e85
Child 2f495573

Added by Katja Luther almost 4 years ago

fix #6045: fixing the NaturalOrderComparator and complete the test

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/TaxonNaturalComparator.java
8 8
*/
9 9
package eu.etaxonomy.cdm.model.taxon;
10 10

  
11
import java.util.ArrayList;
11 12
import java.util.Comparator;
12
import java.util.Iterator;
13
import java.util.List;
13 14

  
14 15
/**
15 16
 * Comparator to compare {@link TaxonNode taxon nodes} by its user defined ordering
......
22 23

  
23 24
	}
24 25

  
25
	@Override
26
	@SuppressWarnings("null")
27
    @Override
26 28
	public int compare(TaxonNode node1, TaxonNode node2) {
27
		if (node1.isAncestor(node2)) {
28
            return 1;
29
	   // System.out.println("compare node 1: "+ node1.getTaxon().getTitleCache() + " - node 2: " + node2.getTaxon().getTitleCache());
30
	    if (node1.equals(node2)) {
31

  
32
	        return 0;
29 33
        }
30
		if (node2.isAncestor(node1)) {
34

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

  
31 37
            return -1;
32 38
        }
33
		if (node1.equals(node2)) {
34
            return 0;
39
		if (node2.isAncestor(node1)) {
40

  
41
            return 1;
35 42
        }
36 43

  
44

  
37 45
		String[] splitNode1 = node1.treeIndex().split("#");
38 46
		String[] splitNode2 = node2.treeIndex().split("#");
39
		if (splitNode1.length < splitNode2.length) {
40
            return 1;
41
        } else if (splitNode2.length < splitNode1.length) {
42
            return -1;
43
        }
47

  
44 48

  
45 49
		if (node1.getParent().equals(node2.getParent())){
46 50
			return node1.getSortIndex().compareTo(node2.getSortIndex());
47 51
		}
48
		String lastEqualAncestorTreeIndex = null;
52
		String lastEqualAncestorTreeIndex = "";
53
		List<TaxonNode> ancestorAndNode= new ArrayList<TaxonNode>();
54
		ancestorAndNode.add(node1);
55
		ancestorAndNode.addAll(node1.getAncestors());
56
		java.util.Collections.sort(ancestorAndNode, new TreeIndexComparator());
57

  
58

  
59
		List<TaxonNode> ancestorAndNode2= new ArrayList<TaxonNode>();
60
        ancestorAndNode2.add(node2);
61
        ancestorAndNode2.addAll(node2.getAncestors());
62
        java.util.Collections.sort(ancestorAndNode2, new TreeIndexComparator());
49 63

  
50
		Iterator<TaxonNode> ancestorsNode1 = node1.getAncestors().iterator();
51
		Iterator<TaxonNode> ancestorsNode2 = node2.getAncestors().iterator();
52 64
		for (int i = 0; i < splitNode1.length; i++){
53 65
			if (!splitNode1[i].equals(splitNode2[i])){
54 66
				// take the last equal ancestor and compare the sortindex
55 67
				if (lastEqualAncestorTreeIndex != null){
56 68
					TaxonNode lastEqualTreeIndexAncestorNode1 = null;
57 69
					TaxonNode lastEqualTreeIndexAncestorNode2 = null;
58
					while (ancestorsNode1.hasNext()){
59
						TaxonNode next1 = ancestorsNode1.next();
60
						String[] split = next1.treeIndex().split("#");
61
						if (split[split.length-1].equals(lastEqualAncestorTreeIndex)){
62
							lastEqualTreeIndexAncestorNode1 = next1;
70
					for (TaxonNode next1 :ancestorAndNode){
71

  
72
						if (next1.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode1[i]+ "#") ){
73
						    lastEqualTreeIndexAncestorNode1 = next1;
63 74
						}
64 75
					}
65
					while (ancestorsNode2.hasNext()){
66
						TaxonNode next2 = ancestorsNode2.next();
67
						String[] split = next2.treeIndex().split("#");
68
						if (split[split.length-1].equals(lastEqualAncestorTreeIndex)){
69
							lastEqualTreeIndexAncestorNode2 = next2;
76
					for (TaxonNode next2 :ancestorAndNode2){
77

  
78
						if (next2.treeIndex().equals(lastEqualAncestorTreeIndex+"#"+splitNode2[i]+ "#")){
79
						    lastEqualTreeIndexAncestorNode2 = next2;
70 80
						}
71 81
					}
72 82
					return lastEqualTreeIndexAncestorNode1.getSortIndex().compareTo(lastEqualTreeIndexAncestorNode2.getSortIndex());
73 83
				}
74 84
			}
75
			lastEqualAncestorTreeIndex = splitNode1[i];
85
			if (!splitNode1[i].equals("")){
86
			    lastEqualAncestorTreeIndex = lastEqualAncestorTreeIndex+"#"+splitNode1[i];
87
			}
76 88
		}
77 89
		return 0;
78 90
	}
91

  
92
	private final class TreeIndexComparator implements Comparator<TaxonNode> {
93
	    @Override
94
	    public int compare(TaxonNode node1,TaxonNode node2)
95
	    {
96
	        return node1.treeIndex().compareTo(node2.treeIndex());
97
	    }
98
	}
79 99
}
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TaxonNodeServiceImplTest.java
531 531
	    System.out.println(nodes.size());
532 532

  
533 533
	}
534
	
534

  
535 535
    @Test
536 536
    public void testCompareNaturalOrder() {
537 537
    	/*
......
556 556
    	pinusPampaName.setGenusOrUninomial("Pinus");
557 557
    	pinusPampaName.setSpecificEpithet("pampa");
558 558
    	Taxon pinusPampa = Taxon.NewInstance(pinusPampaName, null);
559
    	
559

  
560
        BotanicalName abiesBalsameaName = BotanicalName.NewInstance(Rank.SPECIES());
561
        abiesBalsameaName.setGenusOrUninomial("Abies");
562
        abiesBalsameaName.setSpecificEpithet("balsamea");
563
        Taxon abiesBalsamea = Taxon.NewInstance(abiesBalsameaName, null);
564

  
565

  
560 566
    	classification.addChildTaxon(abies, null, null);
561
    	classification.addParentChild(abies, abiesAlba, null, null);
567
    	TaxonNode abiesAlbaNode = classification.addParentChild(abies, abiesAlba, null, null);
562 568
    	classification.addChildTaxon(pinus, null, null);
563 569
    	classification.addParentChild(pinus, pinusPampa, null, null);
570
    	TaxonNode balsameaNode = classification.addParentChild(abies, abiesBalsamea, null, null);
564 571
    	classificationService.save(classification);
565
    	
572

  
566 573
    	TaxonNaturalComparator comparator = new TaxonNaturalComparator();
567 574
    	List<TaxonNode> allNodes = new ArrayList<>(classification.getAllNodes());
568 575
    	Collections.sort(allNodes, comparator);
569
    	//TODO implement assertion for correct order
576
    	for (TaxonNode node: allNodes){
577
    	    System.out.println(node.getTaxon().getTitleCache());
578
    	}
579
    	Assert.assertEquals(allNodes.get(0).getTaxon(), abies );
580
    	Assert.assertEquals(allNodes.get(2).getTaxon(), abiesBalsamea );
581
    	Assert.assertEquals(allNodes.get(1).getTaxon(), abiesAlba );
582

  
583
    	taxonNodeService.moveTaxonNode(balsameaNode, abiesAlbaNode,1);
584
    	classification = classificationService.load(classification.getUuid());
585

  
586
       allNodes = new ArrayList<>(classification.getAllNodes());
587
        Collections.sort(allNodes, comparator);
588
        for (TaxonNode node: allNodes){
589
            System.out.println(node.getTaxon().getTitleCache());
590
        }
591
        Assert.assertEquals(allNodes.get(0).getTaxon(), abies );
592
        Assert.assertEquals(allNodes.get(1).getTaxon(), abiesBalsamea );
593
        Assert.assertEquals(allNodes.get(2).getTaxon(), abiesAlba );
594

  
595

  
570 596
    }
571 597

  
572 598
    /* (non-Javadoc)

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)