CoverageBuilder.java revision 1571d7b17928964004d337caea8b3f0cc7660460
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.core.analysis; 13 14import java.util.Collection; 15import java.util.Collections; 16import java.util.HashMap; 17import java.util.Map; 18 19import org.jacoco.core.data.ExecutionDataStore; 20 21/** 22 * Builder for hierarchical {@link ICoverageNode} structures based on execution 23 * and structure information. The builder is constructed for a given 24 * {@link ExecutionDataStore} and then feed with class structure information 25 * through its {@link ICoverageVisitor} interface. Afterwards the collected data 26 * can be obtained with {@link #getClasses()}, {@link #getSourceFiles()} or 27 * {@link #getBundle(String)}. 28 * 29 * @author Marc R. Hoffmann 30 * @version $qualified.bundle.version$ 31 */ 32public class CoverageBuilder implements ICoverageVisitor { 33 34 private final Map<String, ClassCoverage> classes; 35 36 private final Map<String, SourceFileCoverage> sourcefiles; 37 38 /** 39 * Create a new builder. 40 * 41 */ 42 public CoverageBuilder() { 43 this.classes = new HashMap<String, ClassCoverage>(); 44 this.sourcefiles = new HashMap<String, SourceFileCoverage>(); 45 } 46 47 /** 48 * Returns all class nodes currently contained in this builder. 49 * 50 * @return all class nodes 51 */ 52 public Collection<ClassCoverage> getClasses() { 53 return Collections.unmodifiableCollection(classes.values()); 54 } 55 56 /** 57 * Returns all source file nodes currently contained in this builder. 58 * 59 * @return all source file nodes 60 */ 61 public Collection<SourceFileCoverage> getSourceFiles() { 62 return Collections.unmodifiableCollection(sourcefiles.values()); 63 } 64 65 /** 66 * Creates a bundle from all nodes currently contained in this bundle. 67 * 68 * @param name 69 * Name of the bundle 70 * @return bundle containing all classes and source files 71 */ 72 public BundleCoverage getBundle(final String name) { 73 return new BundleCoverage(name, classes.values(), sourcefiles.values()); 74 } 75 76 // === IStructureVisitor === 77 78 public void visitCoverage(final ClassCoverage coverage) { 79 // Only consider classes that actually contain code: 80 if (coverage.getInstructionCounter().getTotalCount() > 0) { 81 final String name = coverage.getName(); 82 if (classes.put(name, coverage) != null) { 83 throw new IllegalStateException( 84 "Duplicate class name in same group: " + name); 85 } 86 final String source = coverage.getSourceFileName(); 87 if (source != null) { 88 final SourceFileCoverage sourceFile = getSourceFile(source, 89 coverage.getPackageName()); 90 sourceFile.increment(coverage); 91 } 92 } 93 } 94 95 private SourceFileCoverage getSourceFile(final String filename, 96 final String packagename) { 97 final String key = packagename + '/' + filename; 98 SourceFileCoverage sourcefile = sourcefiles.get(key); 99 if (sourcefile == null) { 100 sourcefile = new SourceFileCoverage(filename, packagename); 101 sourcefiles.put(key, sourcefile); 102 } 103 return sourcefile; 104 } 105 106} 107