BarColumn.java revision d05ad7d4bc65e91b6c6efb45687f7a850d07f02a
1/******************************************************************************* 2 * Copyright (c) 2009, 2011 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.internal.html.HTMLElement; 27import org.jacoco.report.internal.html.resources.Resources; 28 29/** 30 * Column with a graphical bar that represents the total amount of items in with 31 * length, and the coverage ratio with a red/green sections. The implementation 32 * is stateful, instances must not be used in parallel. 33 * 34 * @author Marc R. Hoffmann 35 * @version $qualified.bundle.version$ 36 */ 37public class BarColumn implements IColumnRenderer { 38 39 private static final int WIDTH = 120; 40 41 private final CounterEntity entity; 42 43 private final NumberFormat integerFormat; 44 45 private int max; 46 47 private final Comparator<ITableItem> comparator; 48 49 /** 50 * Creates a new column that is based on the {@link ICounter} for the given 51 * entity. 52 * 53 * @param entity 54 * counter entity for visualization 55 * @param locale 56 * locale for rendering numbers 57 */ 58 public BarColumn(final CounterEntity entity, final Locale locale) { 59 this.entity = entity; 60 this.integerFormat = DecimalFormat.getIntegerInstance(locale); 61 this.comparator = new TableItemComparator(CounterComparator.MISSEDITEMS 62 .reverse().on(entity) 63 .second(CounterComparator.TOTALITEMS.reverse().on(entity))); 64 } 65 66 public boolean init(final List<? extends ITableItem> items, 67 final ICoverageNode total) { 68 this.max = 0; 69 for (final ITableItem item : items) { 70 final int count = item.getNode().getCounter(entity).getTotalCount(); 71 if (count > this.max) { 72 this.max = count; 73 } 74 } 75 return true; 76 } 77 78 public void footer(final HTMLElement td, final ICoverageNode total, 79 final Resources resources, final ReportOutputFolder base) 80 throws IOException { 81 final ICounter counter = total.getCounter(entity); 82 td.text(integerFormat.format(counter.getMissedCount())).text(" of ") 83 .text(integerFormat.format(counter.getTotalCount())); 84 } 85 86 public void item(final HTMLElement td, final ITableItem item, 87 final Resources resources, final ReportOutputFolder base) 88 throws IOException { 89 if (max > 0) { 90 final ICounter counter = item.getNode().getCounter(entity); 91 final int missed = counter.getMissedCount(); 92 bar(td, missed, Resources.REDBAR, resources, base); 93 final int covered = counter.getCoveredCount(); 94 bar(td, covered, Resources.GREENBAR, resources, base); 95 } 96 } 97 98 private void bar(final HTMLElement td, final int count, final String image, 99 final Resources resources, final ReportOutputFolder base) 100 throws IOException { 101 final int width = count * WIDTH / max; 102 if (width > 0) { 103 td.img(resources.getLink(base, image), width, 10, 104 integerFormat.format(count)); 105 } 106 } 107 108 public Comparator<ITableItem> getComparator() { 109 return comparator; 110 } 111 112} 113