Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Repository |
|
| 1.7222222222222223;1.722 |
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 | 150 | public class Repository { |
42 | 150 | private final SortedSet files = new TreeSet(); |
43 | 150 | private final SortedSet authors = new TreeSet(); |
44 | 150 | private final SortedSet revisions = new TreeSet(); |
45 | 150 | private Directory root = null; |
46 | 150 | private Date firstDate = null; |
47 | 150 | private Date lastDate = null; |
48 | private List commits; | |
49 | 150 | private SortedSet symbolicNames = new TreeSet(); |
50 | 150 | 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 | 222 | files.add(file); |
58 | 222 | final Iterator it = file.getRevisions().iterator(); |
59 | 630 | while (it.hasNext()) { |
60 | 408 | final Revision revision = (Revision) it.next(); |
61 | 408 | revisions.add(revision); |
62 | 408 | if (revision.getAuthor() != null) { |
63 | 378 | authors.add(revision.getAuthor()); |
64 | } | |
65 | 408 | adjustStartAndEndDate(revision.getDate()); |
66 | 340 | } |
67 | 222 | if (root == null) { |
68 | 132 | initRoot(); |
69 | } | |
70 | 222 | if (!file.isDead()) { |
71 | 204 | this.head.addRevision(file.getLatestRevision()); |
72 | } | |
73 | 222 | } |
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 | 126 | this.commits = commits; |
84 | 126 | } |
85 | ||
86 | /** | |
87 | * Returns a <tt>List</tt> of all {@link Commit}s. | |
88 | * | |
89 | * @return all commits | |
90 | */ | |
91 | public List getCommits() { | |
92 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | int result = 0; |
121 | 0 | final Iterator it = files.iterator(); |
122 | 0 | while (it.hasNext()) { |
123 | 0 | final VersionedFile file = (VersionedFile) it.next(); |
124 | 0 | result += file.getCurrentLinesOfCode(); |
125 | 0 | } |
126 | 0 | 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 | 138 | 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 | 12 | 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 | 126 | 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 | 6 | 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 | 12 | 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 | 126 | this.symbolicNames = symbolicNames; |
179 | 126 | } |
180 | ||
181 | /** | |
182 | * Returns a list of {@link SymbolicName}s, | |
183 | * ordered from latest to oldest. | |
184 | */ | |
185 | public SortedSet getSymbolicNames() { | |
186 | 0 | return symbolicNames; |
187 | } | |
188 | ||
189 | /** | |
190 | * A special symbolic name that contains the latest revision of every file. | |
191 | */ | |
192 | public SymbolicName getHead() { | |
193 | 0 | return this.head; |
194 | } | |
195 | ||
196 | /** | |
197 | * {@inheritDoc} | |
198 | */ | |
199 | public String toString() { | |
200 | 0 | final StringBuffer result = new StringBuffer(); |
201 | 0 | final Iterator it = files.iterator(); |
202 | 0 | VersionedFile cf = null; |
203 | 0 | while (it.hasNext()) { |
204 | 0 | cf = (VersionedFile) it.next(); |
205 | 0 | result.append(cf.toString()).append("\n"); |
206 | } | |
207 | 0 | 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 | 12 | return authors; |
217 | } | |
218 | ||
219 | private void initRoot() { | |
220 | 132 | if (files.isEmpty()) { |
221 | 0 | return; |
222 | } | |
223 | 132 | final VersionedFile file = (VersionedFile) files.first(); |
224 | 132 | Directory dir = file.getDirectory(); |
225 | 162 | while (!dir.isRoot()) { |
226 | 30 | dir = dir.getParent(); |
227 | } | |
228 | 132 | root = dir; |
229 | 132 | } |
230 | ||
231 | private void adjustStartAndEndDate(final Date revisionDate) { | |
232 | 408 | if (revisionDate == null) { |
233 | 0 | return; |
234 | } | |
235 | 408 | if (firstDate == null || firstDate.compareTo(revisionDate) > 0) { |
236 | 144 | firstDate = revisionDate; |
237 | } | |
238 | 408 | if (lastDate == null || lastDate.compareTo(revisionDate) < 0) { |
239 | 252 | lastDate = revisionDate; |
240 | } | |
241 | 408 | } |
242 | } |