Coverage Report - net.sf.statcvs.reports.TagReport
 
Classes in this File Line Coverage Branch Coverage Complexity
TagReport
0%
0/77
0%
0/22
2.375
 
 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  0
     public TagReport(final ReportConfig config) {
 37  0
         this.config = config;
 38  0
         this.repository = config.getRepository();
 39  0
     }
 40  
 
 41  
     public void calculate() {
 42  0
         final LinkColumn tags = new LinkColumn("Name");
 43  0
         final SimpleTextColumn dates = new SimpleTextColumn("Date");
 44  0
         final IntegerColumn loc = new IntegerColumn("Lines");
 45  0
         loc.setShowPercentages(false);
 46  0
         final IntegerColumn churn = new IntegerColumn("LOC Churn");
 47  0
         final IntegerColumn developers = new IntegerColumn("Developers");
 48  0
         developers.setShowPercentages(false);
 49  
 
 50  0
         this.table = new Table("Repository Tags");
 51  0
         this.table.setKeysInFirstColumn(true);
 52  0
         this.table.addColumn(tags);
 53  0
         this.table.addColumn(dates);
 54  0
         this.table.addColumn(loc);
 55  0
         this.table.addColumn(churn);
 56  0
         this.table.addColumn(developers);
 57  
 
 58  0
         final List tagList = new ArrayList(this.repository.getSymbolicNames());
 59  0
         tagList.add(this.repository.getHead());
 60  0
         Collections.reverse(tagList);
 61  0
         final Iterator it = tagList.iterator();
 62  0
         while (it.hasNext()) {
 63  0
             final SymbolicName tag = (SymbolicName) it.next();
 64  0
             final Date startDate = getStartDate(tag);
 65  0
             final List revisions = getRevisionsBetween(startDate, tag.getDate());
 66  0
             if (tag == this.repository.getHead()) {
 67  0
                 tags.addValue(null, "(now)");
 68  
             } else {
 69  0
                 tags.addValue(CommitLogPageMaker.getURL(tag.getDate()) + "#" + CommitLogPageMaker.getAnchor(tag), tag.getName());
 70  
             }
 71  0
             dates.addValue(HTML.getDate(tag.getDate()));
 72  0
             loc.addValue(getLOC(tag));
 73  0
             churn.addValue(getLOCChurn(revisions));
 74  0
             developers.addValue(countDevelopers(revisions));
 75  0
         }
 76  0
     }
 77  
 
 78  
     public Table getTable() {
 79  0
         return this.table;
 80  
     }
 81  
 
 82  
     private int getLOC(final SymbolicName tag) {
 83  0
         int loc = 0;
 84  0
         final Iterator it = tag.getRevisions().iterator();
 85  0
         while (it.hasNext()) {
 86  0
             final Revision revision = (Revision) it.next();
 87  0
             loc += revision.getLines();
 88  0
         }
 89  0
         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  0
         final List revisions = new ArrayList();
 98  0
         final Iterator it = this.repository.getRevisions().iterator();
 99  0
         while (it.hasNext()) {
 100  0
             final Revision revision = (Revision) it.next();
 101  0
             final long time = revision.getDate().getTime();
 102  0
             if (time > start.getTime() && time <= end.getTime()) {
 103  0
                 revisions.add(revision);
 104  
             }
 105  0
         }
 106  0
         return revisions;
 107  
     }
 108  
 
 109  
     private int getLOCChurn(final List revisions) {
 110  0
         int churn = 0;
 111  0
         final Iterator it = revisions.iterator();
 112  0
         while (it.hasNext()) {
 113  0
             final Revision revision = (Revision) it.next();
 114  0
             churn += revision.getNewLines();
 115  0
         }
 116  0
         return churn;
 117  
     }
 118  
 
 119  
     private Date getStartDate(final SymbolicName tag) {
 120  0
         final SortedSet earlierTags = this.repository.getSymbolicNames().headSet(tag);
 121  0
         if (earlierTags.isEmpty()) {
 122  0
             return this.repository.getFirstDate();
 123  
         }
 124  0
         return ((SymbolicName) earlierTags.last()).getDate();
 125  
     }
 126  
 
 127  
     private int countDevelopers(final Collection revisions) {
 128  0
         final Set authors = new HashSet();
 129  0
         Iterator it = revisions.iterator();
 130  0
         while (it.hasNext()) {
 131  0
             final Revision revision = (Revision) it.next();
 132  0
             authors.add(revision.getAuthor());
 133  0
         }
 134  0
         int result = 0;
 135  0
         it = authors.iterator();
 136  0
         while (it.hasNext()) {
 137  0
             final Author author = (Author) it.next();
 138  0
             if (this.config.isDeveloper(author)) {
 139  0
                 result++;
 140  
             }
 141  0
         }
 142  0
         return result;
 143  
     }
 144  
 }