001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program 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 * This program 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.measures; 021 022import java.util.ArrayList; 023import java.util.Arrays; 024import java.util.Collection; 025import java.util.Collections; 026import java.util.List; 027import java.util.SortedMap; 028import java.util.TreeMap; 029import org.sonar.api.batch.sensor.SensorContext; 030import org.sonar.api.utils.KeyValueFormat; 031 032/** 033 * @since 2.7 034 * @deprecated since 5.2 use {@link SensorContext#newCoverage()} 035 */ 036@Deprecated 037public final class CoverageMeasuresBuilder { 038 039 /** 040 * Metrics of generated measures 041 */ 042 public static final List<Metric> METRICS = Arrays.<Metric>asList( 043 CoreMetrics.LINES_TO_COVER, CoreMetrics.UNCOVERED_LINES, CoreMetrics.COVERAGE_LINE_HITS_DATA, 044 CoreMetrics.CONDITIONS_TO_COVER, CoreMetrics.UNCOVERED_CONDITIONS, CoreMetrics.CONDITIONS_BY_LINE, 045 CoreMetrics.COVERED_CONDITIONS_BY_LINE); 046 047 private int totalCoveredLines = 0; 048 private int totalConditions = 0; 049 private int totalCoveredConditions = 0; 050 private SortedMap<Integer, Integer> hitsByLine = new TreeMap<>(); 051 private SortedMap<Integer, Integer> conditionsByLine = new TreeMap<>(); 052 private SortedMap<Integer, Integer> coveredConditionsByLine = new TreeMap<>(); 053 054 private CoverageMeasuresBuilder() { 055 // use the factory 056 } 057 058 public CoverageMeasuresBuilder reset() { 059 totalCoveredLines = 0; 060 totalConditions = 0; 061 totalCoveredConditions = 0; 062 hitsByLine.clear(); 063 conditionsByLine.clear(); 064 coveredConditionsByLine.clear(); 065 return this; 066 } 067 068 public CoverageMeasuresBuilder setHits(int lineId, int hits) { 069 if (!hitsByLine.containsKey(lineId)) { 070 hitsByLine.put(lineId, hits); 071 if (hits > 0) { 072 totalCoveredLines += 1; 073 } 074 } 075 return this; 076 } 077 078 public CoverageMeasuresBuilder setConditions(int lineId, int conditions, int coveredConditions) { 079 if (conditions > 0 && !conditionsByLine.containsKey(lineId)) { 080 totalConditions += conditions; 081 totalCoveredConditions += coveredConditions; 082 conditionsByLine.put(lineId, conditions); 083 coveredConditionsByLine.put(lineId, coveredConditions); 084 } 085 return this; 086 } 087 088 public int getCoveredLines() { 089 return totalCoveredLines; 090 } 091 092 public int getLinesToCover() { 093 return hitsByLine.size(); 094 } 095 096 public int getConditions() { 097 return totalConditions; 098 } 099 100 public int getCoveredConditions() { 101 return totalCoveredConditions; 102 } 103 104 public SortedMap<Integer, Integer> getHitsByLine() { 105 return Collections.unmodifiableSortedMap(hitsByLine); 106 } 107 108 public SortedMap<Integer, Integer> getConditionsByLine() { 109 return Collections.unmodifiableSortedMap(conditionsByLine); 110 } 111 112 public SortedMap<Integer, Integer> getCoveredConditionsByLine() { 113 return Collections.unmodifiableSortedMap(coveredConditionsByLine); 114 } 115 116 public Collection<Measure> createMeasures() { 117 Collection<Measure> measures = new ArrayList<>(); 118 if (getLinesToCover() > 0) { 119 measures.add(new Measure(CoreMetrics.LINES_TO_COVER, (double) getLinesToCover())); 120 measures.add(new Measure(CoreMetrics.UNCOVERED_LINES, (double) (getLinesToCover() - getCoveredLines()))); 121 measures.add(new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA).setData(KeyValueFormat.format(hitsByLine)).setPersistenceMode(PersistenceMode.DATABASE)); 122 } 123 if (getConditions() > 0) { 124 measures.add(new Measure(CoreMetrics.CONDITIONS_TO_COVER, (double) getConditions())); 125 measures.add(new Measure(CoreMetrics.UNCOVERED_CONDITIONS, (double) (getConditions() - getCoveredConditions()))); 126 measures.add(createConditionsByLine()); 127 measures.add(createCoveredConditionsByLine()); 128 } 129 return measures; 130 } 131 132 private Measure createCoveredConditionsByLine() { 133 return new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE) 134 .setData(KeyValueFormat.format(coveredConditionsByLine)) 135 .setPersistenceMode(PersistenceMode.DATABASE); 136 } 137 138 private Measure createConditionsByLine() { 139 return new Measure(CoreMetrics.CONDITIONS_BY_LINE) 140 .setData(KeyValueFormat.format(conditionsByLine)) 141 .setPersistenceMode(PersistenceMode.DATABASE); 142 } 143 144 public static CoverageMeasuresBuilder create() { 145 return new CoverageMeasuresBuilder(); 146 } 147 148}