001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020
021package org.sonar.api.batch;
022
023import org.sonar.api.measures.CoreMetrics;
024import org.sonar.api.measures.CountDistributionBuilder;
025import org.sonar.api.measures.Measure;
026import org.sonar.api.measures.Metric;
027import org.sonar.api.resources.Language;
028import org.sonar.api.resources.Project;
029import org.sonar.api.resources.Resource;
030
031/**
032 * @deprecated since 2.1, a formula has been implemented on the metric, so no need to have decorator anymore
033 * @since 2.0
034 */
035@Deprecated
036public abstract class AbstractFunctionComplexityDistributionDecorator implements Decorator {
037
038  private CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
039  private Language language;
040
041  public AbstractFunctionComplexityDistributionDecorator(Language language) {
042    this.language = language;
043  }
044
045  @DependedUpon
046  public Metric generatesMetrics() {
047    return CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION;
048  }
049
050  public boolean shouldExecuteOnProject(Project project) {
051    return language.equals(project.getLanguage());
052  }
053
054  public void decorate(Resource resource, DecoratorContext context) {
055    if (shouldDecorateResource(context)) {
056      reset();
057      saveDistribution(context);
058    }
059  }
060
061  private void saveDistribution(DecoratorContext context) {
062    for (Measure childMeasure : context.getChildrenMeasures(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION)) {
063      builder.add(childMeasure);
064    }
065
066    if (!builder.isEmpty()) {
067      context.saveMeasure(builder.build());
068    }
069  }
070
071  private void reset() {
072    builder.clear();
073  }
074
075  private boolean shouldDecorateResource(DecoratorContext context) {
076    return context.getMeasure(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION) == null;
077  }
078
079  @Override
080  public String toString() {
081    return getClass().getSimpleName();
082  }
083}