001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * SonarQube is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.batch.sensor.coverage.internal; 021 022import com.google.common.base.Preconditions; 023import com.google.common.collect.Maps; 024import org.sonar.api.batch.fs.InputFile; 025import org.sonar.api.batch.fs.internal.DefaultInputFile; 026import org.sonar.api.batch.sensor.coverage.CoverageType; 027import org.sonar.api.batch.sensor.coverage.NewCoverage; 028import org.sonar.api.batch.sensor.internal.DefaultStorable; 029import org.sonar.api.batch.sensor.internal.SensorStorage; 030 031import javax.annotation.Nullable; 032 033import java.util.Collections; 034import java.util.SortedMap; 035 036public class DefaultCoverage extends DefaultStorable implements NewCoverage { 037 038 private DefaultInputFile inputFile; 039 private CoverageType type; 040 private int totalCoveredLines = 0; 041 private int totalConditions = 0; 042 private int totalCoveredConditions = 0; 043 private SortedMap<Integer, Integer> hitsByLine = Maps.newTreeMap(); 044 private SortedMap<Integer, Integer> conditionsByLine = Maps.newTreeMap(); 045 private SortedMap<Integer, Integer> coveredConditionsByLine = Maps.newTreeMap(); 046 047 public DefaultCoverage() { 048 super(); 049 } 050 051 public DefaultCoverage(@Nullable SensorStorage storage) { 052 super(storage); 053 } 054 055 @Override 056 public DefaultCoverage onFile(InputFile inputFile) { 057 Preconditions.checkNotNull(inputFile, "file can't be null"); 058 this.inputFile = (DefaultInputFile) inputFile; 059 return this; 060 } 061 062 public InputFile inputFile() { 063 return inputFile; 064 } 065 066 @Override 067 public NewCoverage ofType(CoverageType type) { 068 Preconditions.checkNotNull(type, "type can't be null"); 069 this.type = type; 070 return this; 071 } 072 073 public CoverageType type() { 074 return type; 075 } 076 077 @Override 078 public NewCoverage lineHits(int line, int hits) { 079 if (!hitsByLine.containsKey(line)) { 080 hitsByLine.put(line, hits); 081 if (hits > 0) { 082 totalCoveredLines += 1; 083 } 084 } 085 return this; 086 } 087 088 @Override 089 public NewCoverage conditions(int line, int conditions, int coveredConditions) { 090 if (conditions > 0 && !conditionsByLine.containsKey(line)) { 091 totalConditions += conditions; 092 totalCoveredConditions += coveredConditions; 093 conditionsByLine.put(line, conditions); 094 coveredConditionsByLine.put(line, coveredConditions); 095 } 096 return this; 097 } 098 099 public int coveredLines() { 100 return totalCoveredLines; 101 } 102 103 public int linesToCover() { 104 return hitsByLine.size(); 105 } 106 107 public int conditions() { 108 return totalConditions; 109 } 110 111 public int coveredConditions() { 112 return totalCoveredConditions; 113 } 114 115 public SortedMap<Integer, Integer> hitsByLine() { 116 return Collections.unmodifiableSortedMap(hitsByLine); 117 } 118 119 public SortedMap<Integer, Integer> conditionsByLine() { 120 return Collections.unmodifiableSortedMap(conditionsByLine); 121 } 122 123 public SortedMap<Integer, Integer> coveredConditionsByLine() { 124 return Collections.unmodifiableSortedMap(coveredConditionsByLine); 125 } 126 127 @Override 128 public void doSave() { 129 Preconditions.checkNotNull(inputFile, "Call onFile() first"); 130 Preconditions.checkNotNull(type, "Call ofType() first"); 131 storage.store(this); 132 } 133 134}