2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.squid.decorators;
22 import org.sonar.api.batch.Decorator;
23 import org.sonar.api.batch.DecoratorContext;
24 import org.sonar.api.batch.DependedUpon;
25 import org.sonar.api.batch.DependsUpon;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.api.measures.Measure;
28 import org.sonar.api.measures.Metric;
29 import org.sonar.api.measures.RangeDistributionBuilder;
30 import org.sonar.api.resources.Java;
31 import org.sonar.api.resources.Project;
32 import org.sonar.api.resources.Resource;
33 import org.sonar.api.resources.Scopes;
35 public final class ChidamberKemererDistributionBuilder implements Decorator {
37 public static final Integer[] LCOM4_LIMITS = {2, 3, 4, 5, 10};// 1 is excluded
38 public static final Integer[] RFC_LIMITS = {0, 5, 10, 20, 30, 50, 90, 150};
41 public Metric generatesLcom4Distribution() {
42 return CoreMetrics.LCOM4_DISTRIBUTION;
46 public Metric dependsInLcom4() {
47 return CoreMetrics.LCOM4;
51 public Metric generatesRfcDistribution() {
52 return CoreMetrics.RFC_DISTRIBUTION;
56 public Metric dependsInRfc() {
57 return CoreMetrics.RFC;
60 public void decorate(Resource resource, DecoratorContext context) {
61 if (shouldExecuteOn(resource)) {
62 RangeDistributionBuilder lcom4Distribution = new RangeDistributionBuilder(CoreMetrics.LCOM4_DISTRIBUTION, LCOM4_LIMITS);
63 RangeDistributionBuilder rfcDistribution = new RangeDistributionBuilder(CoreMetrics.RFC_DISTRIBUTION, RFC_LIMITS);
65 for (DecoratorContext childContext : context.getChildren()) {
66 if (Scopes.isFile(childContext.getResource())) {
67 addMeasureToDistribution(childContext, lcom4Distribution, CoreMetrics.LCOM4);
68 addMeasureToDistribution(childContext, rfcDistribution, CoreMetrics.RFC);
72 saveDistribution(context, lcom4Distribution);
73 saveDistribution(context, rfcDistribution);
77 private void addMeasureToDistribution(DecoratorContext childContext, RangeDistributionBuilder distribution, Metric metric) {
78 Measure measure = childContext.getMeasure(metric);
79 if (measure != null) {
80 distribution.add(measure.getIntValue());
84 private void saveDistribution(DecoratorContext context, RangeDistributionBuilder distribution) {
85 Measure measure = distribution.build(false);
86 if (measure != null) {
87 context.saveMeasure(measure);
91 boolean shouldExecuteOn(Resource resource) {
92 return Scopes.isDirectory(resource);
95 public boolean shouldExecuteOnProject(Project project) {
96 return Java.KEY.equals(project.getLanguageKey());