CounterComparator.java
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.core.analysis;
13
14import java.util.Comparator;
15
16import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
17
18/**
19 * Collection of comparators to compare {@link ICounter} objects by different
20 * criteria.
21 *
22 * @author Marc R. Hoffmann
23 * @version 0.4.1.20101007204400
24 */
25public abstract class CounterComparator implements Comparator<ICounter> {
26
27 /**
28 * Compares the absolute number of total items.
29 */
30 public static final CounterComparator TOTALITEMS = new CounterComparator() {
31 public int compare(final ICounter c1, final ICounter c2) {
32 return c1.getTotalCount() - c2.getTotalCount();
33 }
34 };
35
36 /**
37 * Compares the absolute number of covered items.
38 */
39 public static final CounterComparator COVEREDITEMS = new CounterComparator() {
40 public int compare(final ICounter c1, final ICounter c2) {
41 return c1.getCoveredCount() - c2.getCoveredCount();
42 }
43 };
44
45 /**
46 * Compares the absolute number of missed items.
47 */
48 public static final CounterComparator MISSEDITEMS = new CounterComparator() {
49 public int compare(final ICounter c1, final ICounter c2) {
50 return c1.getMissedCount() - c2.getMissedCount();
51 }
52 };
53
54 /**
55 * Compares the ratio of covered items.
56 */
57 public static final CounterComparator COVEREDRATIO = new CounterComparator() {
58 public int compare(final ICounter c1, final ICounter c2) {
59 return Double.compare(c1.getCoveredRatio(), c2.getCoveredRatio());
60 }
61 };
62
63 /**
64 * Compares the ratio of missed items.
65 */
66 public static final CounterComparator MISSEDRATIO = new CounterComparator() {
67 public int compare(final ICounter c1, final ICounter c2) {
68 return Double.compare(c1.getMissedRatio(), c2.getMissedRatio());
69 }
70 };
71
72 /**
73 * Creates a new version of this comparator that sorts in reverse order.
74 *
75 * @return reverse comparator
76 */
77 public CounterComparator reverse() {
78 final CounterComparator original = this;
79 return new CounterComparator() {
80 public int compare(final ICounter o1, final ICounter o2) {
81 return original.compare(o2, o1);
82 }
83 };
84 }
85
86 /**
87 * Creates a new comparator for {@link ICoverageNode} counters of the given
88 * entity based on this counter sorting criteria.
89 *
90 * @param entity
91 * counter entity to sort on
92 * @return comparator for {@link ICoverageNode} elements
93 */
94 public NodeComparator on(final CounterEntity entity) {
95 return new NodeComparator(this, entity);
96 }
97
98}