View Javadoc

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