001    /*
002     * Copyright (C) 2010 Evgeny Mandrikov
003     *
004     * Sonar-IDE is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Lesser General Public
006     * License as published by the Free Software Foundation; either
007     * version 3 of the License, or (at your option) any later version.
008     *
009     * Sonar-IDE is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with Sonar-IDE; if not, write to the Free Software
016     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
017     */
018    
019    package org.sonar.ide.shared.coverage;
020    
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    /**
027     * @author Evgeny Mandrikov
028     * @since 0.2
029     */
030    public final class CoverageData {
031      private Map<Integer, String> hitsByLine = new HashMap<Integer, String>();
032      private Map<Integer, String> branchHitsByLine = new HashMap<Integer, String>();
033    
034      public CoverageData(Map<Integer, String> hitsByLine, Map<Integer, String> branchHitsByLine) {
035        this.hitsByLine = hitsByLine;
036        this.branchHitsByLine = branchHitsByLine;
037      }
038    
039      public String getHitsByLine(int line) {
040        return hitsByLine.get(line);
041      }
042    
043      public String getBranchHitsByLine(int line) {
044        return branchHitsByLine.get(line);
045      }
046    
047      public CoverageStatus getCoverageStatus(int line) {
048        String hits = hitsByLine.get(line);
049        String branchHits = branchHitsByLine.get(line);
050        boolean hasLineCoverage = (null != hits);
051        boolean hasBranchCoverage = (null != branchHits);
052        boolean lineIsCovered = (hasLineCoverage && Integer.parseInt(hits) > 0);
053        boolean branchIsCovered = (hasBranchCoverage && "100%".equals(branchHits));
054    
055        if (lineIsCovered) {
056          if (branchIsCovered) {
057            return CoverageStatus.FULLY_COVERED;
058          } else if (hasBranchCoverage) {
059            return CoverageStatus.PARTIALLY_COVERED;
060          } else {
061            return CoverageStatus.FULLY_COVERED;
062          }
063        } else if (hasLineCoverage) {
064          return CoverageStatus.UNCOVERED;
065        }
066        return CoverageStatus.NO_DATA;
067      }
068    
069      /**
070       * For eclipse.
071       */
072      public Collection<CoverageLine> getCoverageLines() {
073        final Collection<CoverageLine> coverageLines = new ArrayList<CoverageLine>();
074        for (final Integer line : hitsByLine.keySet()) {
075          coverageLines.add(new CoverageLine(line, hitsByLine.get(line), branchHitsByLine.get(line)));
076        }
077        return coverageLines;
078      }
079    
080      public enum CoverageStatus {
081        NO_DATA,
082        FULLY_COVERED,
083        PARTIALLY_COVERED,
084        UNCOVERED
085      }
086    
087    }