Home | History | Annotate | Download | only in analysis
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 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  *******************************************************************************/
     12 package org.jacoco.core.analysis;
     13 
     14 import java.io.Serializable;
     15 import java.util.Comparator;
     16 
     17 import org.jacoco.core.analysis.ICounter.CounterValue;
     18 import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
     19 
     20 /**
     21  * Collection of comparators to compare {@link ICounter} objects by different
     22  * criteria.
     23  */
     24 public class CounterComparator implements Comparator<ICounter>, Serializable {
     25 
     26 	private static final long serialVersionUID = -3777463066252746748L;
     27 
     28 	/**
     29 	 * Compares the absolute number of total items.
     30 	 */
     31 	public static final CounterComparator TOTALITEMS = new CounterComparator(
     32 			CounterValue.TOTALCOUNT);
     33 
     34 	/**
     35 	 * Compares the absolute number of covered items.
     36 	 */
     37 	public static final CounterComparator COVEREDITEMS = new CounterComparator(
     38 			CounterValue.COVEREDCOUNT);
     39 
     40 	/**
     41 	 * Compares the absolute number of missed items.
     42 	 */
     43 	public static final CounterComparator MISSEDITEMS = new CounterComparator(
     44 			CounterValue.MISSEDCOUNT);
     45 
     46 	/**
     47 	 * Compares the ratio of covered items.
     48 	 */
     49 	public static final CounterComparator COVEREDRATIO = new CounterComparator(
     50 			CounterValue.COVEREDRATIO);
     51 
     52 	/**
     53 	 * Compares the ratio of missed items.
     54 	 */
     55 	public static final CounterComparator MISSEDRATIO = new CounterComparator(
     56 			CounterValue.MISSEDRATIO);
     57 
     58 	private final CounterValue value;
     59 	private final boolean reverse;
     60 
     61 	private CounterComparator(final CounterValue value) {
     62 		this(value, false);
     63 	}
     64 
     65 	private CounterComparator(final CounterValue value, final boolean reverse) {
     66 		this.value = value;
     67 		this.reverse = reverse;
     68 	}
     69 
     70 	public int compare(final ICounter c1, final ICounter c2) {
     71 		final int cmp = Double.compare(c1.getValue(value), c2.getValue(value));
     72 		return reverse ? -cmp : cmp;
     73 	}
     74 
     75 	/**
     76 	 * Creates a new version of this comparator that sorts in reverse order.
     77 	 *
     78 	 * @return reverse comparator
     79 	 */
     80 	public CounterComparator reverse() {
     81 		return new CounterComparator(value, !reverse);
     82 	}
     83 
     84 	/**
     85 	 * Creates a new comparator for {@link ICoverageNode} counters of the given
     86 	 * entity based on this counter sorting criteria.
     87 	 *
     88 	 * @param entity
     89 	 *            counter entity to sort on
     90 	 * @return comparator for {@link ICoverageNode} elements
     91 	 */
     92 	public NodeComparator on(final CounterEntity entity) {
     93 		return new NodeComparator(this, entity);
     94 	}
     95 
     96 }
     97