PercentageColumn.java revision 398ee59bebad6835dab57b60157eff16d511709e
1/*******************************************************************************
2 * Copyright (c) 2009, 2015 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.internal.ReportOutputFolder;
26import org.jacoco.report.internal.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 */
34public class PercentageColumn implements IColumnRenderer {
35
36	private final CounterEntity entity;
37
38	private final NumberFormat percentageFormat;
39
40	private final Comparator<ITableItem> comparator;
41
42	/**
43	 * Creates a new column that is based on the {@link ICounter} for the given
44	 * entity.
45	 *
46	 * @param entity
47	 *            counter entity for this column
48	 * @param locale
49	 *            locale for rendering numbers
50	 */
51	public PercentageColumn(final CounterEntity entity, final Locale locale) {
52		this.entity = entity;
53		this.percentageFormat = DecimalFormat.getPercentInstance(locale);
54		comparator = new TableItemComparator(
55				CounterComparator.MISSEDRATIO.on(entity));
56	}
57
58	public boolean init(final List<? extends ITableItem> items,
59			final ICoverageNode total) {
60		return true;
61	}
62
63	public void footer(final HTMLElement td, final ICoverageNode total,
64			final Resources resources, final ReportOutputFolder base)
65			throws IOException {
66		cell(td, total);
67	}
68
69	public void item(final HTMLElement td, final ITableItem item,
70			final Resources resources, final ReportOutputFolder base)
71			throws IOException {
72		cell(td, item.getNode());
73	}
74
75	private void cell(final HTMLElement td, final ICoverageNode node)
76			throws IOException {
77		final ICounter counter = node.getCounter(entity);
78		final int total = counter.getTotalCount();
79		if (total == 0) {
80			td.text("n/a");
81		} else {
82			td.text(percentageFormat.format(counter.getCoveredRatio()));
83		}
84	}
85
86	public Comparator<ITableItem> getComparator() {
87		return comparator;
88	}
89
90}
91