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.squid.decorators;
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.Metric;
029    import org.sonar.api.measures.RangeDistributionBuilder;
030    import org.sonar.api.resources.Java;
031    import org.sonar.api.resources.Project;
032    import org.sonar.api.resources.Resource;
033    import org.sonar.api.resources.Scopes;
034    
035    public final class ChidamberKemererDistributionBuilder implements Decorator {
036    
037      private static final Integer[] LCOM4_LIMITS = { 2, 3, 4, 5, 10 }; // 1 is excluded
038      private static final Integer[] RFC_LIMITS = { 0, 5, 10, 20, 30, 50, 90, 150 };
039    
040      @DependedUpon
041      public Metric generatesLcom4Distribution() {
042        return CoreMetrics.LCOM4_DISTRIBUTION;
043      }
044    
045      @DependsUpon
046      public Metric dependsInLcom4() {
047        return CoreMetrics.LCOM4;
048      }
049    
050      @DependedUpon
051      public Metric generatesRfcDistribution() {
052        return CoreMetrics.RFC_DISTRIBUTION;
053      }
054    
055      @DependsUpon
056      public Metric dependsInRfc() {
057        return CoreMetrics.RFC;
058      }
059    
060      public void decorate(Resource resource, DecoratorContext context) {
061        if (shouldExecuteOn(resource)) {
062          RangeDistributionBuilder lcom4Distribution = new RangeDistributionBuilder(CoreMetrics.LCOM4_DISTRIBUTION, LCOM4_LIMITS);
063          RangeDistributionBuilder rfcDistribution = new RangeDistributionBuilder(CoreMetrics.RFC_DISTRIBUTION, RFC_LIMITS);
064    
065          for (DecoratorContext childContext : context.getChildren()) {
066            if (Scopes.isFile(childContext.getResource())) {
067              addMeasureToDistribution(childContext, lcom4Distribution, CoreMetrics.LCOM4);
068              addMeasureToDistribution(childContext, rfcDistribution, CoreMetrics.RFC);
069            }
070          }
071    
072          saveDistribution(context, lcom4Distribution);
073          saveDistribution(context, rfcDistribution);
074        }
075      }
076    
077      private void addMeasureToDistribution(DecoratorContext childContext, RangeDistributionBuilder distribution, Metric metric) {
078        Measure measure = childContext.getMeasure(metric);
079        if (measure != null) {
080          distribution.add(measure.getIntValue());
081        }
082      }
083    
084      private void saveDistribution(DecoratorContext context, RangeDistributionBuilder distribution) {
085        Measure measure = distribution.build(false);
086        if (measure != null) {
087          context.saveMeasure(measure);
088        }
089      }
090    
091      boolean shouldExecuteOn(Resource resource) {
092        return Scopes.isDirectory(resource);
093      }
094    
095      public boolean shouldExecuteOnProject(Project project) {
096        return Java.KEY.equals(project.getLanguageKey());
097      }
098    }