001 /*
002 * Cobertura - http://cobertura.sourceforge.net/
003 *
004 * Copyright (C) 2003 jcoverage ltd.
005 * Copyright (C) 2005 Mark Doliner
006 * Copyright (C) 2005 Jeremy Thomerson
007 *
008 * Cobertura is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License as published
010 * by the Free Software Foundation; either version 2 of the License,
011 * or (at your option) any later version.
012 *
013 * Cobertura is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016 * General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with Cobertura; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
021 * USA
022 */
023
024 package net.sourceforge.cobertura.coveragedata;
025
026 import java.util.Collection;
027 import java.util.Iterator;
028 import java.util.SortedMap;
029 import java.util.SortedSet;
030 import java.util.TreeMap;
031 import java.util.TreeSet;
032
033 public class PackageData extends CoverageDataContainer
034 implements Comparable, HasBeenInstrumented
035 {
036
037 private static final long serialVersionUID = 7;
038
039 private String name;
040
041 public PackageData(String name)
042 {
043 if (name == null)
044 throw new IllegalArgumentException(
045 "Package name must be specified.");
046 this.name = name;
047 }
048
049 public void addClassData(ClassData classData)
050 {
051 if (children.containsKey(classData.getBaseName()))
052 throw new IllegalArgumentException("Package " + this.name
053 + " already contains a class with the name "
054 + classData.getBaseName());
055
056 // Each key is a class basename, stored as an String object.
057 // Each value is information about the class, stored as a ClassData object.
058 children.put(classData.getBaseName(), classData);
059 }
060
061 /**
062 * This is required because we implement Comparable.
063 */
064 public int compareTo(Object o)
065 {
066 if (!o.getClass().equals(PackageData.class))
067 return Integer.MAX_VALUE;
068 return this.name.compareTo(((PackageData)o).name);
069 }
070
071 public boolean contains(String name)
072 {
073 return this.children.containsKey(name);
074 }
075
076 /**
077 * Returns true if the given object is an instance of the
078 * PackageData class, and it contains the same data as this
079 * class.
080 */
081 public boolean equals(Object obj)
082 {
083 if (this == obj)
084 return true;
085 if ((obj == null) || !(obj.getClass().equals(this.getClass())))
086 return false;
087
088 PackageData packageData = (PackageData)obj;
089 return super.equals(obj) && this.name.equals(packageData.name);
090 }
091
092 public SortedSet getClasses()
093 {
094 return new TreeSet(this.children.values());
095 }
096
097 public String getName()
098 {
099 return this.name;
100 }
101
102 public String getSourceFileName()
103 {
104 return this.name.replace('.', '/');
105 }
106
107 public Collection getSourceFiles()
108 {
109 SortedMap sourceFileDatas = new TreeMap();
110 Iterator iter = this.children.values().iterator();
111 while (iter.hasNext()) {
112 ClassData classData = (ClassData)iter.next();
113 String 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 }