KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package net.sourceforge.cobertura.coveragedata;
25
26 import java.io.Serializable JavaDoc;
27
28 /**
29  * <p>
30  * This class implements HasBeenInstrumented so that when cobertura
31  * instruments itself, it will omit this class. It does this to
32  * avoid an infinite recursion problem because instrumented classes
33  * make use of this class.
34  * </p>
35  */

36 public class LineData
37         implements Comparable JavaDoc, CoverageData, HasBeenInstrumented, Serializable JavaDoc
38 {
39     private static final long serialVersionUID = 4;
40
41     private long hits;
42     private boolean isBranch;
43     private final int lineNumber;
44     private String JavaDoc methodDescriptor;
45     private String JavaDoc methodName;
46
47     LineData(int lineNumber)
48     {
49         this(lineNumber, null, null);
50     }
51
52     LineData(int lineNumber, String JavaDoc methodName, String JavaDoc methodDescriptor)
53     {
54         this.hits = 0;
55         this.isBranch = false;
56         this.lineNumber = lineNumber;
57         this.methodName = methodName;
58         this.methodDescriptor = methodDescriptor;
59     }
60
61     /**
62      * This is required because we implement Comparable.
63      */

64     public int compareTo(Object JavaDoc o)
65     {
66         if (!o.getClass().equals(LineData.class))
67             return Integer.MAX_VALUE;
68         return this.lineNumber - ((LineData)o).lineNumber;
69     }
70
71     public boolean equals(Object JavaDoc obj)
72     {
73         if (this == obj)
74             return true;
75         if ((obj == null) || !(obj.getClass().equals(this.getClass())))
76             return false;
77
78         LineData lineData = (LineData)obj;
79         return (this.hits == lineData.hits)
80                 && (this.isBranch == lineData.isBranch)
81                 && (this.lineNumber == lineData.lineNumber)
82                 && (this.methodDescriptor.equals(lineData.methodDescriptor))
83                 && (this.methodName.equals(lineData.methodName));
84     }
85
86     public double getBranchCoverageRate()
87     {
88         return (!isBranch || (getHits() > 0)) ? 1 : 0;
89     }
90
91     public long getHits()
92     {
93         return hits;
94     }
95
96     public double getLineCoverageRate()
97     {
98         return (getHits() > 0) ? 1 : 0;
99     }
100
101     public int getLineNumber()
102     {
103         return lineNumber;
104     }
105
106     public String JavaDoc getMethodDescriptor()
107     {
108         return methodDescriptor;
109     }
110
111     public String JavaDoc getMethodName()
112     {
113         return methodName;
114     }
115
116     public int getNumberOfCoveredBranches()
117     {
118         return (isBranch() && (getHits() > 0)) ? 1 : 0;
119     }
120
121     public int getNumberOfCoveredLines()
122     {
123         return (getHits() > 0) ? 1 : 0;
124     }
125
126     public int getNumberOfValidBranches()
127     {
128         return (isBranch()) ? 1 : 0;
129     }
130
131     public int getNumberOfValidLines()
132     {
133         return 1;
134     }
135
136     public int hashCode()
137     {
138         return this.lineNumber;
139     }
140
141     public boolean isBranch()
142     {
143         return isBranch;
144     }
145
146     public void merge(CoverageData coverageData)
147     {
148         LineData lineData = (LineData)coverageData;
149         this.hits += lineData.hits;
150         this.isBranch |= lineData.isBranch();
151         if (lineData.methodName != null)
152             this.methodName = lineData.methodName;
153         if (lineData.methodDescriptor != null)
154             this.methodDescriptor = lineData.methodDescriptor;
155     }
156
157     void setBranch(boolean isBranch)
158     {
159         this.isBranch = isBranch;
160     }
161
162     void setMethodNameAndDescriptor(String JavaDoc name, String JavaDoc descriptor)
163     {
164         this.methodName = name;
165         this.methodDescriptor = descriptor;
166     }
167
168     void touch()
169     {
170         this.hits++;
171     }
172
173 }
174
Popular Tags