001/**
002 * www.jcoverage.com
003 * Copyright (C)2003 jcoverage ltd.
004 *
005 * This file is part of jcoverage.
006 *
007 * jcoverage is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published
009 * by the Free Software Foundation; either version 2 of the License,
010 * or (at your option) any later version.
011 *
012 * jcoverage is distributed in the hope that it will be useful, but
013 * WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with jcoverage; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020 * USA
021 *
022 */
023package com.jcoverage.coverage.reporting.html;
024
025import java.util.Comparator;
026import java.util.Set;
027import java.util.TreeSet;
028
029import org.apache.log4j.Logger;
030
031import com.jcoverage.coverage.reporting.collation.JavaFileLine;
032import com.jcoverage.coverage.reporting.collation.PackageSummaryPage;
033import com.jcoverage.coverage.reporting.collation.ReportSummaryPackageLine;
034import com.jcoverage.coverage.reporting.collation.ReportSummaryPage;
035import com.jcoverage.reporting.Column;
036import com.jcoverage.reporting.Line;
037import com.jcoverage.reporting.LineCategory;
038import com.jcoverage.reporting.Page;
039import com.jcoverage.reporting.staticgen.StaticView;
040
041/**
042 *
043 */
044public class CoverageView implements StaticView {
045  
046  static Logger logger=Logger.getLogger(CoverageView.class);
047  
048  public final static int ASCENDING=1;
049  public final static int DESCENDING=-1;
050
051  Comparator packageComparator,fileComparator;
052  String label;
053  int direction;
054
055  class CoverageComparator implements Comparator {
056
057    Column column;
058    int direction;
059
060    CoverageComparator(Column column,int direction) {
061      this.column=column;
062      this.direction=direction;
063    }
064
065    public int compare(Object o1,Object o2) {
066      if (o1 instanceof Line && o2 instanceof Line) {
067
068        double cov1=((Double)((Line)o1).getField(column)).doubleValue();
069        double cov2=((Double)((Line)o2).getField(column)).doubleValue();
070
071        if(logger.isDebugEnabled()) {
072          logger.debug("Comparing "+cov1+" with "+cov2);
073        }
074
075        if (o1.equals(o2)) {
076          return 0;
077        } else if (cov1<cov2) {
078          return direction;
079        } else {
080          return -direction;
081        }
082      } else {
083        throw new ClassCastException("Arguments must both be of type "+Line.class.getName());
084      }
085    }
086  }
087
088  public CoverageView(String label,int direction) {
089    this.label=label;
090    this.direction=direction;
091    this.packageComparator=new CoverageComparator(ReportSummaryPackageLine.COLUMN_AVERAGE_COVERAGE,direction);
092    this.fileComparator=new CoverageComparator(JavaFileLine.COLUMN_COVERAGE,direction);
093  }
094
095  public Set orderLines(Set lines,LineCategory category) {
096    if (category.equals(ReportSummaryPage.CATEGORY_PACKAGE_SUMMARY)) {
097      Set set=new TreeSet(packageComparator);
098      set.addAll(lines);
099      return set;
100    } else if (category.equals(PackageSummaryPage.CATEGORY_JAVAFILES)) {
101      Set set=new TreeSet(fileComparator);
102      set.addAll(lines);
103      return set;
104    } else {
105      throw new IllegalArgumentException("Illegal category "+category);
106    }
107  }
108
109  public String getFilenameModifier(Page page) {
110    if (page.getClass().equals(ReportSummaryPage.class) || page.getClass().equals(PackageSummaryPage.class)) {
111      switch(direction) {
112      case ASCENDING: return "cova";
113      default: return "covd";
114      }
115    } else {
116      return null;
117    }
118  }
119
120  public String getLabel() {
121    return label;
122  }
123
124}