Coverage Report - net.sf.statcvs.model.Directory
 
Classes in this File Line Coverage Branch Coverage Complexity
Directory
0%
0/55
0%
0/18
1.688
 
 1  
 /*
 2  
         StatCvs - CVS statistics generation 
 3  
         Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
 4  
         http://statcvs.sf.net/
 5  
     
 6  
         This library is free software; you can redistribute it and/or
 7  
         modify it under the terms of the GNU Lesser General Public
 8  
         License as published by the Free Software Foundation; either
 9  
         version 2.1 of the License, or (at your option) any later version.
 10  
 
 11  
         This library is distributed in the hope that it will be useful,
 12  
         but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  
         Lesser General Public License for more details.
 15  
 
 16  
         You should have received a copy of the GNU Lesser General Public
 17  
         License along with this library; if not, write to the Free Software
 18  
         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19  
 */
 20  
 package net.sf.statcvs.model;
 21  
 
 22  
 import java.util.Iterator;
 23  
 import java.util.SortedSet;
 24  
 import java.util.TreeSet;
 25  
 
 26  
 /**
 27  
  * Represents a directory in the {@link Repository Repository}, a
 28  
  * container for {@link VersionedFile}s and sub<tt>Directory</tt>s.
 29  
  * A new root directory is created by {@link #createRoot}.
 30  
  * The {@link #createSubdirectory} method creates new subdirectories.
 31  
  * 
 32  
  * TODO: Rename getCurrentLOC to getCurrentLines or getCurrentLineCount
 33  
  * 
 34  
  * @author Richard Cyganiak <richard@cyganiak.de>
 35  
  * @version $Id: Directory.java,v 1.11 2008/04/02 11:22:16 benoitx Exp $
 36  
  */
 37  0
 public abstract class Directory implements Comparable {
 38  0
     private final SortedSet files = new TreeSet();
 39  0
     private final SortedSet directories = new TreeSet();
 40  
 
 41  
     /**
 42  
      * Factory method for creating a new root directory.
 43  
      * @return a new root directory
 44  
      */
 45  
     public static Directory createRoot() {
 46  0
         return new DirectoryRoot();
 47  
     }
 48  
 
 49  
     /**
 50  
      * Factory method for creating a new subdirectory.
 51  
      * @param name the subdirectory's name
 52  
      * @return the subdirectory instance
 53  
      */
 54  
     public Directory createSubdirectory(final String name) {
 55  0
         final Directory result = new DirectoryImpl(this, name);
 56  0
         directories.add(result);
 57  0
         return result;
 58  
     }
 59  
 
 60  
     /**
 61  
      * Returns the directory's name without full path or any slashes, 
 62  
      * for example "src".
 63  
      * @return the directory's name
 64  
      */
 65  
     public abstract String getName();
 66  
 
 67  
     /**
 68  
      * Returns the directory's full path with trailing slash,
 69  
      * for example "src/net/sf/statcvs/".
 70  
      * @return the directory's path
 71  
      */
 72  
     public abstract String getPath();
 73  
 
 74  
     /**
 75  
      * Returns the directory's parent directory or <tt>null</tt> if it is the root
 76  
      * @return the directory's parent.
 77  
      */
 78  
     public abstract Directory getParent();
 79  
 
 80  
     /**
 81  
      * Returns <tt>true</tt> if this is the root of the directory tree.
 82  
      * @return <tt>true</tt> if this is the root of the directory tree
 83  
      */
 84  
     public abstract boolean isRoot();
 85  
 
 86  
     /**
 87  
      * Returns the level of this directory in the direcotry tree.
 88  
      * The root has level 0, its subdirectories have level 1, and so forth.
 89  
      * @return the level of this directory in the directory tree
 90  
      */
 91  
     public abstract int getDepth();
 92  
 
 93  
     /**
 94  
      * Returns all {@link VersionedFile} objects in this directory, ordered
 95  
      * by filename. Files in subdirectories are not included.
 96  
      * @return the files in this directory
 97  
      */
 98  
     public SortedSet getFiles() {
 99  0
         return files;
 100  
     }
 101  
 
 102  
     /**
 103  
      * Returns all {@link Revision}s to files in
 104  
      * this directory, in order from oldest to most recent.
 105  
      * @return list of <tt>Revision</tt>s for this directory
 106  
      */
 107  
     public SortedSet getRevisions() {
 108  0
         final SortedSet result = new TreeSet();
 109  0
         final Iterator iterator = files.iterator();
 110  0
         while (iterator.hasNext()) {
 111  0
             final VersionedFile file = (VersionedFile) iterator.next();
 112  0
             result.addAll(file.getRevisions());
 113  0
         }
 114  0
         return result;
 115  
     }
 116  
 
 117  
     /**
 118  
      * Returns a <tt>SortedSet</tt> of all immediate subdirectories,
 119  
      * ordered by name.
 120  
      * @return <tt>SortedSet</tt> of {@link Directory} objects
 121  
      */
 122  
     public SortedSet getSubdirectories() {
 123  0
         return directories;
 124  
     }
 125  
 
 126  
     /**
 127  
      * Returns a list of all subdirectories, including their subdirectories
 128  
      * and this directory itself. The list is preordered, beginning with this
 129  
      * directory itself.
 130  
      * @return <tt>SortedSet</tt> of {@link Directory} objects
 131  
      */
 132  
     public SortedSet getSubdirectoriesRecursive() {
 133  0
         final SortedSet result = new TreeSet();
 134  0
         result.add(this);
 135  0
         final Iterator it = directories.iterator();
 136  0
         while (it.hasNext()) {
 137  0
             final Directory dir = (Directory) it.next();
 138  0
             result.addAll(dir.getSubdirectoriesRecursive());
 139  0
         }
 140  0
         return result;
 141  
     }
 142  
 
 143  
     /**
 144  
      * Returns the number of lines in this directory. The returned number
 145  
      * will be for the current revisions of all files.
 146  
      * @return lines in this directory
 147  
      */
 148  
     public int getCurrentLOC() {
 149  0
         int result = 0;
 150  0
         final Iterator it = files.iterator();
 151  0
         while (it.hasNext()) {
 152  0
             final VersionedFile file = (VersionedFile) it.next();
 153  0
             result += file.getCurrentLinesOfCode();
 154  0
         }
 155  0
         return result;
 156  
     }
 157  
 
 158  
     /**
 159  
      * Returns the number of files in this directory. Deleted files are not
 160  
      * counted.
 161  
      * @return number of files in this directory
 162  
      */
 163  
     public int getCurrentFileCount() {
 164  0
         int result = 0;
 165  0
         final Iterator it = files.iterator();
 166  0
         while (it.hasNext()) {
 167  0
             final VersionedFile file = (VersionedFile) it.next();
 168  0
             if (!file.isDead()) {
 169  0
                 result++;
 170  
             }
 171  0
         }
 172  0
         return result;
 173  
     }
 174  
 
 175  
     /**
 176  
      * Returns <code>true</code> if all files in this directory and its
 177  
      * subdirectories are deleted, or if it doesn't have any files and
 178  
      * subdirectories at all.
 179  
      * @return <code>true</code> if the directory is currently empty
 180  
      */
 181  
     public boolean isEmpty() {
 182  0
         Iterator it = files.iterator();
 183  0
         while (it.hasNext()) {
 184  0
             final VersionedFile file = (VersionedFile) it.next();
 185  0
             if (!file.isDead()) {
 186  0
                 return false;
 187  
             }
 188  0
         }
 189  0
         it = directories.iterator();
 190  0
         while (it.hasNext()) {
 191  0
             final Directory subdir = (Directory) it.next();
 192  0
             if (!subdir.isEmpty()) {
 193  0
                 return false;
 194  
             }
 195  0
         }
 196  0
         return true;
 197  
     }
 198  
 
 199  
     /**
 200  
      * Compares this directory to another one, based on their full names.
 201  
      * @see java.lang.Comparable#compareTo(java.lang.Object)
 202  
      */
 203  
     public int compareTo(final Object o) {
 204  0
         return getPath().compareTo(((Directory) o).getPath());
 205  
     }
 206  
 
 207  
     /**
 208  
      * Adds a file to this directory.
 209  
      * @param file a file in this directory
 210  
      */
 211  
     void addFile(final VersionedFile file) {
 212  0
         files.add(file);
 213  0
     }
 214  
 }