001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 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 */
020package org.sonar.plugins.squid.decorators;
021
022import org.sonar.api.batch.Decorator;
023import org.sonar.api.batch.DecoratorContext;
024import org.sonar.api.batch.DependedUpon;
025import org.sonar.api.batch.DependsUpon;
026import org.sonar.api.measures.*;
027import org.sonar.api.resources.Java;
028import org.sonar.api.resources.Project;
029import org.sonar.api.resources.Resource;
030import org.sonar.api.resources.Scopes;
031
032/**
033 * @since 3.0
034 */
035public class FileComplexityDistributionDecorator implements Decorator {
036
037  private static final Number[] LIMITS = {0, 5, 10, 20, 30, 60, 90};
038
039  @DependsUpon
040  public Metric dependOnComplexity() {
041    return CoreMetrics.COMPLEXITY;
042  }
043
044  @DependedUpon
045  public Metric generatesComplexityDistribution() {
046    return CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION;
047  }
048
049  public boolean shouldExecuteOnProject(Project project) {
050    return Java.KEY.equals(project.getLanguageKey());
051  }
052
053  private static boolean shouldExecuteOn(Resource resource, DecoratorContext context) {
054    return Scopes.isFile(resource) && context.getMeasure(CoreMetrics.COMPLEXITY) != null;
055  }
056
057  public void decorate(Resource resource, DecoratorContext context) {
058    if (shouldExecuteOn(resource, context)) {
059      RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, LIMITS);
060      Measure complexity = context.getMeasure(CoreMetrics.COMPLEXITY);
061      builder.add(complexity.getValue());
062      Measure measure = builder.build(true);
063      measure.setPersistenceMode(PersistenceMode.MEMORY);
064      context.saveMeasure(measure);
065    }
066  }
067
068}