View Javadoc

1   package net.sf.statcvs.reports;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.Date;
7   import java.util.HashSet;
8   import java.util.Iterator;
9   import java.util.List;
10  import java.util.Set;
11  import java.util.SortedSet;
12  
13  import net.sf.statcvs.model.Author;
14  import net.sf.statcvs.model.Repository;
15  import net.sf.statcvs.model.Revision;
16  import net.sf.statcvs.model.SymbolicName;
17  import net.sf.statcvs.output.ReportConfig;
18  import net.sf.statcvs.pages.HTML;
19  import net.sf.statcvs.pages.CommitLogPageMaker;
20  import net.sf.statcvs.reportmodel.IntegerColumn;
21  import net.sf.statcvs.reportmodel.LinkColumn;
22  import net.sf.statcvs.reportmodel.SimpleTextColumn;
23  import net.sf.statcvs.reportmodel.Table;
24  
25  /**
26   * Collects information about repository tags.
27   *
28   * @author Richard Cyganiak (richard@cyganiak.de)
29   * @version $Id: TagReport.java,v 1.3 2009/08/22 10:30:42 benoitx Exp $
30   */
31  public class TagReport implements TableReport {
32      private final ReportConfig config;
33      private final Repository repository;
34      private Table table;
35  
36      public TagReport(final ReportConfig config) {
37          this.config = config;
38          this.repository = config.getRepository();
39      }
40  
41      public void calculate() {
42          final LinkColumn tags = new LinkColumn("Name");
43          final SimpleTextColumn dates = new SimpleTextColumn("Date");
44          final IntegerColumn loc = new IntegerColumn("Lines");
45          loc.setShowPercentages(false);
46          final IntegerColumn churn = new IntegerColumn("LOC Churn");
47          final IntegerColumn developers = new IntegerColumn("Developers");
48          developers.setShowPercentages(false);
49  
50          this.table = new Table("Repository Tags");
51          this.table.setKeysInFirstColumn(true);
52          this.table.addColumn(tags);
53          this.table.addColumn(dates);
54          this.table.addColumn(loc);
55          this.table.addColumn(churn);
56          this.table.addColumn(developers);
57  
58          final List tagList = new ArrayList(this.repository.getSymbolicNames());
59          tagList.add(this.repository.getHead());
60          Collections.reverse(tagList);
61          final Iterator it = tagList.iterator();
62          while (it.hasNext()) {
63              final SymbolicName tag = (SymbolicName) it.next();
64              final Date startDate = getStartDate(tag);
65              final List revisions = getRevisionsBetween(startDate, tag.getDate());
66              if (tag == this.repository.getHead()) {
67                  tags.addValue(null, "(now)");
68              } else {
69                  tags.addValue(CommitLogPageMaker.getURL(tag.getDate()) + "#" + CommitLogPageMaker.getAnchor(tag), tag.getName());
70              }
71              dates.addValue(HTML.getDate(tag.getDate()));
72              loc.addValue(getLOC(tag));
73              churn.addValue(getLOCChurn(revisions));
74              developers.addValue(countDevelopers(revisions));
75          }
76      }
77  
78      public Table getTable() {
79          return this.table;
80      }
81  
82      private int getLOC(final SymbolicName tag) {
83          int loc = 0;
84          final Iterator it = tag.getRevisions().iterator();
85          while (it.hasNext()) {
86              final Revision revision = (Revision) it.next();
87              loc += revision.getLines();
88          }
89          return loc;
90      }
91  
92      /**
93       * @param start Exclusive
94       * @param end Inclusive
95       */
96      private List getRevisionsBetween(final Date start, final Date end) {
97          final List revisions = new ArrayList();
98          final Iterator it = this.repository.getRevisions().iterator();
99          while (it.hasNext()) {
100             final Revision revision = (Revision) it.next();
101             final long time = revision.getDate().getTime();
102             if (time > start.getTime() && time <= end.getTime()) {
103                 revisions.add(revision);
104             }
105         }
106         return revisions;
107     }
108 
109     private int getLOCChurn(final List revisions) {
110         int churn = 0;
111         final Iterator it = revisions.iterator();
112         while (it.hasNext()) {
113             final Revision revision = (Revision) it.next();
114             churn += revision.getNewLines();
115         }
116         return churn;
117     }
118 
119     private Date getStartDate(final SymbolicName tag) {
120         final SortedSet earlierTags = this.repository.getSymbolicNames().headSet(tag);
121         if (earlierTags.isEmpty()) {
122             return this.repository.getFirstDate();
123         }
124         return ((SymbolicName) earlierTags.last()).getDate();
125     }
126 
127     private int countDevelopers(final Collection revisions) {
128         final Set authors = new HashSet();
129         Iterator it = revisions.iterator();
130         while (it.hasNext()) {
131             final Revision revision = (Revision) it.next();
132             authors.add(revision.getAuthor());
133         }
134         int result = 0;
135         it = authors.iterator();
136         while (it.hasNext()) {
137             final Author author = (Author) it.next();
138             if (this.config.isDeveloper(author)) {
139                 result++;
140             }
141         }
142         return result;
143     }
144 }