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 org.sonar.api.batch.Decorator;
023    import org.sonar.api.batch.DecoratorContext;
024    import org.sonar.api.batch.DependedUpon;
025    import org.sonar.api.batch.DependsUpon;
026    import org.sonar.api.measures.CoreMetrics;
027    import org.sonar.api.measures.Measure;
028    import org.sonar.api.measures.MeasureUtils;
029    import org.sonar.api.measures.Metric;
030    import org.sonar.api.resources.Project;
031    import org.sonar.api.resources.Resource;
032    
033    import java.util.Arrays;
034    import java.util.List;
035    
036    public class ViolationsDensityDecorator implements Decorator {
037    
038      public boolean shouldExecuteOnProject(Project project) {
039        return true;
040      }
041    
042      @DependsUpon
043      public List<Metric> dependsUponWeightedViolationsAndNcloc() {
044        return Arrays.asList(CoreMetrics.WEIGHTED_VIOLATIONS, CoreMetrics.NCLOC);
045      }
046    
047      @DependedUpon
048      public Metric generatesViolationsDensity() {
049        return CoreMetrics.VIOLATIONS_DENSITY;
050      }
051    
052      public void decorate(Resource resource, DecoratorContext context) {
053        if (shouldDecorateResource(context)) {
054          decorateDensity(context);
055        }
056      }
057    
058      protected boolean shouldDecorateResource(DecoratorContext context) {
059        return context.getMeasure(CoreMetrics.VIOLATIONS_DENSITY) == null;
060      }
061    
062      private void decorateDensity(DecoratorContext context) {
063        Measure ncloc = context.getMeasure(CoreMetrics.NCLOC);
064        if (MeasureUtils.hasValue(ncloc) && ncloc.getValue() > 0.0) {
065          saveDensity(context, ncloc.getValue().intValue());
066        }
067      }
068    
069      private void saveDensity(DecoratorContext context, int ncloc) {
070        Measure debt = context.getMeasure(CoreMetrics.WEIGHTED_VIOLATIONS);
071        Integer debtValue = 0;
072        if (MeasureUtils.hasValue(debt)) {
073          debtValue = debt.getValue().intValue();
074        }
075        double density = calculate(debtValue, ncloc);
076        context.saveMeasure(CoreMetrics.VIOLATIONS_DENSITY, density);
077      }
078    
079      protected static double calculate(int debt, int ncloc) {
080        double rci = (1.0 - ((double) debt / (double) ncloc)) * 100.0;
081        rci = Math.max(rci, 0.0);
082        return rci;
083      }
084    
085      @Override
086      public String toString() {
087        return getClass().getSimpleName();
088      }
089    }