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 * A pre-implementation to decorate the number of files
035
036 * @since 1.10
037 */
038 public abstract class AbstractFilesDecorator 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 AbstractFilesDecorator(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 downstream dependencies
058 */
059 @DependedUpon
060 public Metric generateFilesMetric() {
061 return CoreMetrics.FILES;
062 }
063
064 /**
065 * {@inheritDoc}
066 */
067 public void decorate(Resource resource, DecoratorContext context) {
068 if (MeasureUtils.hasValue(context.getMeasure(CoreMetrics.FILES))) {
069 return;
070 }
071
072 if (ResourceUtils.isEntity(resource)) {
073 if (!ResourceUtils.isUnitTestClass(resource)) {
074 context.saveMeasure(CoreMetrics.FILES, 1.0);
075 }
076 } else {
077 Collection<Measure> childrenMeasures = context.getChildrenMeasures(CoreMetrics.FILES);
078 context.saveMeasure(CoreMetrics.FILES, MeasureUtils.sum(true, childrenMeasures));
079 }
080 }
081 }