PercentageColumn.java revision bb18678e1149389693c6051e9c3e5b8e6a5cd164
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.internal.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.internal.html.resources.Resources;
28
29/**
30 * Column that prints the coverage percentage for each item and the total
31 * percentage in the footer. The implementation is stateless, instances might be
32 * used in parallel.
33 *
34 * @author Marc R. Hoffmann
35 * @version $qualified.bundle.version$
36 */
37public class PercentageColumn implements IColumnRenderer {
38
39	private final CounterEntity entity;
40
41	private final NumberFormat percentageFormat;
42
43	private final Comparator<ITableItem> comparator;
44
45	/**
46	 * Creates a new column that is based on the {@link ICounter} for the given
47	 * entity.
48	 *
49	 * @param entity
50	 *            counter entity for this column
51	 * @param locale
52	 *            locale for rendering numbers
53	 */
54	public PercentageColumn(final CounterEntity entity, final Locale locale) {
55		this.entity = entity;
56		this.percentageFormat = DecimalFormat.getPercentInstance(locale);
57		comparator = new TableItemComparator(
58				CounterComparator.MISSEDRATIO.on(entity));
59	}
60
61	public boolean init(final List<? extends ITableItem> items,
62			final ICoverageNode total) {
63		return true;
64	}
65
66	public void footer(final HTMLElement td, final ICoverageNode total,
67			final Resources resources, final ReportOutputFolder base)
68			throws IOException {
69		cell(td, total);
70	}
71
72	public void item(final HTMLElement td, final ITableItem item,
73			final Resources resources, final ReportOutputFolder base)
74			throws IOException {
75		cell(td, item.getNode());
76	}
77
78	private void cell(final HTMLElement td, final ICoverageNode node)
79			throws IOException {
80		final ICounter counter = node.getCounter(entity);
81		final int total = counter.getTotalCount();
82		if (total == 0) {
83			td.text("n/a");
84		} else {
85			td.text(percentageFormat.format(counter.getCoveredRatio()));
86		}
87	}
88
89	public Comparator<ITableItem> getComparator() {
90		return comparator;
91	}
92
93}
94