KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.SortedMap JavaDoc;
29 import java.util.SortedSet JavaDoc;
30 import java.util.TreeMap JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 public class PackageData extends CoverageDataContainer
34         implements Comparable JavaDoc, HasBeenInstrumented
35 {
36
37     private static final long serialVersionUID = 5;
38
39     private String JavaDoc name;
40
41     public PackageData(String JavaDoc name)
42     {
43         if (name == null)
44             throw new IllegalArgumentException JavaDoc(
45                     "Package name must be specified.");
46         this.name = name;
47     }
48     
49     public void addClassData(ClassData classData)
50     {
51         if (children.containsKey(classData.getBaseName()))
52             throw new IllegalArgumentException JavaDoc("Package " + this.name
53                     + " already contains a class with the name "
54                     + classData.getBaseName());
55
56         // Each key is a class basename, stored as an String object.
57
// Each value is information about the class, stored as a ClassData object.
58
children.put(classData.getBaseName(), classData);
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(PackageData.class))
67             return Integer.MAX_VALUE;
68         return this.name.compareTo(((PackageData)o).name);
69     }
70
71     public boolean contains(String JavaDoc name)
72     {
73         return this.children.containsKey(name);
74     }
75
76     /**
77      * Returns true if the given object is an instance of the
78      * PackageData class, and it contains the same data as this
79      * class.
80      */

81     public boolean equals(Object JavaDoc obj)
82     {
83         if (this == obj)
84             return true;
85         if ((obj == null) || !(obj.getClass().equals(this.getClass())))
86             return false;
87
88         PackageData packageData = (PackageData)obj;
89         return super.equals(obj) && this.name.equals(packageData.name);
90     }
91
92     public SortedSet JavaDoc getClasses()
93     {
94         return new TreeSet JavaDoc(this.children.values());
95     }
96
97     public String JavaDoc getName()
98     {
99         return this.name;
100     }
101
102     public String JavaDoc getSourceFileName()
103     {
104         return this.name.replace('.', '/');
105     }
106
107     public Collection JavaDoc getSourceFiles()
108     {
109         SortedMap JavaDoc sourceFileDatas = new TreeMap JavaDoc();
110         Iterator JavaDoc iter = this.children.values().iterator();
111         while (iter.hasNext()) {
112             ClassData classData = (ClassData)iter.next();
113             String JavaDoc sourceFileName = classData.getSourceFileName();
114             SourceFileData sourceFileData = (SourceFileData)sourceFileDatas.get(sourceFileName);
115             if (sourceFileData == null)
116             {
117                 sourceFileData = new SourceFileData(sourceFileName);
118                 sourceFileDatas.put(sourceFileName, sourceFileData);
119             }
120             sourceFileData.addClassData(classData);
121         }
122         return sourceFileDatas.values();
123     }
124
125     public int hashCode()
126     {
127         return this.name.hashCode();
128     }
129
130 }
131
Popular Tags