• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Marc R. Hoffmann - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.core.analysis;
14 
15 import java.util.Collection;
16 
17 import org.jacoco.core.internal.analysis.CounterImpl;
18 
19 /**
20  * Base implementation for coverage data nodes.
21  */
22 public class CoverageNodeImpl implements ICoverageNode {
23 
24 	private final ElementType elementType;
25 
26 	private final String name;
27 
28 	/** Counter for branches. */
29 	protected CounterImpl branchCounter;
30 
31 	/** Counter for instructions. */
32 	protected CounterImpl instructionCounter;
33 
34 	/** Counter for lines */
35 	protected CounterImpl lineCounter;
36 
37 	/** Counter for complexity. */
38 	protected CounterImpl complexityCounter;
39 
40 	/** Counter for methods. */
41 	protected CounterImpl methodCounter;
42 
43 	/** Counter for classes. */
44 	protected CounterImpl classCounter;
45 
46 	/**
47 	 * Creates a new coverage data node.
48 	 *
49 	 * @param elementType
50 	 *            type of the element represented by this instance
51 	 * @param name
52 	 *            name of this node
53 	 */
CoverageNodeImpl(final ElementType elementType, final String name)54 	public CoverageNodeImpl(final ElementType elementType, final String name) {
55 		this.elementType = elementType;
56 		this.name = name;
57 		this.branchCounter = CounterImpl.COUNTER_0_0;
58 		this.instructionCounter = CounterImpl.COUNTER_0_0;
59 		this.complexityCounter = CounterImpl.COUNTER_0_0;
60 		this.methodCounter = CounterImpl.COUNTER_0_0;
61 		this.classCounter = CounterImpl.COUNTER_0_0;
62 		this.lineCounter = CounterImpl.COUNTER_0_0;
63 	}
64 
65 	/**
66 	 * Increments the counters by the values given by another element.
67 	 *
68 	 * @param child
69 	 *            counters to add
70 	 */
increment(final ICoverageNode child)71 	public void increment(final ICoverageNode child) {
72 		instructionCounter = instructionCounter
73 				.increment(child.getInstructionCounter());
74 		branchCounter = branchCounter.increment(child.getBranchCounter());
75 		lineCounter = lineCounter.increment(child.getLineCounter());
76 		complexityCounter = complexityCounter
77 				.increment(child.getComplexityCounter());
78 		methodCounter = methodCounter.increment(child.getMethodCounter());
79 		classCounter = classCounter.increment(child.getClassCounter());
80 	}
81 
82 	/**
83 	 * Increments the counters by the values given by the collection of
84 	 * elements.
85 	 *
86 	 * @param children
87 	 *            list of nodes, which counters will be added to this node
88 	 */
increment(final Collection<? extends ICoverageNode> children)89 	public void increment(final Collection<? extends ICoverageNode> children) {
90 		for (final ICoverageNode child : children) {
91 			increment(child);
92 		}
93 	}
94 
95 	// === ICoverageDataNode ===
96 
getElementType()97 	public ElementType getElementType() {
98 		return elementType;
99 	}
100 
getName()101 	public String getName() {
102 		return name;
103 	}
104 
getInstructionCounter()105 	public ICounter getInstructionCounter() {
106 		return instructionCounter;
107 	}
108 
getBranchCounter()109 	public ICounter getBranchCounter() {
110 		return branchCounter;
111 	}
112 
getLineCounter()113 	public ICounter getLineCounter() {
114 		return lineCounter;
115 	}
116 
getComplexityCounter()117 	public ICounter getComplexityCounter() {
118 		return complexityCounter;
119 	}
120 
getMethodCounter()121 	public ICounter getMethodCounter() {
122 		return methodCounter;
123 	}
124 
getClassCounter()125 	public ICounter getClassCounter() {
126 		return classCounter;
127 	}
128 
getCounter(final CounterEntity entity)129 	public ICounter getCounter(final CounterEntity entity) {
130 		switch (entity) {
131 		case INSTRUCTION:
132 			return getInstructionCounter();
133 		case BRANCH:
134 			return getBranchCounter();
135 		case LINE:
136 			return getLineCounter();
137 		case COMPLEXITY:
138 			return getComplexityCounter();
139 		case METHOD:
140 			return getMethodCounter();
141 		case CLASS:
142 			return getClassCounter();
143 		}
144 		throw new AssertionError(entity);
145 	}
146 
containsCode()147 	public boolean containsCode() {
148 		return getInstructionCounter().getTotalCount() != 0;
149 	}
150 
getPlainCopy()151 	public ICoverageNode getPlainCopy() {
152 		final CoverageNodeImpl copy = new CoverageNodeImpl(elementType, name);
153 		copy.instructionCounter = CounterImpl.getInstance(instructionCounter);
154 		copy.branchCounter = CounterImpl.getInstance(branchCounter);
155 		copy.lineCounter = CounterImpl.getInstance(lineCounter);
156 		copy.complexityCounter = CounterImpl.getInstance(complexityCounter);
157 		copy.methodCounter = CounterImpl.getInstance(methodCounter);
158 		copy.classCounter = CounterImpl.getInstance(classCounter);
159 		return copy;
160 	}
161 
162 	@Override
toString()163 	public String toString() {
164 		final StringBuilder sb = new StringBuilder();
165 		sb.append(name).append(" [").append(elementType).append("]");
166 		return sb.toString();
167 	}
168 
169 }
170