AbstractCounter.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 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 *******************************************************************************/
   12package org.jacoco.core.analysis;
   13
   14/**
   15 * Base class for {@link ICounter} implementations.
   16 * 
   17 * @author Marc R. Hoffmann
   18 * @version 0.4.1.20101007204400
   19 */
   20public abstract class AbstractCounter implements ICounter {
   21
   22    /** total number of items */
   23    protected int total;
   24
   25    /** covered number of items */
   26    protected int covered;
   27
   28    /**
   29     * Creates a instance with the given numbers.
   30     * 
   31     * @param total
   32     *            number of total items
   33     * @param covered
   34     *            number of covered items
   35     */
   36    protected AbstractCounter(final int total, final int covered) {
   37        this.total = total;
   38        this.covered = covered;
   39    }
   40
   41    public int getTotalCount() {
   42        return total;
   43    }
   44
   45    public int getCoveredCount() {
   46        return covered;
   47    }
   48
   49    public int getMissedCount() {
   50        return total - covered;
   51    }
   52
   53    public double getCoveredRatio() {
   54        return (double) covered / total;
   55    }
   56
   57    public double getMissedRatio() {
   58        return (double) (total - covered) / total;
   59    }
   60
   61    @Override
   62    public boolean equals(final Object obj) {
   63        if (obj instanceof ICounter) {
   64            final ICounter that = (ICounter) obj;
   65            return this.total == that.getTotalCount()
   66                    && this.covered == that.getCoveredCount();
   67        } else {
   68            return false;
   69        }
   70    }
   71
   72    @Override
   73    public int hashCode() {
   74        return total ^ covered * 17;
   75    }
   76
   77    @Override
   78    public String toString() {
   79        final StringBuilder b = new StringBuilder("Counter["); //$NON-NLS-1$
   80        b.append(getCoveredCount());
   81        b.append('/').append(getTotalCount());
   82        b.append(']');
   83        return b.toString();
   84    }
   85
   86}