CounterColumn.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.report.html.table;
   13
   14import java.io.IOException;
   15import java.text.DecimalFormat;
   16import java.text.NumberFormat;
   17import java.util.Comparator;
   18import java.util.List;
   19import java.util.Locale;
   20
   21import org.jacoco.core.analysis.CounterComparator;
   22import org.jacoco.core.analysis.ICounter;
   23import org.jacoco.core.analysis.ICoverageNode;
   24import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
   25import org.jacoco.report.ReportOutputFolder;
   26import org.jacoco.report.html.HTMLElement;
   27import org.jacoco.report.html.resources.Resources;
   28
   29/**
   30 * Column that prints the counter values of entities for each item and a summary
   31 * in the footer. If the total number of items is zero, no column is emitted at
   32 * all. The implementation is stateful, instances must not be used in parallel.
   33 * 
   34 * @author Marc R. Hoffmann
   35 * @version 0.4.1.20101007204400
   36 */
   37public abstract class CounterColumn implements IColumnRenderer {
   38
   39    /**
   40     * Creates a new column that shows the total count for the given entity.
   41     * 
   42     * @param entity
   43     *            counter entity for this column
   44     * @param locale
   45     *            locale for rendering numbers
   46     * @return column instance
   47     */
   48    public static CounterColumn newTotal(final CounterEntity entity,
   49            final Locale locale) {
   50        return new CounterColumn(entity, locale, CounterComparator.TOTALITEMS
   51                .reverse().on(entity)) {
   52            @Override
   53            protected int getValue(final ICounter counter) {
   54                return counter.getTotalCount();
   55            }
   56        };
   57    }
   58
   59    /**
   60     * Creates a new column that shows the missed count for the given entity.
   61     * 
   62     * @param entity
   63     *            counter entity for this column
   64     * @param locale
   65     *            locale for rendering numbers
   66     * @return column instance
   67     */
   68    public static CounterColumn newMissed(final CounterEntity entity,
   69            final Locale locale) {
   70        return new CounterColumn(entity, locale, CounterComparator.MISSEDITEMS
   71                .reverse().on(entity)) {
   72            @Override
   73            protected int getValue(final ICounter counter) {
   74                return counter.getMissedCount();
   75            }
   76        };
   77    }
   78
   79    /**
   80     * Creates a new column that shows the covered count for the given entity.
   81     * 
   82     * @param entity
   83     *            counter entity for this column
   84     * @param locale
   85     *            locale for rendering numbers
   86     * @return column instance
   87     */
   88    public static CounterColumn newCovered(final CounterEntity entity,
   89            final Locale locale) {
   90        return new CounterColumn(entity, locale, CounterComparator.COVEREDITEMS
   91                .reverse().on(entity)) {
   92            @Override
   93            protected int getValue(final ICounter counter) {
   94                return counter.getCoveredCount();
   95            }
   96        };
   97    }
   98
   99    private final CounterEntity entity;
  100
  101    private final NumberFormat integerFormat;
  102
  103    private final Comparator<ITableItem> comparator;
  104
  105    /**
  106     * Creates a new column that is based on the {@link ICounter} for the given
  107     * entity.
  108     * 
  109     * @param entity
  110     *            counter entity for this column
  111     * @param locale
  112     *            locale for rendering numbers
  113     * @param comparator
  114     *            comparator for the nodes of this column
  115     */
  116    protected CounterColumn(final CounterEntity entity, final Locale locale,
  117            final Comparator<ICoverageNode> comparator) {
  118        this.entity = entity;
  119        this.integerFormat = DecimalFormat.getIntegerInstance(locale);
  120        this.comparator = new TableItemComparator(comparator);
  121    }
  122
  123    public boolean init(final List<? extends ITableItem> items,
  124            final ICoverageNode total) {
  125        for (final ITableItem i : items) {
  126            if (i.getNode().getCounter(entity).getTotalCount() > 0) {
  127                return true;
  128            }
  129        }
  130        return false;
  131    }
  132
  133    public void footer(final HTMLElement td, final ICoverageNode total,
  134            final Resources resources, final ReportOutputFolder base)
  135            throws IOException {
  136        cell(td, total);
  137    }
  138
  139    public void item(final HTMLElement td, final ITableItem item,
  140            final Resources resources, final ReportOutputFolder base)
  141            throws IOException {
  142        cell(td, item.getNode());
  143    }
  144
  145    private void cell(final HTMLElement td, final ICoverageNode node)
  146            throws IOException {
  147        final int value = getValue(node.getCounter(entity));
  148        td.text(integerFormat.format(value));
  149    }
  150
  151    public Comparator<ITableItem> getComparator() {
  152        return comparator;
  153    }
  154
  155    /**
  156     * Retrieves the respective value from the counter.
  157     * 
  158     * @param counter
  159     *            counter object
  160     * @return value of interest
  161     */
  162    protected abstract int getValue(ICounter counter);
  163
  164}