Coverage Report - net.sourceforge.cobertura.coveragedata.SourceFileData
 
Classes in this File Line Coverage Branch Coverage Complexity
SourceFileData
N/A
N/A
2.786
 
 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) 2006 Jiri Mares
 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.util.Iterator;
 28  
 import java.util.SortedSet;
 29  
 import java.util.TreeSet;
 30  
 
 31  
 import net.sourceforge.cobertura.util.StringUtil;
 32  
 
 33  
 public class SourceFileData extends CoverageDataContainer
 34  
                 implements Comparable, HasBeenInstrumented
 35  
 {
 36  
 
 37  
         private static final long serialVersionUID = 3;
 38  
 
 39  
         private String name;
 40  
 
 41  
    /**
 42  
     * @param name In the format, "net/sourceforge/cobertura/coveragedata/SourceFileData.java"
 43  
     */
 44  
         public SourceFileData(String name)
 45  
         {
 46  
                 if (name == null)
 47  
                         throw new IllegalArgumentException(
 48  
                                 "Source file name must be specified.");
 49  
                 this.name = name;
 50  
         }
 51  
 
 52  
         public synchronized void addClassData(ClassData classData)
 53  
         {
 54  
                 if (children.containsKey(classData.getBaseName()))
 55  
                         throw new IllegalArgumentException("Source file " + this.name
 56  
                                         + " already contains a class with the name "
 57  
                                         + classData.getBaseName());
 58  
 
 59  
                 // Each key is a class basename, stored as an String object.
 60  
                 // Each value is information about the class, stored as a ClassData object.
 61  
                 children.put(classData.getBaseName(), classData);
 62  
         }
 63  
 
 64  
         /**
 65  
          * This is required because we implement Comparable.
 66  
          */
 67  
         public int compareTo(Object o)
 68  
         {
 69  
                 if (!o.getClass().equals(SourceFileData.class))
 70  
                         return Integer.MAX_VALUE;
 71  
                 return this.name.compareTo(((SourceFileData)o).name);
 72  
         }
 73  
 
 74  
         public boolean contains(String name)
 75  
         {
 76  
                 return this.children.containsKey(name);
 77  
         }
 78  
 
 79  
         public boolean containsInstrumentationInfo()
 80  
         {
 81  
                 // Return false if any of our child ClassData's does not
 82  
                 // contain instrumentation info
 83  
                 Iterator iter = this.children.values().iterator();
 84  
                 while (iter.hasNext())
 85  
                 {
 86  
                         ClassData classData = (ClassData)iter.next();
 87  
                         if (!classData.containsInstrumentationInfo())
 88  
                                 return false;
 89  
                 }
 90  
                 return true;
 91  
         }
 92  
 
 93  
         /**
 94  
          * Returns true if the given object is an instance of the
 95  
          * SourceFileData class, and it contains the same data as this
 96  
          * class.
 97  
          */
 98  
         public boolean equals(Object obj)
 99  
         {
 100  
                 if (this == obj)
 101  
                         return true;
 102  
                 if ((obj == null) || !(obj.getClass().equals(this.getClass())))
 103  
                         return false;
 104  
 
 105  
                 SourceFileData sourceFileData = (SourceFileData)obj;
 106  
                 return super.equals(obj)
 107  
                                 && this.name.equals(sourceFileData.name);
 108  
         }
 109  
 
 110  
         public String getBaseName()
 111  
         {
 112  
                 String fullNameWithoutExtension;
 113  
                 int lastDot = this.name.lastIndexOf('.');
 114  
                 if (lastDot == -1)
 115  
                 {
 116  
                         fullNameWithoutExtension = this.name;
 117  
                 }
 118  
                 else
 119  
                 {
 120  
                         fullNameWithoutExtension = this.name.substring(0, lastDot);
 121  
                 }
 122  
 
 123  
                 int lastSlash = fullNameWithoutExtension.lastIndexOf('/');
 124  
                 if (lastSlash == -1)
 125  
                 {
 126  
                         return fullNameWithoutExtension;
 127  
                 }
 128  
                 return fullNameWithoutExtension.substring(lastSlash + 1);
 129  
         }
 130  
 
 131  
         public SortedSet getClasses()
 132  
         {
 133  
                 return new TreeSet(this.children.values());
 134  
         }
 135  
 
 136  
         public LineData getLineCoverage(int lineNumber)
 137  
         {
 138  
                 Iterator iter = this.children.values().iterator();
 139  
                 while (iter.hasNext())
 140  
                 {
 141  
                         ClassData classData = (ClassData)iter.next();
 142  
                         if (classData.isValidSourceLineNumber(lineNumber))
 143  
                                 return classData.getLineCoverage(lineNumber);
 144  
                 }
 145  
                 return null;
 146  
         }
 147  
 
 148  
         public String getName()
 149  
         {
 150  
                 return this.name;
 151  
         }
 152  
 
 153  
         /**
 154  
          * @return The name of this source file without the file extension
 155  
          *         in the format
 156  
          *         "net.sourceforge.cobertura.coveragedata.SourceFileData"
 157  
          */
 158  
         public String getNormalizedName()
 159  
         {
 160  
                 String fullNameWithoutExtension;
 161  
                 int lastDot = this.name.lastIndexOf('.');
 162  
                 if (lastDot == -1)
 163  
                 {
 164  
                         fullNameWithoutExtension = this.name;
 165  
                 }
 166  
                 else
 167  
                 {
 168  
                         fullNameWithoutExtension = this.name.substring(0, lastDot);
 169  
                 }
 170  
 
 171  
                 return StringUtil.replaceAll(fullNameWithoutExtension, "/", ".");
 172  
         }
 173  
 
 174  
         /**
 175  
          * @return The name of the package that this source file is in.
 176  
          *         In the format "net.sourceforge.cobertura.coveragedata"
 177  
          */
 178  
         public String getPackageName()
 179  
         {
 180  
                 int lastSlash = this.name.lastIndexOf('/');
 181  
                 if (lastSlash == -1)
 182  
                 {
 183  
                         return null;
 184  
                 }
 185  
                 return StringUtil.replaceAll(this.name.substring(0, lastSlash), "/",
 186  
                                 ".");
 187  
         }
 188  
 
 189  
         public int hashCode()
 190  
         {
 191  
                 return this.name.hashCode();
 192  
         }
 193  
 
 194  
         public boolean isValidSourceLineNumber(int lineNumber)
 195  
         {
 196  
                 Iterator iter = this.children.values().iterator();
 197  
                 while (iter.hasNext())
 198  
                 {
 199  
                         ClassData classData = (ClassData)iter.next();
 200  
                         if (classData.isValidSourceLineNumber(lineNumber))
 201  
                                 return true;
 202  
                 }
 203  
                 return false;
 204  
         }
 205  
 
 206  
 }