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    package org.sonar.api.batch;
021    
022    import org.sonar.api.measures.CoreMetrics;
023    import org.sonar.api.measures.Measure;
024    import org.sonar.api.measures.MeasureUtils;
025    import org.sonar.api.measures.Metric;
026    import org.sonar.api.resources.Language;
027    import org.sonar.api.resources.Project;
028    import org.sonar.api.resources.Resource;
029    import org.sonar.api.resources.ResourceUtils;
030    
031    import java.util.Collection;
032    
033    /**
034     * @since 1.10
035     */
036    public abstract class AbstractDirectoriesDecorator implements Decorator {
037    
038      private Language language;
039    
040      public AbstractDirectoriesDecorator(Language language) {
041        this.language = language;
042      }
043    
044      public boolean shouldExecuteOnProject(Project project) {
045        return language.equals(project.getLanguage());
046      }
047    
048      @DependedUpon
049      public Metric generateDirectoriesMetric() {
050        return CoreMetrics.DIRECTORIES;
051      }
052    
053      public void decorate(Resource resource, DecoratorContext context) {
054        if (MeasureUtils.hasValue(context.getMeasure(CoreMetrics.DIRECTORIES))) {
055          return;
056        }
057    
058        if (ResourceUtils.isSpace(resource)) {
059          context.saveMeasure(CoreMetrics.DIRECTORIES, 1.0);
060    
061        } else if (ResourceUtils.isSet(resource)) {
062          Collection<Measure> childrenMeasures = context.getChildrenMeasures(CoreMetrics.DIRECTORIES);
063          context.saveMeasure(CoreMetrics.DIRECTORIES, MeasureUtils.sum(true, childrenMeasures));
064        }
065      }
066    }