KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > coveragedata > CoverageDataContainer


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2003 jcoverage ltd.
5  * Copyright (C) 2005 Mark Doliner
6  * Copyright (C) 2005 Jeremy Thomerson
7  * Copyright (C) 2005 Mark Sinke
8  *
9  * Cobertura is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published
11  * by the Free Software Foundation; either version 2 of the License,
12  * or (at your option) any later version.
13  *
14  * Cobertura is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Cobertura; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  */

24
25 package net.sourceforge.cobertura.coveragedata;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * <p>
34  * Coverage data information is typically serialized to a file.
35  * </p>
36  *
37  * <p>
38  * This class implements HasBeenInstrumented so that when cobertura
39  * instruments itself, it will omit this class. It does this to
40  * avoid an infinite recursion problem because instrumented classes
41  * make use of this class.
42  * </p>
43  */

44 public abstract class CoverageDataContainer
45         implements CoverageData, HasBeenInstrumented, Serializable JavaDoc
46 {
47
48     /**
49      * Each key is the name of a child, usually stored as a String or
50      * an Integer object. Each value is information about the child,
51      * stored as an object that implements the CoverageData interface.
52      */

53     Map JavaDoc children = new HashMap JavaDoc();
54
55     /**
56      * Determine if this CoverageDataContainer is equal to
57      * another one. Subclasses should override this and
58      * make sure they implement the hashCode method.
59      *
60      * @param obj An object to test for equality.
61      * @return True if the objects are equal.
62      */

63     public boolean equals(Object JavaDoc obj)
64     {
65         if (this == obj)
66             return true;
67         if ((obj == null) || !(obj.getClass().equals(this.getClass())))
68             return false;
69
70         CoverageDataContainer coverageDataContainer = (CoverageDataContainer)obj;
71         return this.children.equals(coverageDataContainer.children);
72     }
73
74     /**
75      * @return The average branch coverage rate for all children
76      * in this container.
77      */

78     public double getBranchCoverageRate()
79     {
80         int number = 0;
81         int numberCovered = 0;
82         Iterator JavaDoc iter = this.children.values().iterator();
83         while (iter.hasNext())
84         {
85             CoverageData coverageContainer = (CoverageData)iter.next();
86             number += coverageContainer.getNumberOfValidBranches();
87             numberCovered += coverageContainer.getNumberOfCoveredBranches();
88         }
89         if (number == 0)
90         {
91             // no branches, therefore 100% branch coverage.
92
return 1d;
93         }
94         return (double)numberCovered / number;
95     }
96
97     /**
98      * Get a child from this container with the specified
99      * key.
100      * @param name The key used to lookup the child in the
101      * map.
102      * @return The child object, if found, or null if not found.
103      */

104     public CoverageData getChild(String JavaDoc name)
105     {
106         return (CoverageData)this.children.get(name);
107     }
108
109     /**
110      * @return The average line coverage rate for all children
111      * in this container. This number will be a decimal
112      * between 0 and 1, inclusive.
113      */

114     public double getLineCoverageRate()
115     {
116         int number = 0;
117         int numberCovered = 0;
118         Iterator JavaDoc iter = this.children.values().iterator();
119         while (iter.hasNext())
120         {
121             CoverageData coverageContainer = (CoverageData)iter.next();
122             number += coverageContainer.getNumberOfValidLines();
123             numberCovered += coverageContainer.getNumberOfCoveredLines();
124         }
125         if (number == 0)
126         {
127             // no lines, therefore 100% line coverage.
128
return 1d;
129         }
130         return (double)numberCovered / number;
131     }
132
133     /**
134      * @return The number of children in this container.
135      */

136     public int getNumberOfChildren()
137     {
138         return this.children.size();
139     }
140
141     public int getNumberOfCoveredBranches()
142     {
143         int number = 0;
144         Iterator JavaDoc iter = this.children.values().iterator();
145         while (iter.hasNext())
146         {
147             CoverageData coverageContainer = (CoverageData)iter.next();
148             number += coverageContainer.getNumberOfCoveredBranches();
149         }
150         return number;
151     }
152
153     public int getNumberOfCoveredLines()
154     {
155         int number = 0;
156         Iterator JavaDoc iter = this.children.values().iterator();
157         while (iter.hasNext())
158         {
159             CoverageData coverageContainer = (CoverageData)iter.next();
160             number += coverageContainer.getNumberOfCoveredLines();
161         }
162         return number;
163     }
164
165     public int getNumberOfValidBranches()
166     {
167         int number = 0;
168         Iterator JavaDoc iter = this.children.values().iterator();
169         while (iter.hasNext())
170         {
171             CoverageData coverageContainer = (CoverageData)iter.next();
172             number += coverageContainer.getNumberOfValidBranches();
173         }
174         return number;
175     }
176
177     public int getNumberOfValidLines()
178     {
179         int number = 0;
180         Iterator JavaDoc iter = this.children.values().iterator();
181         while (iter.hasNext())
182         {
183             CoverageData coverageContainer = (CoverageData)iter.next();
184             number += coverageContainer.getNumberOfValidLines();
185         }
186         return number;
187     }
188
189     /**
190      * It is highly recommended that classes extending this
191      * class override this hashCode method and generate a more
192      * effective hash code.
193      */

194     public int hashCode()
195     {
196         return this.children.size();
197     }
198
199     /**
200      * Merge two <code>CoverageDataContainer</code>s.
201      *
202      * @param coverageData The container to merge into this one.
203      */

204     public void merge(CoverageData coverageData)
205     {
206         CoverageDataContainer container = (CoverageDataContainer)coverageData;
207         Iterator JavaDoc iter = container.children.keySet().iterator();
208         while (iter.hasNext())
209         {
210             Object JavaDoc key = iter.next();
211             CoverageData newChild = (CoverageData)container.children.get(key);
212             CoverageData existingChild = (CoverageData)this.children.get(key);
213             if (existingChild != null)
214             {
215                 existingChild.merge(newChild);
216             }
217             else
218             {
219                 // TODO: Shouldn't we be cloning newChild here? I think so that
220
// would be better... but we would need to override the
221
// clone() method all over the place?
222
this.children.put(key, newChild);
223             }
224         }
225     }
226
227 }
228
Popular Tags