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     */
023    package com.jcoverage.coverage.reporting.html;
024    
025    import java.text.Collator;
026    import java.util.Comparator;
027    import java.util.Set;
028    import java.util.TreeSet;
029    
030    import org.apache.log4j.Logger;
031    
032    import com.jcoverage.coverage.reporting.collation.JavaFileLine;
033    import com.jcoverage.coverage.reporting.collation.PackageSummaryPage;
034    import com.jcoverage.coverage.reporting.collation.ReportSummaryPackageLine;
035    import com.jcoverage.coverage.reporting.collation.ReportSummaryPage;
036    import com.jcoverage.reporting.Column;
037    import com.jcoverage.reporting.Line;
038    import com.jcoverage.reporting.LineCategory;
039    import com.jcoverage.reporting.Page;
040    import com.jcoverage.reporting.View;
041    import com.jcoverage.reporting.staticgen.StaticView;
042    
043    /**
044     *
045     */
046    public class AlphabeticalView implements StaticView {
047      
048      static Logger logger=Logger.getLogger(AlphabeticalView.class);
049      
050      Comparator packageComparator=new AlphabeticalComparator(ReportSummaryPackageLine.COLUMN_PACKAGE_NAME);
051      Comparator filenameComparator=new AlphabeticalComparator(JavaFileLine.COLUMN_FILE_NAME);
052    
053      String label,id;
054    
055      class AlphabeticalComparator implements Comparator {
056    
057        Column column;
058        Comparator delegate=Collator.getInstance();
059    
060        AlphabeticalComparator(Column column) {
061          this.column=column;
062        }
063    
064        public int compare(Object o1,Object o2) {
065          if (o1 instanceof Line && o2 instanceof Line) {
066            String s1=(String)((Line)o1).getField(column);
067            String s2=(String)((Line)o2).getField(column);
068            return delegate.compare(s1,s2);
069          } else {
070            throw new ClassCastException("Arguments must both be of type "+Line.class.getName());
071          }
072        }
073      }
074    
075      public AlphabeticalView(String label) {
076        this.label=label;
077      }
078    
079      public Set orderLines(Set lines,LineCategory category) {
080        if (category.equals(ReportSummaryPage.CATEGORY_PACKAGE_SUMMARY)) {
081          Set set=new TreeSet(packageComparator);
082          set.addAll(lines);
083          return set;
084        } else if (category.equals(PackageSummaryPage.CATEGORY_JAVAFILES)) {
085          Set set=new TreeSet(filenameComparator);
086          set.addAll(lines);
087          return set;
088        } else {
089          throw new IllegalArgumentException("Illegal category "+category);
090        }
091      }
092    
093      public String getFilenameModifier(Page page) {
094        if (page.getClass().equals(ReportSummaryPage.class)) {
095          return "alpha";
096        } else if (page.getClass().equals(PackageSummaryPage.class)) {
097          return "alpha";
098        } else {
099          return null;
100        }
101      }
102    
103      public String getLabel() {
104        return label;
105      }
106    
107    }