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 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.report.html;
   14
   15import java.io.IOException;
   16import java.text.DecimalFormat;
   17import java.text.NumberFormat;
   18import java.util.List;
   19
   20import org.jacoco.core.analysis.ICounter;
   21import org.jacoco.core.analysis.ICoverageNode;
   22import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
   23import org.jacoco.report.ReportOutputFolder;
   24import org.jacoco.report.html.resources.Resources;
   25import org.jacoco.report.html.resources.Styles;
   26
   27/**
   28 * Column that prints the number of missed entities and the total number for
   29 * each item and a summary in the footer. If the total number of items is zero,
   30 * no column is emitted at all. The implementation is stateful, instances must
   31 * not be used in parallel.
   32 * 
   33 * @author Marc R. Hoffmann
   34 * @version $Revision: $
   35 */
   36public class CounterColumn implements ICoverageTableColumn {
   37
   38    private final String header;
   39
   40    private final CounterEntity entity;
   41
   42    private boolean visible;
   43
   44    private final NumberFormat integerFormat = DecimalFormat
   45            .getIntegerInstance();
   46
   47    /**
   48     * Creates a new column that is based on the {@link ICounter} for the given
   49     * entity.
   50     * 
   51     * @param header
   52     *            column header caption
   53     * @param entity
   54     *            counter entity for this column
   55     */
   56    public CounterColumn(final String header, final CounterEntity entity) {
   57        this.header = header;
   58        this.entity = entity;
   59    }
   60
   61    public void init(final List<ICoverageTableItem> items,
   62            final ICoverageNode total) {
   63        for (final ICoverageTableItem i : items) {
   64            if (i.getNode().getCounter(entity).getTotalCount() > 0) {
   65                visible = true;
   66                return;
   67            }
   68        }
   69        visible = false;
   70    }
   71
   72    public void header(final HTMLElement tr, final Resources resources,
   73            final ReportOutputFolder base) throws IOException {
   74        if (visible) {
   75            tr.td(Styles.CTR2, 3).text(header);
   76        }
   77    }
   78
   79    public void footer(final HTMLElement tr, final ICoverageNode total,
   80            final Resources resources, final ReportOutputFolder base)
   81            throws IOException {
   82        cell(tr, total);
   83    }
   84
   85    public void item(final HTMLElement tr, final ICoverageTableItem item,
   86            final Resources resources, final ReportOutputFolder base)
   87            throws IOException {
   88        cell(tr, item.getNode());
   89    }
   90
   91    private void cell(final HTMLElement tr, final ICoverageNode node)
   92            throws IOException {
   93        if (visible) {
   94            tr.td(); // extra column to allow alignment to the right
   95            final ICounter c = node.getCounter(entity);
   96            tr.td(Styles.CTR1).text(
   97                    integerFormat.format(c.getMissedCount())).text(" / ");
   98            tr.td(Styles.CTR2).text(integerFormat.format(c.getTotalCount()));
   99        }
  100    }
  101
  102}