Home | History | Annotate | Download | only in internal
      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.report.internal;
     13 
     14 import java.io.IOException;
     15 
     16 import org.jacoco.core.analysis.CoverageNodeImpl;
     17 import org.jacoco.core.analysis.IBundleCoverage;
     18 import org.jacoco.core.analysis.ICoverageNode.ElementType;
     19 import org.jacoco.report.IReportGroupVisitor;
     20 import org.jacoco.report.ISourceFileLocator;
     21 
     22 /**
     23  * Internal base visitor to calculate group counter summaries for hierarchical
     24  * reports.
     25  */
     26 public abstract class AbstractGroupVisitor implements IReportGroupVisitor {
     27 
     28 	/** coverage node for this group to total counters */
     29 	protected final CoverageNodeImpl total;
     30 
     31 	private AbstractGroupVisitor lastChild;
     32 
     33 	/**
     34 	 * Creates a new group with the given name.
     35 	 *
     36 	 * @param name
     37 	 *            name for the coverage node created internally
     38 	 */
     39 	protected AbstractGroupVisitor(final String name) {
     40 		total = new CoverageNodeImpl(ElementType.GROUP, name);
     41 	}
     42 
     43 	public final void visitBundle(final IBundleCoverage bundle,
     44 			final ISourceFileLocator locator) throws IOException {
     45 		finalizeLastChild();
     46 		total.increment(bundle);
     47 		handleBundle(bundle, locator);
     48 	}
     49 
     50 	/**
     51 	 * Called to handle the given bundle in a specific way.
     52 	 *
     53 	 * @param bundle
     54 	 *            analyzed bundle
     55 	 * @param locator
     56 	 *            source locator
     57 	 * @throws IOException
     58 	 *             if the report can't be written
     59 	 */
     60 	protected abstract void handleBundle(IBundleCoverage bundle,
     61 			ISourceFileLocator locator) throws IOException;
     62 
     63 	public final IReportGroupVisitor visitGroup(final String name)
     64 			throws IOException {
     65 		finalizeLastChild();
     66 		lastChild = handleGroup(name);
     67 		return lastChild;
     68 	}
     69 
     70 	/**
     71 	 * Called to handle a group with the given name in a specific way.
     72 	 *
     73 	 * @param name
     74 	 *            name of the group
     75 	 * @return created child group
     76 	 * @throws IOException
     77 	 *             if the report can't be written
     78 	 */
     79 	protected abstract AbstractGroupVisitor handleGroup(final String name)
     80 			throws IOException;
     81 
     82 	/**
     83 	 * Must be called at the end of every group.
     84 	 *
     85 	 * @throws IOException
     86 	 *             if the report can't be written
     87 	 */
     88 	public final void visitEnd() throws IOException {
     89 		finalizeLastChild();
     90 		handleEnd();
     91 	}
     92 
     93 	/**
     94 	 * Called to handle the end of this group in a specific way.
     95 	 *
     96 	 * @throws IOException
     97 	 *             if the report can't be written
     98 	 */
     99 	protected abstract void handleEnd() throws IOException;
    100 
    101 	private void finalizeLastChild() throws IOException {
    102 		if (lastChild != null) {
    103 			lastChild.visitEnd();
    104 			total.increment(lastChild.total);
    105 			lastChild = null;
    106 		}
    107 	}
    108 
    109 }
    110