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 * $Id: $
12 *******************************************************************************/
13package org.jacoco.core.analysis;
14
15import java.util.Comparator;
16
17import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
18
19/**
20 * Collection of comparators to compare {@link ICounter} objects by different
21 * criteria.
22 *
23 * @author Marc R. Hoffmann
24 * @version $Revision: $
25 */
26public abstract class CounterComparator implements Comparator<ICounter> {
27
28 /**
29 * Compares the absolute number of total items.
30 */
31 public static final CounterComparator TOTALITEMS = new CounterComparator() {
32 public int compare(final ICounter c1, final ICounter c2) {
33 return c1.getTotalCount() - c2.getTotalCount();
34 }
35 };
36
37 /**
38 * Compares the absolute number of covered items.
39 */
40 public static final CounterComparator COVEREDITEMS = new CounterComparator() {
41 public int compare(final ICounter c1, final ICounter c2) {
42 return c1.getCoveredCount() - c2.getCoveredCount();
43 }
44 };
45
46 /**
47 * Compares the absolute number of missed items.
48 */
49 public static final CounterComparator MISSEDITEMS = new CounterComparator() {
50 public int compare(final ICounter c1, final ICounter c2) {
51 return c1.getMissedCount() - c2.getMissedCount();
52 }
53 };
54
55 /**
56 * Compares the ratio of covered items.
57 */
58 public static final CounterComparator COVEREDRATIO = new CounterComparator() {
59 public int compare(final ICounter c1, final ICounter c2) {
60 return Double.compare(c1.getCoveredRatio(), c2.getCoveredRatio());
61 }
62 };
63
64 /**
65 * Compares the ratio of missed items.
66 */
67 public static final CounterComparator MISSEDRATIO = new CounterComparator() {
68 public int compare(final ICounter c1, final ICounter c2) {
69 return Double.compare(c1.getMissedRatio(), c2.getMissedRatio());
70 }
71 };
72
73 /**
74 * Creates a new version of this comparator that sorts in reverse order.
75 *
76 * @return reverse comparator
77 */
78 public CounterComparator reverse() {
79 final CounterComparator original = this;
80 return new CounterComparator() {
81 public int compare(final ICounter o1, final ICounter o2) {
82 return original.compare(o2, o1);
83 }
84 };
85 }
86
87 /**
88 * Creates a new comparator for {@link ICoverageNode} counters of the given
89 * entity based on this counter sorting criteria.
90 *
91 * @param entity
92 * counter entity to sort on
93 * @return comparator for {@link ICoverageNode} elements
94 */
95 public NodeComparator on(final CounterEntity entity) {
96 return new NodeComparator(this, entity);
97 }
98
99}