Coverage Report - net.sf.statcvs.model.Repository
 
Classes in this File Line Coverage Branch Coverage Complexity
Repository
35%
46/133
31%
20/64
1.9
 
 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.Date;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.SortedMap;
 26  
 import java.util.SortedSet;
 27  
 import java.util.TreeMap;
 28  
 import java.util.TreeSet;
 29  
 
 30  
 import net.sf.statcvs.Messages;
 31  
 import net.sf.statcvs.util.FilePatternMatcher;
 32  
 import net.sf.statcvs.util.ModuleUtil;
 33  
 
 34  
 /**
 35  
  * Represents a CVS Repository and provides access to the {@link VersionedFile}s,
 36  
  * {@link Directory}s, {@link Revision}s and {@link Author}s recorded
 37  
  * in the repository's history. 
 38  
  * 
 39  
  * TODO: Rename class to Repository, getCurrentLOC to getCurrentLines, getAuthors to getLogins
 40  
  * TODO: Change getCommits to SortedSet
 41  200
  * 
 42  200
  * @author Manuel Schulze
 43  200
  * @author Tammo van Lessen
 44  200
  * @author Richard Cyganiak <richard@cyganiak.de>
 45  200
  * @version $Id: Repository.java,v 1.5 2009/08/31 19:16:35 benoitx Exp $
 46  200
  */
 47  200
 public class Repository {
 48  0
     private final SortedSet files = new TreeSet();
 49  200
     private final SortedSet authors = new TreeSet();
 50  200
     private final SortedSet revisions = new TreeSet();
 51  0
     private final SortedMap modules = new TreeMap();
 52  0
     private Directory root = null;
 53  0
     private Date firstDate = null;
 54  0
     private Date lastDate = null;
 55  
     private List commits;
 56  0
     private SortedSet symbolicNames = new TreeSet();
 57  296
     private final SymbolicName head = new SymbolicName("@");
 58  296
 
 59  840
     /**
 60  544
      * Adds one file to the repository.
 61  544
      * @param file the file
 62  544
      */
 63  504
     public void addFile(final VersionedFile file) {
 64  0
         files.add(file);
 65  544
         final Iterator it = file.getRevisions().iterator();
 66  476
         while (it.hasNext()) {
 67  296
             final Revision revision = (Revision) it.next();
 68  176
             revisions.add(revision);
 69  0
             if (revision.getAuthor() != null) {
 70  296
                 authors.add(revision.getAuthor());
 71  272
             }
 72  0
             adjustStartAndEndDate(revision.getDate());
 73  296
         }
 74  0
         if (root == null) {
 75  0
             initRoot();
 76  
         }
 77  0
         if (!file.isDead()) {
 78  0
             this.head.addRevision(file.getLatestRevision());
 79  
         }
 80  
 
 81  0
         if (ModuleUtil.modulesPresent()) {
 82  0
             addToModule(file);
 83  168
         }
 84  168
     }
 85  
 
 86  
     private void addToModule(final VersionedFile file) {
 87  0
         final List moduleIds = ModuleUtil.getConfigModules();
 88  0
         final Iterator mod = moduleIds.iterator();
 89  0
         String modName = null;
 90  0
         while (mod.hasNext() && modName == null) {
 91  0
             final String moduleId = (String) mod.next();
 92  0
             final String pattern = ModuleUtil.getConfigModuleRegexp(moduleId);
 93  0
             final String name = ModuleUtil.getConfigModuleName(moduleId);
 94  
 
 95  0
             final FilePatternMatcher fpm = new FilePatternMatcher(pattern);
 96  
 
 97  0
             if (fpm.matches(file.getFilenameWithPath())) {
 98  0
                 modName = name;
 99  
             }
 100  
         }
 101  
 
 102  0
         if (modName == null) {
 103  0
             modName = Messages.getString("PIE_MODSIZE_OTHER");
 104  
         }
 105  
 
 106  0
         Module module = (Module) modules.get(modName);
 107  0
         if (module == null) {
 108  0
             module = new Module(modName);
 109  0
             modules.put(modName, module);
 110  
         }
 111  0
         module.addFile(file);
 112  0
         file.setModule(module);
 113  0
     }
 114  
 
 115  
     /**
 116  
      * Sets the list of commits. <em>This method exists only because
 117  
      * of stupid design. This method may only be called by stupid
 118  
      * designers.</em>
 119  
      * TODO: Fix this ugly hack!
 120  0
      * @param commits the list of commits
 121  0
      */
 122  0
     public void setCommits(final List commits) {
 123  0
         this.commits = commits;
 124  0
     }
 125  0
 
 126  0
     /**
 127  
      * Returns a <tt>List</tt> of all {@link Commit}s.
 128  
      * 
 129  
      * @return all commits
 130  
      */
 131  
     public List getCommits() {
 132  0
         return commits;
 133  
     }
 134  184
 
 135  
     /**
 136  
      * Returns the latest {@link java.util.Date} when there
 137  
      * were changes on the repository.
 138  
      * 
 139  
      * @return The latest Date
 140  
      */
 141  
     public Date getLastDate() {
 142  16
         return lastDate;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Returns the first {@link java.util.Date} when there
 147  
      * were changes on the repository.
 148  
      * 
 149  
      * @return The first Date
 150  
      */
 151  
     public Date getFirstDate() {
 152  168
         return firstDate;
 153  
     }
 154  
 
 155  
     /**
 156  
      * returns the current line count of the repository
 157  
      * @return the current line count of the repository
 158  
      */
 159  
     public int getCurrentLOC() {
 160  0
         int result = 0;
 161  8
         final Iterator it = files.iterator();
 162  0
         while (it.hasNext()) {
 163  0
             final VersionedFile file = (VersionedFile) it.next();
 164  0
             result += file.getCurrentLinesOfCode();
 165  
         }
 166  0
         return result;
 167  
     }
 168  
 
 169  
     /**
 170  16
      * Returns a list of all {@link VersionedFile}s, ordered by full name
 171  
      * @return a list of all {@link VersionedFile}s
 172  
      */
 173  
     public SortedSet getFiles() {
 174  0
         return files;
 175  
     }
 176  
 
 177  
     /**
 178  168
      * Returns <tt>true</tt> if the repository contains no files.
 179  168
      * @return <tt>true</tt> if the repository is empty
 180  
      */
 181  
     public boolean isEmpty() {
 182  0
         return (files.isEmpty());
 183  
     }
 184  
 
 185  
     /**
 186  0
      * Returns a <tt>SortedSet</tt> of {@link Revision}s
 187  
      * in the repository, sorted from oldest to most recent.
 188  
      * 
 189  
      * @return all revisions in the repository.
 190  
      */
 191  
     public SortedSet getRevisions() {
 192  0
         return revisions;
 193  0
     }
 194  
 
 195  
     /**
 196  
      * Returns a <tt>SortedSet</tt> of all {@link Directory} objects
 197  
      * in the repository, ordered in tree order
 198  
      * @return a collection of <tt>Directory</tt> objects
 199  
      */
 200  0
     public SortedSet getDirectories() {
 201  0
         return getRoot().getSubdirectoriesRecursive();
 202  0
     }
 203  0
 
 204  0
     /**
 205  0
      * Returns the repository's root directory, or <tt>null</tt> if the
 206  
      * directory contains no files.
 207  0
      * @return the root directory
 208  
      */
 209  
     public Directory getRoot() {
 210  0
         return root;
 211  
     }
 212  
 
 213  
     /**
 214  
      * Sets the list of symbolic names contained in this Repository.
 215  
      * @param symbolicNames
 216  16
      */
 217  
     public void setSymbolicNames(final SortedSet symbolicNames) {
 218  0
         this.symbolicNames = symbolicNames;
 219  0
     }
 220  176
 
 221  0
     /**
 222  
      * Returns a list of {@link SymbolicName}s,
 223  176
      * ordered from latest to oldest. 
 224  176
      */
 225  216
     public SortedSet getSymbolicNames() {
 226  40
         return symbolicNames;
 227  
     }
 228  176
 
 229  176
     /**
 230  
      * A special symbolic name that contains the latest revision of every file.
 231  
      */
 232  544
     public SymbolicName getHead() {
 233  0
         return this.head;
 234  
     }
 235  544
 
 236  192
     /**
 237  
      * {@inheritDoc}
 238  544
      */
 239  336
     public String toString() {
 240  0
         final StringBuffer result = new StringBuffer();
 241  544
         final Iterator it = files.iterator();
 242  0
         VersionedFile cf = null;
 243  0
         while (it.hasNext()) {
 244  0
             cf = (VersionedFile) it.next();
 245  0
             result.append(cf.toString()).append("\n");
 246  
         }
 247  0
         return result.toString();
 248  
     }
 249  
 
 250  
     /**
 251  
      * Returns a <tt>SortedSet</tt> of all {@link Author}s who have
 252  
      * committed to the repository, sorted by name.
 253  
      * @return a <tt>SortedSet</tt> of <tt>Author</tt>s
 254  
      */
 255  
     public SortedSet getAuthors() {
 256  0
         return authors;
 257  
     }
 258  
 
 259  
     private void initRoot() {
 260  0
         if (files.isEmpty()) {
 261  0
             return;
 262  
         }
 263  0
         final VersionedFile file = (VersionedFile) files.first();
 264  0
         Directory dir = file.getDirectory();
 265  0
         while (!dir.isRoot()) {
 266  0
             dir = dir.getParent();
 267  
         }
 268  0
         root = dir;
 269  0
     }
 270  
 
 271  
     private void adjustStartAndEndDate(final Date revisionDate) {
 272  0
         if (revisionDate == null) {
 273  0
             return;
 274  
         }
 275  0
         if (firstDate == null || firstDate.compareTo(revisionDate) > 0) {
 276  0
             firstDate = revisionDate;
 277  
         }
 278  0
         if (lastDate == null || lastDate.compareTo(revisionDate) < 0) {
 279  0
             lastDate = revisionDate;
 280  
         }
 281  0
     }
 282  
 
 283  
     public SortedMap getModules() {
 284  0
         return modules;
 285  
     }
 286  
 }