CounterComparator.java

    1/*******************************************************************************
    2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
    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 not covered items.
   48     */
   49    public static final CounterComparator NOTCOVEREDITEMS = new CounterComparator() {
   50        public int compare(final ICounter c1, final ICounter c2) {
   51            return c1.getNotCoveredCount() - c2.getNotCoveredCount();
   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 not covered items.
   66     */
   67    public static final CounterComparator NOTCOVEREDRATIO = new CounterComparator() {
   68        public int compare(final ICounter c1, final ICounter c2) {
   69            return Double.compare(c1.getNotCoveredRatio(), c2
   70                    .getNotCoveredRatio());
   71        }
   72    };
   73
   74    /**
   75     * Creates a new version of this comparator that sorts in reverse order.
   76     * 
   77     * @return reverse comparator
   78     */
   79    public CounterComparator reverse() {
   80        final CounterComparator original = this;
   81        return new CounterComparator() {
   82            public int compare(final ICounter o1, final ICounter o2) {
   83                return original.compare(o2, o1);
   84            }
   85        };
   86    }
   87
   88    /**
   89     * Creates a new comparator for {@link ICoverageNode} counters of the given
   90     * entity based on this counter sorting criteria.
   91     * 
   92     * @param entity
   93     *            counter entity to sort on
   94     * @return comparator for {@link ICoverageNode} elements
   95     */
   96    public NodeComparator on(final CounterEntity entity) {
   97        return new NodeComparator(this, entity);
   98    }
   99
  100}