001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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    
021    package org.sonar.api.batch;
022    
023    import org.sonar.api.measures.CountDistributionBuilder;
024    import org.sonar.api.measures.CoreMetrics;
025    import org.sonar.api.measures.Metric;
026    import org.sonar.api.measures.Measure;
027    import org.sonar.api.resources.Project;
028    import org.sonar.api.resources.Resource;
029    import org.sonar.api.resources.Language;
030    
031    
032    public abstract class AbstractFunctionComplexityDistributionDecorator implements Decorator {
033    
034      private CountDistributionBuilder builder = new CountDistributionBuilder(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
035      private Language language;
036    
037      public AbstractFunctionComplexityDistributionDecorator(Language language) {
038        this.language = language;
039      }
040    
041      @DependedUpon
042      public Metric generatesMetrics() {
043        return CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION;
044      }
045    
046      public boolean shouldExecuteOnProject(Project project) {
047        return language.equals(project.getLanguage());
048      }
049    
050      public void decorate(Resource resource, DecoratorContext context) {
051        if (shouldDecorateResource(context)) {
052          reset();
053          saveDistribution(context);
054        }
055      }
056    
057      private void saveDistribution(DecoratorContext context) {
058        for (Measure childMeasure : context.getChildrenMeasures(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION)) {
059          builder.add(childMeasure);
060        }
061    
062        if (!builder.isEmpty()) {
063          context.saveMeasure(builder.build());
064        }
065      }
066    
067      private void reset() {
068        builder.clear();
069      }
070    
071      private boolean shouldDecorateResource(DecoratorContext context) {
072        return context.getMeasure(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION) == null;
073      }
074    
075      @Override
076      public String toString() {
077        return getClass().getSimpleName();
078      }
079    }