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    package org.sonar.api.batch;
021    
022    import org.sonar.api.measures.CoreMetrics;
023    import org.sonar.api.measures.MeasureUtils;
024    import org.sonar.api.measures.Metric;
025    import org.sonar.api.resources.Language;
026    import org.sonar.api.resources.Project;
027    import org.sonar.api.resources.Resource;
028    
029    import java.util.Arrays;
030    import java.util.List;
031    
032    /**
033     * @deprecated since 2.1, a formula has been implemented on the metric, so no need to have decorator anymore
034     * @since 1.13
035     */
036    @Deprecated
037    public abstract class AbstractFunctionComplexityDecorator implements Decorator {
038    
039      private Language language;
040    
041      /**
042       * @param language this will be use to defined whether the decorator should be executed on a project
043       */
044      public AbstractFunctionComplexityDecorator(Language language) {
045        this.language = language;
046      }
047    
048      /**
049       * {@inheritDoc}
050       */
051      public boolean shouldExecuteOnProject(Project project) {
052        return language.equals(project.getLanguage());
053      }
054    
055      /**
056       * Used to define upstream dependencies
057       */
058      @DependsUpon
059      public List<Metric> dependsUponFileAndComplexityMetrics() {
060        return Arrays.asList(CoreMetrics.FUNCTIONS, CoreMetrics.COMPLEXITY);
061      }
062    
063      /**
064       * Used to define downstream dependencies
065       */
066      @DependedUpon
067      public Metric generateFileComplexityMetric() {
068        return CoreMetrics.FUNCTION_COMPLEXITY;
069      }
070    
071      /**
072       * {@inheritDoc}
073       */
074      public void decorate(Resource resource, DecoratorContext context) {
075        if (!shouldDecorateResource(resource, context)) {
076          return;
077        }
078        Double functions = MeasureUtils.getValue(context.getMeasure(CoreMetrics.FUNCTIONS), null);
079        Double complexity = MeasureUtils.getValue(context.getMeasure(CoreMetrics.COMPLEXITY), null);
080        if (complexity != null && functions != null && functions > 0.0) {
081          context.saveMeasure(CoreMetrics.FUNCTION_COMPLEXITY, complexity / functions);
082        }
083      }
084    
085      private boolean shouldDecorateResource(Resource resource, DecoratorContext context) {
086        return !MeasureUtils.hasValue(context.getMeasure(CoreMetrics.FUNCTION_COMPLEXITY));
087      }
088    }