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