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  	$RCSfile: FilesRevisionCountComparator.java,v $
21  	$Date: 2008/04/02 11:22:15 $
22  */
23  package net.sf.statcvs.reports;
24  
25  import java.util.Comparator;
26  import java.util.Iterator;
27  
28  import net.sf.statcvs.model.Revision;
29  import net.sf.statcvs.model.VersionedFile;
30  
31  /**
32   * Compares two files according to their number of changes (revisions).
33   * If two files have the same number of changes, the number of changed
34   * lines of code is used.
35   * 
36   * @author Richard Cyganiak <rcyg@gmx.de>
37   * @version $Id: FilesRevisionCountComparator.java,v 1.5 2008/04/02 11:22:15 benoitx Exp $
38   */
39  public class FilesRevisionCountComparator implements Comparator {
40  
41      /**
42       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
43       */
44      public int compare(final Object o1, final Object o2) {
45          final VersionedFile file1 = (VersionedFile) o1;
46          final VersionedFile file2 = (VersionedFile) o2;
47          if (file1.getRevisions().size() < file2.getRevisions().size()) {
48              return 2;
49          } else if (file1.getRevisions().size() > file2.getRevisions().size()) {
50              return -2;
51          } else {
52              int lines1 = 0;
53              Iterator it = file1.getRevisions().iterator();
54              while (it.hasNext()) {
55                  final Revision rev = (Revision) it.next();
56                  lines1 += rev.getNewLines();
57              }
58              int lines2 = 0;
59              it = file2.getRevisions().iterator();
60              while (it.hasNext()) {
61                  final Revision rev = (Revision) it.next();
62                  lines2 += rev.getNewLines();
63              }
64              if (lines1 < lines2) {
65                  return 1;
66              } else if (lines1 > lines2) {
67                  return -1;
68              } else {
69                  return 0;
70              }
71          }
72      }
73  }