PercentageColumn.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.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 coverage percentage for each item and the total
29 * percentage in the footer. The implementation is stateless, instances might be
30 * used in parallel.
31 *
32 * @author Marc R. Hoffmann
33 * @version $Revision: $
34 */
35public class PercentageColumn implements ICoverageTableColumn {
36
37 private final String header;
38
39 private final CounterEntity entity;
40
41 private final NumberFormat percentageFormat = DecimalFormat
42 .getPercentInstance();
43
44 /**
45 * Creates a new column that is based on the {@link ICounter} for the given
46 * entity.
47 *
48 * @param header
49 * column header caption
50 * @param entity
51 * counter entity for this column
52 */
53 public PercentageColumn(final String header, final CounterEntity entity) {
54 this.header = header;
55 this.entity = entity;
56 }
57
58 public void init(final List<ICoverageTableItem> items,
59 final ICoverageNode total) {
60 }
61
62 public void header(final HTMLElement tr, final Resources resources,
63 final ReportOutputFolder base) throws IOException {
64 tr.td().text(header);
65 }
66
67 public void footer(final HTMLElement tr, final ICoverageNode total,
68 final Resources resources, final ReportOutputFolder base)
69 throws IOException {
70 cell(tr, total);
71 }
72
73 public void item(final HTMLElement tr, final ICoverageTableItem item,
74 final Resources resources, final ReportOutputFolder base)
75 throws IOException {
76 cell(tr, item.getNode());
77 }
78
79 private void cell(final HTMLElement tr, final ICoverageNode node)
80 throws IOException {
81 final ICounter counter = node.getCounter(entity);
82 final int total = counter.getTotalCount();
83 final HTMLElement td = tr.td(Styles.CTR2);
84 if (total == 0) {
85 td.text("n/a");
86 } else {
87 td.text(percentageFormat.format(counter.getCoveredRatio()));
88 }
89 }
90
91}