KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
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.util.Iterator JavaDoc;
27 import java.util.SortedSet JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 import net.sourceforge.cobertura.util.StringUtil;
31
32 public class SourceFileData extends CoverageDataContainer
33         implements Comparable JavaDoc, HasBeenInstrumented
34 {
35
36     private static final long serialVersionUID = 3;
37
38     private String JavaDoc name;
39
40     /**
41      * @param name In the format, "net/sourceforge/cobertura/coveragedata/SourceFileData.java"
42      */

43     public SourceFileData(String JavaDoc name)
44     {
45         if (name == null)
46             throw new IllegalArgumentException JavaDoc(
47                     "Source file name must be specified.");
48         this.name = name;
49     }
50
51     public void addClassData(ClassData classData)
52     {
53         if (children.containsKey(classData.getBaseName()))
54             throw new IllegalArgumentException JavaDoc("Source file " + this.name
55                     + " already contains a class with the name "
56                     + classData.getBaseName());
57
58         // Each key is a class basename, stored as an String object.
59
// Each value is information about the class, stored as a ClassData object.
60
children.put(classData.getBaseName(), classData);
61     }
62
63     /**
64      * This is required because we implement Comparable.
65      */

66     public int compareTo(Object JavaDoc o)
67     {
68         if (!o.getClass().equals(SourceFileData.class))
69             return Integer.MAX_VALUE;
70         return this.name.compareTo(((SourceFileData)o).name);
71     }
72
73     public boolean contains(String JavaDoc name)
74     {
75         return this.children.containsKey(name);
76     }
77
78     public boolean containsInstrumentationInfo()
79     {
80         // Return false if any of our child ClassData's does not
81
// contain instrumentation info
82
Iterator JavaDoc iter = this.children.values().iterator();
83         while (iter.hasNext())
84         {
85             ClassData classData = (ClassData)iter.next();
86             if (!classData.containsInstrumentationInfo())
87                 return false;
88         }
89         return true;
90     }
91
92     /**
93      * Returns true if the given object is an instance of the
94      * SourceFileData class, and it contains the same data as this
95      * class.
96      */

97     public boolean equals(Object JavaDoc obj)
98     {
99         if (this == obj)
100             return true;
101         if ((obj == null) || !(obj.getClass().equals(this.getClass())))
102             return false;
103
104         SourceFileData sourceFileData = (SourceFileData)obj;
105         return super.equals(obj)
106                 && this.name.equals(sourceFileData.name);
107     }
108
109     public String JavaDoc getBaseName()
110     {
111         String JavaDoc fullNameWithoutExtension;
112         int lastDot = this.name.lastIndexOf('.');
113         if (lastDot == -1)
114         {
115             fullNameWithoutExtension = this.name;
116         }
117         else
118         {
119             fullNameWithoutExtension = this.name.substring(0, lastDot);
120         }
121
122         int lastSlash = fullNameWithoutExtension.lastIndexOf('/');
123         if (lastSlash == -1)
124         {
125             return fullNameWithoutExtension;
126         }
127         return fullNameWithoutExtension.substring(lastSlash + 1);
128     }
129
130     public SortedSet JavaDoc getClasses()
131     {
132         return new TreeSet JavaDoc(this.children.values());
133     }
134
135     public long getHitCount(int lineNumber)
136     {
137         Iterator JavaDoc iter = this.children.values().iterator();
138         while (iter.hasNext())
139         {
140             ClassData classData = (ClassData)iter.next();
141             if (classData.isValidSourceLineNumber(lineNumber))
142                 return classData.getHitCount(lineNumber);
143         }
144         return 0;
145     }
146
147     public String JavaDoc getName()
148     {
149         return this.name;
150     }
151
152     /**
153      * @return The name of this source file without the file extension
154      * in the format
155      * "net.sourceforge.cobertura.coveragedata.SourceFileData"
156      */

157     public String JavaDoc getNormalizedName()
158     {
159         String JavaDoc fullNameWithoutExtension;
160         int lastDot = this.name.lastIndexOf('.');
161         if (lastDot == -1)
162         {
163             fullNameWithoutExtension = this.name;
164         }
165         else
166         {
167             fullNameWithoutExtension = this.name.substring(0, lastDot);
168         }
169
170         return StringUtil.replaceAll(fullNameWithoutExtension, "/", ".");
171     }
172
173     /**
174      * @return The name of the package that this source file is in.
175      * In the format "net.sourceforge.cobertura.coveragedata"
176      */

177     public String JavaDoc getPackageName()
178     {
179         int lastSlash = this.name.lastIndexOf('/');
180         if (lastSlash == -1)
181         {
182             return null;
183         }
184         return StringUtil.replaceAll(this.name.substring(0, lastSlash), "/",
185                 ".");
186     }
187
188     public int hashCode()
189     {
190         return this.name.hashCode();
191     }
192
193     public boolean isValidSourceLineNumber(int lineNumber)
194     {
195         Iterator JavaDoc iter = this.children.values().iterator();
196         while (iter.hasNext())
197         {
198             ClassData classData = (ClassData)iter.next();
199             if (classData.isValidSourceLineNumber(lineNumber))
200                 return true;
201         }
202         return false;
203     }
204
205 }
206
Popular Tags