KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > analysis > Counter


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: Counter.java 171 2006-11-17 11:30:55Z mtnminds $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core.analysis;
9
10 import com.mountainminds.eclemma.core.analysis.ICounter;
11
12 /**
13  * ICounter implementations. Implementing a factory pattern allows to share
14  * counter instances.
15  *
16  * @author Marc R. Hoffmann
17  * @version $Revision: 171 $
18  */

19 public abstract class Counter implements ICounter {
20
21   /** Max counter value for which singletons are created */
22   private static final int SINGLETON_LIMIT = 10;
23   
24   private static final Counter[][] SINGLETONS = new Counter[SINGLETON_LIMIT + 1][];
25   
26   static {
27     for (int i = 0; i <= SINGLETON_LIMIT; i++) {
28       SINGLETONS[i] = new Counter[i + 1];
29       for (int j = 0; j <= i; j++) SINGLETONS[i][j] = new Fix(i, j);
30     }
31   }
32
33   /** Constant for Counter with 0/0 values. */
34   public static final Counter COUNTER_0_0 = SINGLETONS[0][0];
35
36   /**
37    * Mutable version of the counter.
38    */

39   private static class Var extends Counter {
40     public Var(long total, long covered) {
41       super(total, covered);
42     }
43     public Counter increment(int total, int covered) {
44       this.total += total;
45       this.covered += covered;
46       return this;
47     }
48   }
49
50   /**
51    * Immutable version of the counter.
52    */

53   private static class Fix extends Counter {
54     public Fix(long total, long covered) {
55       super(total, covered);
56     }
57     public Counter increment(int total, int covered) {
58       return getInstance(this.total + total, this.covered + covered);
59     }
60   }
61   
62   /**
63    * Factory method to retrieve a counter with the given number of items.
64    *
65    * @param total total number of items
66    * @param covered covered number of items
67    * @return counter instance
68    */

69   public static Counter getInstance(long total, long covered) {
70     if (total <= SINGLETON_LIMIT && covered <= total) {
71       return SINGLETONS[(int) total][(int) covered];
72     } else {
73       return new Var(total, covered);
74     }
75   }
76   
77   protected long total;
78   protected long covered;
79
80   protected Counter(long total, long covered) {
81     this.total = total;
82     this.covered = covered;
83   }
84
85   /**
86    * Returns a counter with incremented values. It is up to the implementation
87    * whether this conter instance is modified or a new instance is returned.
88    *
89    * @param total number of additional total items
90    * @param covered number of additional covered items
91    * @return counter instance with incremented values
92    */

93   public abstract Counter increment(int total, int covered);
94
95   // ICounter implementation
96

97   public long getTotalCount() {
98     return total;
99   }
100
101   public long getCoveredCount() {
102     return covered;
103   }
104
105   public double getRatio() {
106     return (double) covered / (double) total;
107   }
108
109   public int compareTo(Object JavaDoc obj) {
110     ICounter counter = (ICounter) obj;
111     return Double.compare(getRatio(), counter.getRatio());
112   }
113
114   public boolean equals(Object JavaDoc obj) {
115     if (obj instanceof ICounter) {
116       ICounter counter = (ICounter) obj;
117       return getTotalCount() == counter.getTotalCount()
118           && getCoveredCount() == counter.getCoveredCount();
119     } else {
120       return false;
121     }
122   }
123
124   public int hashCode() {
125     long t = getTotalCount();
126     long c = 17 * getCoveredCount();
127     return (int) (t ^ (t >>> 32) ^ c ^ (c >>> 32));
128   }
129
130   public String JavaDoc toString() {
131     StringBuffer JavaDoc b = new StringBuffer JavaDoc("Counter["); //$NON-NLS-1$
132
b.append(getCoveredCount());
133     b.append('/').append(getTotalCount());
134     b.append(']');
135     return b.toString();
136   }
137
138 }
139
Popular Tags