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.*;
027 import org.sonar.api.resources.Java;
028 import org.sonar.api.resources.Project;
029 import org.sonar.api.resources.Resource;
030 import org.sonar.api.resources.Scopes;
031
032 /**
033 * @since 2.6
034 */
035 public final class ClassComplexityDistributionBuilder 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 generatesFunctionComplexityDistribution() {
046 return CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION;
047 }
048
049 public void decorate(Resource resource, DecoratorContext context) {
050 if (shouldExecuteOn(resource, context)) {
051 RangeDistributionBuilder builder = new RangeDistributionBuilder(CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION, LIMITS);
052 for (DecoratorContext childContext : context.getChildren()) {
053 if (Scopes.isProgramUnit(childContext.getResource())) {
054 Measure complexity = childContext.getMeasure(CoreMetrics.COMPLEXITY);
055 if (complexity != null) {
056 builder.add(complexity.getValue());
057 }
058 }
059 }
060 Measure measure = builder.build(true);
061 measure.setPersistenceMode(PersistenceMode.MEMORY);
062 context.saveMeasure(measure);
063 }
064 }
065
066 boolean shouldExecuteOn(Resource resource, DecoratorContext context) {
067 return Scopes.isFile(resource) && context.getMeasure(CoreMetrics.COMPLEXITY) != null;
068 }
069
070 public boolean shouldExecuteOnProject(Project project) {
071 return Java.KEY.equals(project.getLanguageKey());
072 }
073 }