ClassCoverage.java
1/*******************************************************************************
2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
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 * $Id: $
12 *******************************************************************************/
13package org.jacoco.core.analysis;
14
15import java.util.Collection;
16
17/**
18 * Coverage data of a single class.
19 *
20 * @author Marc R. Hoffmann
21 * @version $Revision: $
22 */
23public class ClassCoverage extends CoverageNodeImpl {
24
25 private final Collection<MethodCoverage> methods;
26 private final String sourceFileName;
27
28 /**
29 * Creates a class coverage data object with the given parameters.
30 *
31 * @param name
32 * vm name of the class
33 * @param sourceFileName
34 * optional name of the corresponding source file
35 * @param methods
36 * contained methods
37 */
38 public ClassCoverage(final String name, final String sourceFileName,
39 final Collection<MethodCoverage> methods) {
40 super(ElementType.CLASS, name, true);
41 this.sourceFileName = sourceFileName;
42 this.methods = methods;
43 increment(methods);
44 // As class is considered as covered when at least one method is
45 // covered:
46 final boolean covered = methodCounter.getCoveredCount() > 0;
47 this.classCounter = CounterImpl.getInstance(covered);
48 }
49
50 /**
51 * Returns the VM name of the package this class belongs to.
52 *
53 * @return VM name of the package
54 */
55 public String getPackageName() {
56 final int pos = getName().lastIndexOf('/');
57 return pos == -1 ? "" : getName().substring(0, pos);
58 }
59
60 /**
61 * Returns the VM name of the class without the package prefix.
62 *
63 * @return VM name of the class without the package
64 */
65 public String getSimpleName() {
66 final int pos = getName().lastIndexOf('/');
67 return pos == -1 ? getName() : getName().substring(pos + 1);
68 }
69
70 /**
71 * Returns the optional name of the corresponding source file.
72 *
73 * @return name of the corresponding source file
74 */
75 public String getSourceFileName() {
76 return sourceFileName;
77 }
78
79 /**
80 * Returns the methods included in this class.
81 *
82 * @return methods of this class
83 */
84 public Collection<MethodCoverage> getMethods() {
85 return methods;
86 }
87
88}