Home | History | Annotate | Download | only in analysis
      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.analysis;
     13 
     14 import java.util.Collection;
     15 
     16 import org.jacoco.core.internal.analysis.CounterImpl;
     17 
     18 /**
     19  * Base implementation for coverage data nodes.
     20  */
     21 public class CoverageNodeImpl implements ICoverageNode {
     22 
     23 	private final ElementType elementType;
     24 
     25 	private final String name;
     26 
     27 	/** Counter for branches. */
     28 	protected CounterImpl branchCounter;
     29 
     30 	/** Counter for instructions. */
     31 	protected CounterImpl instructionCounter;
     32 
     33 	/** Counter for lines */
     34 	protected CounterImpl lineCounter;
     35 
     36 	/** Counter for complexity. */
     37 	protected CounterImpl complexityCounter;
     38 
     39 	/** Counter for methods. */
     40 	protected CounterImpl methodCounter;
     41 
     42 	/** Counter for classes. */
     43 	protected CounterImpl classCounter;
     44 
     45 	/**
     46 	 * Creates a new coverage data node.
     47 	 *
     48 	 * @param elementType
     49 	 *            type of the element represented by this instance
     50 	 * @param name
     51 	 *            name of this node
     52 	 */
     53 	public CoverageNodeImpl(final ElementType elementType, final String name) {
     54 		this.elementType = elementType;
     55 		this.name = name;
     56 		this.branchCounter = CounterImpl.COUNTER_0_0;
     57 		this.instructionCounter = CounterImpl.COUNTER_0_0;
     58 		this.complexityCounter = CounterImpl.COUNTER_0_0;
     59 		this.methodCounter = CounterImpl.COUNTER_0_0;
     60 		this.classCounter = CounterImpl.COUNTER_0_0;
     61 		this.lineCounter = CounterImpl.COUNTER_0_0;
     62 	}
     63 
     64 	/**
     65 	 * Increments the counters by the values given by another element.
     66 	 *
     67 	 * @param child
     68 	 *            counters to add
     69 	 */
     70 	public void increment(final ICoverageNode child) {
     71 		instructionCounter = instructionCounter.increment(child
     72 				.getInstructionCounter());
     73 		branchCounter = branchCounter.increment(child.getBranchCounter());
     74 		lineCounter = lineCounter.increment(child.getLineCounter());
     75 		complexityCounter = complexityCounter.increment(child
     76 				.getComplexityCounter());
     77 		methodCounter = methodCounter.increment(child.getMethodCounter());
     78 		classCounter = classCounter.increment(child.getClassCounter());
     79 	}
     80 
     81 	/**
     82 	 * Increments the counters by the values given by the collection of
     83 	 * elements.
     84 	 *
     85 	 * @param children
     86 	 *            list of nodes, which counters will be added to this node
     87 	 */
     88 	public void increment(final Collection<? extends ICoverageNode> children) {
     89 		for (final ICoverageNode child : children) {
     90 			increment(child);
     91 		}
     92 	}
     93 
     94 	// === ICoverageDataNode ===
     95 
     96 	public ElementType getElementType() {
     97 		return elementType;
     98 	}
     99 
    100 	public String getName() {
    101 		return name;
    102 	}
    103 
    104 	public ICounter getInstructionCounter() {
    105 		return instructionCounter;
    106 	}
    107 
    108 	public ICounter getBranchCounter() {
    109 		return branchCounter;
    110 	}
    111 
    112 	public ICounter getLineCounter() {
    113 		return lineCounter;
    114 	}
    115 
    116 	public ICounter getComplexityCounter() {
    117 		return complexityCounter;
    118 	}
    119 
    120 	public ICounter getMethodCounter() {
    121 		return methodCounter;
    122 	}
    123 
    124 	public ICounter getClassCounter() {
    125 		return classCounter;
    126 	}
    127 
    128 	public ICounter getCounter(final CounterEntity entity) {
    129 		switch (entity) {
    130 		case INSTRUCTION:
    131 			return getInstructionCounter();
    132 		case BRANCH:
    133 			return getBranchCounter();
    134 		case LINE:
    135 			return getLineCounter();
    136 		case COMPLEXITY:
    137 			return getComplexityCounter();
    138 		case METHOD:
    139 			return getMethodCounter();
    140 		case CLASS:
    141 			return getClassCounter();
    142 		}
    143 		throw new AssertionError(entity);
    144 	}
    145 
    146 	public ICoverageNode getPlainCopy() {
    147 		final CoverageNodeImpl copy = new CoverageNodeImpl(elementType, name);
    148 		copy.instructionCounter = CounterImpl.getInstance(instructionCounter);
    149 		copy.branchCounter = CounterImpl.getInstance(branchCounter);
    150 		copy.lineCounter = CounterImpl.getInstance(lineCounter);
    151 		copy.complexityCounter = CounterImpl.getInstance(complexityCounter);
    152 		copy.methodCounter = CounterImpl.getInstance(methodCounter);
    153 		copy.classCounter = CounterImpl.getInstance(classCounter);
    154 		return copy;
    155 	}
    156 
    157 	@Override
    158 	public String toString() {
    159 		final StringBuilder sb = new StringBuilder();
    160 		sb.append(name).append(" [").append(elementType).append("]");
    161 		return sb.toString();
    162 	}
    163 
    164 }
    165