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