1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 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 *******************************************************************************/ 12 package org.jacoco.core.internal.analysis; 13 14 import java.util.ArrayList; 15 import java.util.Collection; 16 import java.util.Collections; 17 import java.util.HashMap; 18 import java.util.HashSet; 19 import java.util.Map; 20 import java.util.Set; 21 22 import org.jacoco.core.analysis.CoverageNodeImpl; 23 import org.jacoco.core.analysis.IBundleCoverage; 24 import org.jacoco.core.analysis.IClassCoverage; 25 import org.jacoco.core.analysis.IPackageCoverage; 26 import org.jacoco.core.analysis.ISourceFileCoverage; 27 28 /** 29 * Implementation of {@link IBundleCoverage}. 30 */ 31 public class BundleCoverageImpl extends CoverageNodeImpl implements 32 IBundleCoverage { 33 34 private final Collection<IPackageCoverage> packages; 35 36 /** 37 * Creates a new instance of a bundle with the given name. 38 * 39 * @param name 40 * name of this bundle 41 * @param packages 42 * collection of all packages contained in this bundle 43 */ BundleCoverageImpl(final String name, final Collection<IPackageCoverage> packages)44 public BundleCoverageImpl(final String name, 45 final Collection<IPackageCoverage> packages) { 46 super(ElementType.BUNDLE, name); 47 this.packages = packages; 48 increment(packages); 49 } 50 51 /** 52 * Creates a new instance of a bundle with the given name. The packages are 53 * calculated from the given classes and source files. 54 * 55 * @param name 56 * name of this bundle 57 * @param classes 58 * all classes in this bundle 59 * @param sourcefiles 60 * all source files in this bundle 61 */ BundleCoverageImpl(final String name, final Collection<IClassCoverage> classes, final Collection<ISourceFileCoverage> sourcefiles)62 public BundleCoverageImpl(final String name, 63 final Collection<IClassCoverage> classes, 64 final Collection<ISourceFileCoverage> sourcefiles) { 65 this(name, groupByPackage(classes, sourcefiles)); 66 } 67 groupByPackage( final Collection<IClassCoverage> classes, final Collection<ISourceFileCoverage> sourcefiles)68 private static Collection<IPackageCoverage> groupByPackage( 69 final Collection<IClassCoverage> classes, 70 final Collection<ISourceFileCoverage> sourcefiles) { 71 final Map<String, Collection<IClassCoverage>> classesByPackage = new HashMap<String, Collection<IClassCoverage>>(); 72 for (final IClassCoverage c : classes) { 73 addByName(classesByPackage, c.getPackageName(), c); 74 } 75 76 final Map<String, Collection<ISourceFileCoverage>> sourceFilesByPackage = new HashMap<String, Collection<ISourceFileCoverage>>(); 77 for (final ISourceFileCoverage s : sourcefiles) { 78 addByName(sourceFilesByPackage, s.getPackageName(), s); 79 } 80 81 final Set<String> packageNames = new HashSet<String>(); 82 packageNames.addAll(classesByPackage.keySet()); 83 packageNames.addAll(sourceFilesByPackage.keySet()); 84 85 final Collection<IPackageCoverage> result = new ArrayList<IPackageCoverage>(); 86 for (final String name : packageNames) { 87 Collection<IClassCoverage> c = classesByPackage.get(name); 88 if (c == null) { 89 c = Collections.emptyList(); 90 } 91 Collection<ISourceFileCoverage> s = sourceFilesByPackage.get(name); 92 if (s == null) { 93 s = Collections.emptyList(); 94 } 95 result.add(new PackageCoverageImpl(name, c, s)); 96 } 97 return result; 98 } 99 addByName(final Map<String, Collection<T>> map, final String name, final T value)100 private static <T> void addByName(final Map<String, Collection<T>> map, 101 final String name, final T value) { 102 Collection<T> list = map.get(name); 103 if (list == null) { 104 list = new ArrayList<T>(); 105 map.put(name, list); 106 } 107 list.add(value); 108 } 109 110 // === IBundleCoverage implementation === 111 getPackages()112 public Collection<IPackageCoverage> getPackages() { 113 return packages; 114 } 115 116 } 117