blob: 91661b2c7b7c76dffc21aca65850ee35ecb32634 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*******************************************************************************
* Copyright (c) 2009, 2011 Mountainminds GmbH & Co. KG and Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.analysis;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
/**
* Collection of comparators to compare {@link ICoverageNode} objects by
* different criteria.
*
* @see CounterComparator#on(ICoverageNode.CounterEntity)
* @author Marc R. Hoffmann
* @version $qualified.bundle.version$
*/
public class NodeComparator implements Comparator<ICoverageNode> {
private final Comparator<ICounter> counterComparator;
private final CounterEntity entity;
NodeComparator(final Comparator<ICounter> counterComparator,
final CounterEntity entity) {
this.counterComparator = counterComparator;
this.entity = entity;
}
/**
* Creates a new composite comparator with a second search criterion.
*
* @param second
* second criterion comparator
*
* @return composite comparator
*/
public NodeComparator second(final Comparator<ICoverageNode> second) {
final Comparator<ICoverageNode> first = this;
return new NodeComparator(null, null) {
@Override
public int compare(final ICoverageNode o1, final ICoverageNode o2) {
final int result = first.compare(o1, o2);
return result == 0 ? second.compare(o1, o2) : result;
}
};
}
/**
* Returns a sorted copy of the given collection of {@link ICoverageNode}
* elements.
*
* @param <T>
* actual type of the elements
* @param summaries
* collection to create a copy of
* @return sorted copy
*/
public <T extends ICoverageNode> List<T> sort(final Collection<T> summaries) {
final List<T> result = new ArrayList<T>(summaries);
Collections.sort(result, this);
return result;
}
public int compare(final ICoverageNode n1, final ICoverageNode n2) {
final ICounter c1 = n1.getCounter(entity);
final ICounter c2 = n2.getCounter(entity);
return counterComparator.compare(c1, c2);
}
}
|