001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.plugins.core.sensors; 021 022 import com.google.common.collect.Multiset; 023 import com.google.common.collect.TreeMultiset; 024 import org.sonar.api.batch.Decorator; 025 import org.sonar.api.batch.DecoratorContext; 026 import org.sonar.api.batch.DependedUpon; 027 import org.sonar.api.batch.DependsUpon; 028 import org.sonar.api.measures.*; 029 import org.sonar.api.resources.Project; 030 import org.sonar.api.resources.Resource; 031 import org.sonar.api.rules.RulePriority; 032 import org.sonar.api.rules.RuleUtils; 033 import org.sonar.api.utils.KeyValueFormat; 034 035 import java.util.Map; 036 037 public class WeightedViolationsDecorator implements Decorator { 038 039 private Map<RulePriority, Integer> weights; 040 041 042 @DependsUpon 043 public Metric dependsUponViolations() { 044 return CoreMetrics.VIOLATIONS; 045 } 046 047 @DependedUpon 048 public Metric generatesWeightedViolations() { 049 return CoreMetrics.WEIGHTED_VIOLATIONS; 050 } 051 052 public WeightedViolationsDecorator() { 053 } 054 055 /** 056 * for unit tests 057 */ 058 WeightedViolationsDecorator(Map<RulePriority, Integer> weights) { 059 this.weights = weights; 060 } 061 062 private void loadWeights(DecoratorContext context) { 063 if (weights == null && context != null) { 064 weights = RuleUtils.getPriorityWeights(context.getProject().getConfiguration()); 065 } 066 } 067 068 public boolean shouldExecuteOnProject(Project project) { 069 return true; 070 } 071 072 public void decorate(Resource resource, DecoratorContext context) { 073 loadWeights(context); 074 075 double debt = 0.0; 076 Multiset<RulePriority> violationsByPriority = TreeMultiset.create(); 077 078 for (RuleMeasure violations : context.getMeasures(MeasuresFilters.rules(CoreMetrics.VIOLATIONS))) { 079 if (MeasureUtils.hasValue(violations)) { 080 violationsByPriority.add(violations.getRulePriority(), violations.getValue().intValue()); 081 double add = (int) weights.get(violations.getRulePriority()) * violations.getValue(); 082 debt += add; 083 } 084 } 085 086 Measure debtMeasure = new Measure(CoreMetrics.WEIGHTED_VIOLATIONS, debt, KeyValueFormat.format(violationsByPriority)); 087 saveMeasure(context, debtMeasure); 088 } 089 090 private void saveMeasure(DecoratorContext context, Measure debtMeasure) { 091 if (debtMeasure.getValue() > 0.0) { 092 context.saveMeasure(debtMeasure); 093 } 094 } 095 096 }